There are many ways to get the IP address of your system from the command line in Linux. Here we will outline 4 different ways to get your systems IP address from the command line.

This quick tip was written to teach you how to easily get the IP address configured on your system. The output from the below commands can be either a public or private IP address depending on your configuration. If you are unsure of the difference, you should read "What is the difference between public and private IP address?".

If you are interested in finding ONLY your public IP address you should read "How to Find Your Public IP Address on Linux Command Line".

Using the Hostname Command to Show IP Address

The hostname command gives the cleanest output. Using the -i (--ip-address) option will output just the ip address assigned to the host name.

$ hostname -i
192.168.122.58

This is especially useful in scripting since you do not have to do any output manipulation to isolate the IP address.

Using the ifconfig Command to Show IP Address

The ifconfig command allows you to view and configure your network interfaces from the command line. If you call ifconfig without any options or arguments it will display network interfaces that are currently up.

$ ifconfig
eth0: flags=4163 mtu 1500
inet 192.168.122.58 netmask 255.255.255.0 broadcast 192.168.122.255
inet6 fe80::b3fd:6315:8a16:7f1a prefixlen 64 scopeid 0x20

ether 52:54:00:d9:84:ac txqueuelen 1000 (Ethernet)
RX packets 5457 bytes 430563 (420.4 KiB)
RX errors 0 dropped 10 overruns 0 frame 0
TX packets 2656 bytes 388389 (379.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 32 bytes 2592 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2592 (2.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
inet 192.168.124.1 netmask 255.255.255.0 broadcast 192.168.124.255
ether 52:54:00:84:60:61 txqueuelen 1000 (Ethernet)
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

You can specify the name of the interface to only show information for that specific device only, including it's IP address

$ ifconfig eth0
eth0: flags=4163 mtu 1500
inet 192.168.122.58 netmask 255.255.255.0 broadcast 192.168.122.255
inet6 fe80::b3fd:6315:8a16:7f1a prefixlen 64 scopeid 0x20

ether 52:54:00:d9:84:ac txqueuelen 1000 (Ethernet)
RX packets 4509 bytes 357129 (348.7 KiB)
RX errors 0 dropped 10 overruns 0 frame 0
TX packets 2209 bytes 312627 (305.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

To pull just the IP address, you need to do some output manipulation. Here we use grep to isolate the line with the IP address, then awk to show only that field.

$ ifconfig eth0 | grep -w inet | awk -F' ' '{print $2}'
192.168.122.58

Using the ip Command to Show IP Address

The ip command is a powerful tool to show and configure routing, IP information, policy based routing, tunnels and more. If you follow the ip command with the addr object, you will get the information on all your interfaces, similar to the ifconfig command.

$ ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:d9:84:ac brd ff:ff:ff:ff:ff:ff
inet 192.168.122.58/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
valid_lft 3044sec preferred_lft 3044sec
inet6 fe80::b3fd:6315:8a16:7f1a/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:84:60:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.124.1/24 brd 192.168.124.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:84:60:61 brd ff:ff:ff:ff:ff:ff

You can specify the interface using the show dev arguments and then the name of the device. Here we use this technique to show the information for the eth0 interface.

$ ip addr show dev eth0
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:d9:84:ac brd ff:ff:ff:ff:ff:ff
inet 192.168.122.58/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
valid_lft 2361sec preferred_lft 2361sec
inet6 fe80::b3fd:6315:8a16:7f1a/64 scope link noprefixroute
valid_lft forever preferred_lft forever

This output has a lot of information. If you want to show only the IP address you need to do output manipulation.

$ ip addr show dev eth0 | grep -w inet | awk -F' ' '{print $2}' | cut -d/ -f1
192.168.122.58

Using nmcli Command to Show IP Address

The nmcli (Network Manager Command Line Interface) command is a tool for controlling NetworkManager.

nmcli is used to create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status.

-nmcli man page

This is another powerful tool that outputs a lot of information. If you call nmcli without any options or arguments, it will print information on your network connections.

$ nmcli
eth0: connected to eth0
"Red Hat Virtio"
ethernet (virtio_net), 52:54:00:D9:84:AC, hw, mtu 1500
ip4 default
inet4 192.168.122.58/24
route4 0.0.0.0/0
route4 192.168.122.0/24
inet6 fe80::b3fd:6315:8a16:7f1a/64
route6 fe80::/64
route6 ff00::/8
virbr0: connected to virbr0
"virbr0"
bridge, 52:54:00:84:60:61, sw, mtu 1500
inet4 192.168.124.1/24
route4 192.168.124.0/24
lo: unmanaged
"lo"
loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536
virbr0-nic: unmanaged
"virbr0-nic"
tun, 52:54:00:84:60:61, sw, mtu 1500
DNS configuration:
servers: 192.168.122.1
interface: eth0

Using the connection show argument followed by a interface name we can narrow the information to a specific device. This outputs a TON of information about the specified interface (131 lines to be exact).

$ nmcli connection show eth0
connection.id: eth0
connection.uuid: 286466a9-3e21-41b8-8d3e-1e1ce2dac600
connection.stable-id: --
connection.type: 802-3-ethernet
connection.interface-name: eth0
connection.autoconnect: yes
connection.autoconnect-priority: 0
connection.autoconnect-retries: -1 (default)
...OUTPUT TRUNCATED...

You can use grep to pull just the IP address adress like so:

nmcli connection show eth0 | grep IP4.ADDRESS
$ nmcli connection show eth0 | grep IP4.ADDRESS
IP4.ADDRESS[1]: 192.168.122.58/24

You can further refine the output to the IP address only like so:

$ nmcli connection show eth0 | grep IP4.ADDRESS | awk '{print $2}' | cut -d/ -f1
192.168.122.58

Conclusion

There are quite a few ways to get the IP address information for your system. If you are looking solely for the IP address, I recommend the hostname -i command. This gives the cleanest output and shows you only the information you are looking for. If you need more information like the subnet mask or gateway address, then some of the other options will fit your needs.

Resources