Managing network using IFCONFIG & NMCLI command in Linux

Earlier we have discussed how we can configure network connections using three different methods i.e. by editing network interface file, by using GUI & by using nmtui command (READ ARTICLE HERE). In this tutorial, we are going to use two other methods to configure network connections on our RHEL/CentOS machines. We are going to discuss ‘nmcli’ command & ‘ifconfig’ command in Linux.

First utility that we will be using is ‘nmcli’ command & we can configure network on almost any Linux distribution using this method.

(Recommended read :- Assigning multiple IP addresses to a single NIC)

Also Read: How to manage network in Ubuntu 18.04 — Netplan Command

Using NMCLI command in Linux

Syntax for using nmcli is,

$ nmcli [ OPTIONS ] OBJECT { COMMAND | help }

Viewing current network settings

# nmcli general status

to display only the active connections,

# nmcli connection show -a

to display all active and inactive connections, run

# nmcli connection show

to display a list of devices recognized by NetworkManager and their current status, run

# nmcli device status

Assigning IP address to an interface

# nmcli connection add type ethernet con-name CONNECTION_name ifname INTERFACE_name ip4 IP_address gw4 GATEWAY_address

Change the fields as per you network information, an example would be

# nmcli connection add type ethernet con-name office ifname enOs3 ip4 192.168.1.100 gw4 192.168.1.1

Unlike ifconfig command , we can set up a DNS address using nmcli command. To assign a DNS server to an interface, run

# nmcli connection modify office ipv4.dns “8.8.8.8”

Lastly, we will bring up the newly added connection,

# nmcli connection up office ifname enOs3

Enabling or disabling a network interface

# nmcli device connect enOs3

To disable an interface, run

# nmcli device disconnect enOs3

Using Ifconfig command in Linux

View current network settings

# ifconfig

To view network settings all active, inactive interfaces, run

# ifconfig -a

Or to view network settings for a particular interface, run

#ifconfig enos3

Assigning IP address to an interface

# ifconfig enOs3 IP_ADDRESS netmask SUBNET broadcast BROADCAST_ADDRESS

here, we need to pass information as per our network configurations. An example would be

# ifconfig enOs3 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255

This will assign IP 192.168.1.100 on our network interface enOs3. We can also just modify IP or subnet or broadcast address by running the above command with only that parameter like,

# ifconfig enOs3 192.168.1.100
# ifconfig enOs3 netmask 255.255.255.0
# ifconfig enOs3 broadcast 192.168.1.255

Enabling or disabling a network interface

$ ifconfig enOs3 up

To disable a network interface, run

# ifconfig enOs3 down

Note:- When using ifconfig , entries for the gateway address are to be made in /etc/network file or use the following ‘route’ command to add a default gateway,

# route add default gw 192.168.1.1 enOs3

For adding DNS, make an entry in /etc/resolv.conf.

That’s it guys. There are many other uses for nmcli & ifconfig command in linux but examples mentioned here should just get you started. If having any issues/queries, please mention them in the comment box down below.

http://linuxtechlab.com is a beginner friendly website where you can learn Linux Tips & tricks,Scripting, also has lots of tutorials aimed at making Linux easy.