Introduction
Being knowledgeable about the commands associated with the manipulation of the network stack of the Linux operating system is important for Linux system administrators. Most of the commands that allow users to modify the networking of the system also allow us to obtain information about the servers’ network setup. This information includes mainly IP addresses and routing tables. In this article, we will look at five different ways by which we can display the IP address information about our server.
For the purpose of this demonstration, we will be working on a Centos 7 system.
Method 1: Using ifconfig command
The ifconfig command is the most commonly used command for displaying and modifying IP addresses on the system. The name ifconfig is short for interface configuration. When used without any options or when used with the -a option it displays information about the interfaces available on the system and the IP address set on those interfaces.
[root@linuxnix ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.23.131 netmask 255.255.255.0 broadcast 192.168.23.255 inet6 fe80::830:c411:78b:6bb9 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:7e:5f:4f txqueuelen 1000 (Ethernet) RX packets 810 bytes 80806 (78.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 394 bytes 54734 (53.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
If you are working on a script and want only the IP address to be shown then you can use a quick one liner awk command that we’ve come up with.
[root@linuxnix ~]# ifconfig | awk '/inet 19/ {print $2}' 192.168.23.131 [root@linuxnix ~]#
You can replace the 19 with the network prefix for your network which is commonly 10 for networks in most enterprises.
Method 2: Using ip command
The ifconfig command although still commonly used is considered to be deprecated and the ip command is the one that has taken its place. The ip command, when used with the ‘a’ or ‘addr show’ options, provides the same information as the ifconfig command.
[root@linuxnix ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:7e:5f:4f brd ff:ff:ff:ff:ff:ff inet 192.168.23.131/24 brd 192.168.23.255 scope global dynamic ens33 valid_lft 1473sec preferred_lft 1473sec inet6 fe80::830:c411:78b:6bb9/64 scope link valid_lft forever preferred_lft forever [root@linuxnix ~]# [root@linuxnix ~]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:7e:5f:4f brd ff:ff:ff:ff:ff:ff inet 192.168.23.131/24 brd 192.168.23.255 scope global dynamic ens33 valid_lft 1345sec preferred_lft 1345sec inet6 fe80::830:c411:78b:6bb9/64 scope link valid_lft forever preferred_lft forever [root@linuxnix ~]#
Method 3: Using the hostname command
The hostname command when invoked with the -I option displays IP addresses set on currently active interfaces.
[root@linuxnix ~]# hostname -I 192.168.23.131 [root@linuxnix ~]#
Method 4: using nmcli command
The nmcli command is a command line tool for creating, editing, displaying, deleting, activating and deactivating connections. When used without any options it displays the interfaces active on the system and the IP addresses set on those interfaces.
[root@linuxnix ~]# nmcli ens33: connected to ens33 "Intel 82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single Port Adapter)" ethernet (e1000), 00:0C:29:7E:5F:4F, hw, mtu 1500 inet4 192.168.23.131/24 inet6 fe80::830:c411:78b:6bb9/64 lo: unmanaged loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536 Use "nmcli device show" to get complete information about known devices and "nmcli connection show" to get an overview on active connection profiles. Consult nmcli(1) and nmcli-examples(5) manual pages for complete usage details.
The “nmcli device show” displays more in-depth information about the interfaces.
[root@linuxnix ~]# nmcli device show GENERAL.DEVICE: ens33 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:7E:5F:4F GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: ens33 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 192.168.23.131/24 IP4.GATEWAY: IP4.DNS[1]: 192.168.23.1 IP4.DOMAIN[1]: localdomain IP6.ADDRESS[1]: fe80::830:c411:78b:6bb9/64 IP6.GATEWAY: GENERAL.DEVICE: lo GENERAL.TYPE: loopback GENERAL.HWADDR: 00:00:00:00:00:00 GENERAL.MTU: 65536 GENERAL.STATE: 10 (unmanaged) GENERAL.CONNECTION: -- GENERAL.CON-PATH: -- IP4.ADDRESS[1]: 127.0.0.1/8 IP4.GATEWAY: IP6.ADDRESS[1]: ::1/128 IP6.GATEWAY:
Method 5: Using ip route show command
We can use the ip route show command to display information about interfaces and assigned IP addresses but we need to pipe the output to awk or grep to filter out the required results.
[root@linuxnix ~]# ip route show | awk 'BEGIN {print "IFace", "Address"} /dev/ {print $3" "$9}' | uniq IFace Address ens33 192.168.23.131 [root@linuxnix ~]#
Conclusion
This concludes our demonstration on different commands you could use to find IP address information on your system. We hope that you’ve found this article to be useful and we look forward to your suggestions and feedback.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021