Linux Commands for Viewing IP Addresses

The most commonly used and recommended command to view the IP address in Linux is the <span>ip</span> command. Additionally, this article will introduce some other traditional commands and techniques.

1. Preferred Recommended Command:<span><span>ip addr</span></span>

This is the most recommended command to use in modern Linux distributions, as it is powerful and comprehensive.

Command:

ip addr

Or its shorthand version:

ip a

Output Example:

1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; 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 forever2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:4f:xx:xx brd ff:ff:ff:ff:ff:ff    inet 192.168.1.123/24 brd 192.168.1.255 scope global dynamic noprefixroute ens33       valid_lft 86388sec preferred_lft 86388sec    inet6 fe80::20c:29ff:fe4f:xx/64 scope link noprefixroute       valid_lft forever preferred_lft forever

Output Interpretation:

  • <span>lo</span>: This is the loopback interface, with the IP address typically being <span>127.0.0.1</span>, used for local communication.

  • <span>ens33</span>, <span>eth0</span>, <span>enp0s3</span>: These are the names of your physical or virtual network interfaces (network cards). The names may vary across different systems.

  • <span>inet 192.168.1.123/24</span>: This line shows the IPv4 address. <span>192.168.1.123</span> is the address, and <span>/24</span> is the subnet mask.

  • <span>inet6 fe80::...</span>: This line represents the IPv6 link-local address. Addresses starting with <span>fe80:</span> are typically used for local area networks.

  • <span>link/ether 00:0c:29:4f:xx:xx</span>: This is the MAC address of the network card.

If you want to view information for a specific network card (e.g., <span>ens33</span>), you can do it like this:

ip addr show dev ens33

2. Traditional Command:<span><span>ifconfig</span></span> (gradually being phased out)

<span>ifconfig</span> is a very traditional command that is often used in older tutorials. Most minimal installations of Linux systems may not have this command by default, as it belongs to the <span>net-tools</span> package, which has been replaced by <span>iproute2</span> (i.e., the <span>ip</span> command).

If your system has <span>ifconfig</span>, you can run it directly:

ifconfig

Output Example:

ens33: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500        inet 192.168.1.123  netmask 255.255.255.0  broadcast 192.168.1.255        inet6 fe80::20c:29ff:fe4f:xx  prefixlen 64  scopeid 0x20&lt;link&gt;        ether 00:0c:29:4f:xx:xx  txqueuelen 1000  (Ethernet)        RX packets 142107  bytes 198267405 (198.2 MB)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 31070  bytes 2318362 (2.3 MB)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73&lt;UP,LOOPBACK,RUNNING&gt;  mtu 65536        inet 127.0.0.1  netmask 255.0.0.0        inet6 ::1  prefixlen 128  scopeid 0x10&lt;host&gt;        loop  txqueuelen 1000  (Local Loopback)        RX packets 202  bytes 17516 (17.5 KB)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 202  bytes 17516 (17.5 KB)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

If the system prompts <span>command not found: ifconfig</span>, you can install it using the package manager:

  • Ubuntu/Debian: <span>sudo apt install net-tools</span>

  • CentOS/RHEL/Fedora: <span>sudo yum install net-tools</span> or <span>sudo dnf install net-tools</span>

3. View Gateway (Router) IP and Routing Table

ip route

Or

route -n  # also requires net-tools

In the output, the <span>default via 192.168.1.1</span> indicates your gateway address.4. <span><span>hostname</span></span> command<span>hostname</span> command with the <span>-I</span> (uppercase i) option can display all IP addresses of all network interfaces in a very concise manner.

hostname -I

Output (IP addresses separated by spaces)::

192.168.1.123 172.17.0.1

5. Using <span><span>nmcli</span></span> (NetworkManager Command Line Tool)If your system uses NetworkManager to manage networks (common in desktop versions), this command is very useful.

nmcli device show

Or to view IP more concisely:

nmcli -p device show

6. View Public IP AddressTo view your external public (WAN) IP, you need to query an external service, and the system must have the <span>curl</span> tool installed.

curl ifconfig.me

Or

curl icanhazip.com

Summary and Recommendations

Command Description Recommendation Level
<span>ip addr</span> or <span>ip a</span> Modern Standard, most comprehensive, available by default on all new systems ★★★★★ (Preferred)
<span>hostname -I</span> Quick displays only IP addresses, suitable for use in scripts ★★★★☆
ifconfig Traditional command, may require additional installation, gradually being phased out ★★☆☆☆
ip route View gateway and routing information (Special Purpose)
curl ifconfig.me View public IP address (Special Purpose)

Leave a Comment