How many commands you know to get IP address on your Linux systems? In this article, we’ll discuss a few ways to get IP address as well as public IP on Linux.
- ifconfig command
- ip addr
- Hostname -I command
- nmcli command
- ip route
- Graphical Method
- Display Public IP address
1) ifconfig command
ifconfig is short for interface configuration. It’s a widely used command line tool used for configuration & viewing of ip addresses, and enabling/disabling network interfaces.
Note: ifconfig command is deprecated, try using ip command.
Read Also: How to use IP Command in Linux with Examples
ifconfig / ifconfig -a
ifconfig with no arguments will display statistics of all active interfaces. This is the same as running ifconfig -a
Example
ifconfig
Output
eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether a0:2b:b8:22:0c:6b 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
lo: flags=73<UP,LOOPBACK,RUNNING> 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 28212 bytes 8319418 (7.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 28212 bytes 8319418 (7.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.43.156 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::9ed2:1eff:fe31:3fb9 prefixlen 64 scopeid 0x20
ether 9c:d2:1e:31:3f:b9 txqueuelen 1000 (Ethernet)
RX packets 88974 bytes 98475476 (93.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 59034 bytes 7601548 (7.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig
To get an IP address of a specific network interface, run ifconfig followed by an interface name
Example
ifconfig eth0
Output
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.43.156 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::9ed2:1eff:fe31:3fb9 prefixlen 64 scopeid 0x20
ether 9c:d2:1e:31:3f:b9 txqueuelen 1000 (Ethernet)
RX packets 92374 bytes 98997644 (94.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 62046 bytes 8082927 (7.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2) ip addr
ip addr is yet another command you can use to get an IP address of a system and other interface statistics
Example
ip addr
Output
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether a0:2b:b8:22:0c:6b brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 9c:d2:1e:31:3f:b9 brd ff:ff:ff:ff:ff:ff
inet 192.168.43.156/24 brd 192.168.43.255 scope global dynamic noprefixroute wlan0
valid_lft 3489sec preferred_lft 3489sec
inet6 fe80::9ed2:1eff:fe31:3fb9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3) Hostname -I command
To get the IP address of active network connections, run
hostname -I
Output
192.168.43.156
4) nmcli command
nmcli – Network Manager command Line interface – is a command line tool used for creating, editing, displaying, deleting, activating and deactivating connections.
nmcli
Example
nmcli
Output
wlan0: connected to TECNO Camon CX
“Realtek RTL8188EE”
wifi (rtl8188ee), 9C:D2:1E:31:3F:B9, hw, mtu 1500
ip4 default
inet4 192.168.43.156/24
route4 0.0.0.0/0
route4 192.168.43.0/24
inet6 fe80::9ed2:1eff:fe31:3fb9/64
route6 fe80::/64
route6 ff00::/8
eth0: unavailable
“Realtek RTL810xE”
ethernet (r8169), A0:2B:B8:22:0C:6B, hw, mtu 1500
lo: unmanaged
“lo”
loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536
DNS configuration:
servers: 192.168.43.1
interface: wlan0
Use “nmcli device show” to get complete information about known devices and
“nmcli connection show” to get an overview on active connection profiles.
nmcli device show
To get the IP address and even more detailed statistics, run
nmcli device show
Output
GENERAL.DEVICE: wlan0
GENERAL.TYPE: wifi
GENERAL.HWADDR: 9C:D2:1E:31:3F:B9
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: TECNO Camon CX
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
IP4.ADDRESS[1]: 192.168.43.156/24
IP4.GATEWAY: 192.168.43.1
IP4.ROUTE[1]: dst = 0.0.0.0/0, nh = 192.168.43.1, mt = 600
IP4.ROUTE[2]: dst = 192.168.43.0/24, nh = 0.0.0.0, mt = 600
IP4.DNS[1]: 192.168.43.1
IP6.ADDRESS[1]: fe80::9ed2:1eff:fe31:3fb9/64
IP6.GATEWAY: –
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 600
IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255
GENERAL.DEVICE: eth0
GENERAL.TYPE: ethernet
GENERAL.HWADDR: A0:2B:B8:22:0C:6B
GENERAL.MTU: 1500
–
5) ip route get 1.2.3.4 | awk ”
This is yet another command that can be used to get an IP address without much statistics
ip route get 1.2.3.4 | awk ”
Output
192.168.43.156
6) Network Settings in Graphical User Interface
If you are running Linux in a graphical environment, you can easily navigate to the Network settings and check out the IP address. In this example, we have demonstrated this using Ubuntu 18.04 GNOME interface.
- Click on the Network icon as shown.
- On the pull-down menu that appears, be sure to locate the connected network interface. In this case, it’s the wired connection and we can see that the interface is up by ‘Wired connected’ status. Click on it.
- Next, click on the ‘Wired settings’ option that appears below
- This displayed a ‘Network Settings’ window. Click on the gear icon next to the ON/OFF toggle and a pop-up Window will appear showing the IP details of the connected network interface.
Display a Public IP address of your Linux System
If you wish to get the Public IP of your Linux System, the following commands come in handy
curl ipinfo.io/ip
curl ifconfig.me
curl bot.whatismyipaddress.com
dig +short myip.opendns.com @resolver1.opendns.com
curl checkip.dyndns.org
curl ident.me
host myip.opendns.com resolver1.opendns.com
curl ipecho.net/plain
Do you know any other method to get the IP address? Please reply with your comments.