Determine Your Private and Public IP Addresses from the Command Line | Linux Training Academy (2024)

You can determine the IP address or addresses of your Linux system by using the hostname, ifconfig, or ip commands. To display the IP addresses using the hostname command, use the -I option. In this example the IP address is 192.168.122.236.

$ hostname -I192.168.122.236

The ifconfig command can also be used to display the IP addresses being used by the system. By default, ifconfig will display information on all the network interfaces that are currently up, including the loopback interface.

$ /sbin/ifconfigeth0 Link encap:Ethernet HWaddr 52:54:00:43:84:6c inet addr:192.168.122.236 Bcast:192.168.122.255 Mask:255.255.255.0 inet6 addr: fe80::5054:ff:fe43:846c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:304863 errors:0 dropped:0 overruns:0 frame:0 TX packets:129646 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:53349734 (53.3 MB) TX bytes:130802964 (130.8 MB)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:2059079 errors:0 dropped:0 overruns:0 frame:0 TX packets:2059079 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:383294965 (383.2 MB) TX bytes:383294965 (383.2 MB)

To gather information on just one interface, supply that interface as an argument to ifconfig. You can even take this a step further and extract the line that contains the IP address or even extract the IP address itself.

$ /sbin/ifconfig eth0 | grep -i mask inet addr:192.168.122.236 Bcast:192.168.122.255 Mask:255.255.255.0$ /sbin/ifconfig eth0 | grep -i mask | awk '{print $2}'| cut -f2 -d:192.168.122.236

Let’s do the same thing with the ip command. To view IP addresses, use ip addr show. Like ifconfig, you can specify an interface.

$ ip addr show1: lo: <loopback,up,lower_up> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:43:84:6c brd ff:ff:ff:ff:ff:ff inet 192.168.122.236/24 brd 192.168.122.255 scope global eth0 inet6 fe80::5054:ff:fe43:846c/64 scope link valid_lft forever preferred_lft forever$ ip addr show eth02: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:43:84:6c brd ff:ff:ff:ff:ff:ff inet 192.168.122.236/24 brd 192.168.122.255 scope global eth0 inet6 fe80::5054:ff:fe43:846c/64 scope link valid_lft forever preferred_lft forever$ ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -f1 -d'/'192.168.122.236</broadcast,multicast,up,lower_up></broadcast,multicast,up,lower_up></loopback,up,lower_up>

If you want to know the public IP address of a Linux server, you can send an HTTP request to one of the following web servers.

  • http://ifconfig.me
  • http://www.icanhazip.com
  • http://ipecho.net/plain
  • http://ident.me
  • https://myip.dnsomatic.com
  • https://checkip.amazonaws.com
  • http://whatismyip.akamai.com
  • https://myipv4.p1.opendns.com/get_my_ip

If the Linux system is connected directly to the internet, the public and private IP addresses will be the same. However, in most cases they differ. Here is an example using the curl command.

$ curl ifconfig.me216.239.32.10$ curl icanhazip.com216.239.32.10$ curl ipecho.net/plain216.239.32.10$ curl ident.me216.239.32.10$ curl https://myip.dnsomatic.com216.239.32.10$ curl https://checkip.amazonaws.com216.239.32.10$ curl http://whatismyip.akamai.com216.239.32.10

You can also achieve the same result with wget by enabling quiet mode with -q and sending the output to standard output (STDOUT) with -O-.

$ wget -qO- ifconfig.me216.239.32.10

There’s yet another way to determine your public IP address. Perform a DNS lookup against myip.opendns.com. It’s a service provided by OpenDNS and it’s lightening fast!

$ dig +short myip.opendns.com @resolver1.opendns.com216.239.32.10

Here are some similarly fast alternatives:

$ dig +short ANY whoami.akamai.net @ns1-1.akamaitech.net216.239.32.10
$ dig +short ANY o-o.myaddr.l.google.com @ns1.google.com216.239.32.10
Determine Your Private and Public IP Addresses from the Command Line | Linux Training Academy (1)
Determine Your Private and Public IP Addresses from the Command Line | Linux Training Academy (2024)
Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5619

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.