Linux - How to setup static IP Address via nmcli (NetworkManager CLI)

Linux - How to setup static IP Address via nmcli (NetworkManager CLI)

Disclaimer:
The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author assumes no responsibility for any loss, damage, or issues arising from the use or misuse of the content provided.


How to Check if Interface is using Static IP or DHCP:

1. List interfaces:

# sudo nmcli device status

Example output:
DEVICE TYPE STATE CONNECTION ens18 ethernet connected Wired connection 1 lo loopback connected (externally) lo

2. List current connections:

# sudo nmcli connection show

NAME                UUID                                  TYPE      DEVICE 
Wired connection 1  be3443b1-e1a3-491b-9eb9-3e8bdb1df0c8  ethernet  ens18  
lo                  2896417b-a577-418d-a45f-3c19b2a33f1e  loopback  lo     

3. Check if connection is using DHCP (auto) or not:

# sudo nmcli connection show "<CONNECTION_STRING_NAME>" | grep ipv4.method

For example, if connection name is "Wired connection 1" (use either 'nmcli device status' or 'nmcli connection show' to get actual name):
# sudo nmcli connection show "Wired connection 1" | grep ipv4.method

Output if using static IP:
ipv4.method:                            manual

Output if using DHCP autoconfig:
ipv4.method:                            auto

How to use NetworkManager CLI to configure static IP:

WARNING: Not recommended to do this over a remote session. You'll get disconnected.

You can use this to setup static IP from DHCP or change static IP.

Before you begin, you need to have these information ready:
  1. Valid Connection Name = <CONNECTION_STRING_NAME> (from nmcli)
  2. Valid IP Address. (0.0.0.0 - 255.255.255.255) = <IP_Address>
  3. Valid IP Netmask in bits. (0 - 32) <NetMask>
  4. Gateway IP Address = <GW_IP>
  5. DNS Server IP 1 = <DNS_1> (required)
  6. DNS Server IP 2 = <DNS_2> (optional - you do not need to specify second DNS server)
1. Command syntax:
# sudo nmcli connection modify "<CONNECTION_STRING_NAME>" ipv4.addresses <IP_Address>/<NetMask> ipv4.gateway <GW_IP> ipv4.dns "<DNS_1> <DNS_2>" ipv4.method manual

Example:
# sudo nmcli connection modify "Wired connection 1" ipv4.addresses 192.168.100.1/24 ipv4.gateway 192.168.100.254 ipv4.dns "8.8.8.8" ipv4.method manual

2. Stop the interface.

WARNING: Not recommended to do this over a remote session. You'll get disconnected.

# sudo nmcli connection down "<CONNECTION_STRING_NAME>"
NOTE: This command will bring down the interface and disconnect any remote sessions!

Example:
# sudo nmcli connection down "Wired connection 1"

3. Start the interface.

# sudo nmcli connection up "<CONNECTION_STRING_NAME>"

Example:
# sudo nmcli connection up "Wired connection 1"

To configure interface auto configure via DHCP:

1. Command syntax:
# sudo nmcli connection modify "<CONNECTION_STRING_NAME>"ipv4.method auto

Example:
# sudo nmcli connection modify "Wired connection 1" ipv4.method auto

2. Stop the interface.

WARNING: Not recommended to do this over a remote session. You'll get disconnected.

# sudo nmcli connection down "<CONNECTION_STRING_NAME>"
NOTE: This command will bring down the interface and disconnect any remote sessions!

Example:
# sudo nmcli connection down "Wired connection 1"

3. Start the interface.

# sudo nmcli connection up "<CONNECTION_STRING_NAME>"

Example:
# sudo nmcli connection up "Wired connection 1"

    • Related Articles

    • MySQL - Setup multi-threaded replication

      Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...
    • Installing apache from source on older Linux

      Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...
    • Installing Ollama on MacOS via Homebrew

      Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...
    • Installing Python on MacOS via Homebrew

      Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...
    • Palo Alto

      Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...