Bash Shell Command to Find or Get IP address

ADVERTISEMENTS



Bash Shell Command to Find or Get IP address

How do I find out my Linux / UNIX system ip address, subnet, and related networking information from a bash shell command prompt? How can I determine my private and public IP addresses from the command line?To find out the IP address of Linux/UNIX/*BSD/macOS and Unixish system, you need to use the command called ifconfig on Unix and the ip command or hostname command on Linux. These commands used to configure the kernel-resident network interfaces and display IP address such as 10.8.0.1 or 192.168.2.254. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.

If no arguments are given to ifconfig command, it displays the status of the currently active interfaces. It shows an Ethernet IP address, Mac address, subnet mask, and other information. Type the following /sbin/ipconfig command to display IP address and releated networking information:

$ /sbin/ifconfig

OR type the following command:

$ /sbin/ifconfig | less

Under Solaris and other Unixish oses you may need to type ifconfig command with -a option as follows:

$ /sbin/ifconfig -a

Sample outputs:

eth0 Link encap:Ethernet HWaddr 00:0F:EA:91:04:07 inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20f:eaff:fe91:407/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:31167 errors:0 dropped:0 overruns:0 frame:0 TX packets:26404 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:38338591 (36.5 MiB) TX bytes:3538152 (3.3 MiB) Interrupt:18 Base address:0xc000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:1994 errors:0 dropped:0 overruns:0 frame:0 TX packets:1994 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:188041 (183.6 KiB) TX bytes:188041 (183.6 KiB)

In the above example, 192.168.1.2 is the IP address of the eth0 Ethernet interface. Of course, NIC (network interface card) name will be different as per your version of Unix system and network card driver. Here is an output from my macOS Unix based desktop:

/sbin/ifconfig

/sbin/ifconfig en0



Displaying private IP addresses of FreeBSD Unix server using the ifconfig

Simply run:

ifconfig

ifconfig -a

ifconfig em0



Finding default routing information on Unix

We need to type the following command:

# netstat -rn

Find the IP address of the local machine on Linux using hostname

We can determine the IP address or addresses of the Linux server by using the hostname command too. Open the Terminal application and execute the hostname command as follows:

# hostname -I

Sample outputs:

192.168.2.25

Linux ip Command

# ip addr show

# ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether b8:ac:6f:65:31:e5 brd ff:ff:ff:ff:ff:ff inet 192.168.2.100/24 brd 192.168.2.255 scope global eth0 inet6 fe80::baac:6fff:fe65:31e5/64 scope link valid_lft forever preferred_lft forever

It is recommend that you use the ip command under Linux based systems. The ip command display information about ip address, manipulate routing, network devices, interfaces, tunnels and much more. The following ip command will show all ip address assigned to your system:To see information about NIC named eth0 ip address, enter:Sample outputs:

Enter the following command to list all network interfaces from bash shell:

# ip link show



Linux command to display default routes

The syntax is as follows:

ip route show

ip r s

route -n

default via 10.8.0.1 dev tun0 proto static metric 50 default via 192.168.2.254 dev enp0s31f6 proto static metric 100 10.8.0.0/24 dev tun0 proto kernel scope link src 10.8.0.2 metric 50 10.83.200.0/24 dev lxdbr0 proto kernel scope link src 10.83.200.1 139.xxx.yyy.zzz via 192.168.2.254 dev enp0s31f6 proto static metric 100 192.168.2.0/24 dev enp0s31f6 proto kernel scope link src 192.168.2.25 metric 100 192.168.2.254 dev enp0s31f6 proto static scope link metric 100

Bash command for showing the public IP address of Linux and Unix systems

We can use the host command or dig command as follows to display the public IP address of your Linux or Unix system:

host myip.opendns.com resolver1.opendns.com

OR

dig +short myip.opendns.com @resolver1.opendns.com

Conclusion

You learned various bash commands to find both private and public IP addresses, netmask, default route, and other information. For more information please see howto read UNIX/Linux system IP address in a shell script.