How to Check the IP Address on Ubuntu

1. What is an IP Address?

An IP address is a unique number used to identify a device on a network. It is essential for sending and receiving data over the internet or a local network. There are two main types of IP addresses: IPv4 and IPv6. IPv4 uses a 32-bit address format, such as “192.168.0.1”, while IPv6 uses a 128-bit format, such as “2001:0db8:85a3:0000:0000:8a2e:0370:7334”. IPv6 was introduced to expand address space and support more devices.

2. Basic Commands to Check IP Address on Ubuntu

Ubuntu provides various commands to check your IP address. The following commands allow you to easily find the IP address assigned to your system.

2.1 ip addr show Command

The ip addr show command is a powerful and recommended tool for displaying IPv4 and IPv6 addresses assigned to network interfaces in modern Linux distributions.

Example Usage:

$ sudo ip addr show

Example Output:

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic ens33
       valid_lft 86381sec preferred_lft 86381sec
    inet6 fe80::250:56ff:fe9a:de91/64 scope link 
       valid_lft forever preferred_lft forever
  • The value after inet, such as “192.168.1.10/24”, represents the IPv4 address. The “/24” is CIDR notation, indicating the subnet mask (equivalent to 255.255.255.0). The first 24 bits define the network, while the remaining 8 bits represent the host.
  • The value after inet6, such as “fe80::250:56ff:fe9a:de91”, represents the IPv6 address.

Additional Explanation:

  • brd indicates the broadcast address.
  • scope defines the address scope: global means the address is accessible worldwide, while link means it is limited to the local network segment.

2.2 hostname -I Command

The hostname -I command displays all IP addresses assigned to the system, separated by spaces. It is useful when you need a simple and direct way to retrieve IP addresses.

Example Usage:

$ hostname -I

Example Output:

192.168.1.10 fe80::250:56ff:fe9a:de91
  • The first value is the IPv4 address, while the second value is the IPv6 address. This command does not provide detailed information, only listing assigned IP addresses.

Checking the IP Address of the Default Interface:

  • To obtain the IP address of the default network interface, use the following command:
$ ip route get 1.1.1.1

This command displays the routing information for the specified address, including the default interface.

2.3 curl ifconfig.me Command

The curl ifconfig.me command retrieves the public IP address by accessing an external service. It is useful when you need to check your external IP address, but note that an internet connection is required, and privacy considerations should be taken into account.

Example Usage:

$ curl ifconfig.me

Example Output:

203.0.113.50

This output represents your global IP address as seen from the internet.

Privacy Considerations:

  • The curl ifconfig.me command sends IP information to an external server. If privacy is a concern, consider checking your IP address via your router’s settings instead.

Public vs. Private IP Addresses:

  • The ip addr show command displays private IP addresses used within local networks. The curl ifconfig.me command returns the public IP address used for internet access. In many cases, Network Address Translation (NAT) causes internal and external IP addresses to differ. NAT allows multiple devices to share a single public IP address for internet access.
侍エンジニア塾

3. The Deprecated ifconfig Command and Its Alternatives

The ifconfig command was traditionally used for network management in Linux but is now deprecated and not included by default in newer distributions. Instead, the more powerful ip command is recommended.

Installing ifconfig:

$ sudo apt install net-tools

Example Usage:

$ sudo ifconfig

Example Output:

inet 192.168.1.10  netmask 255.255.255.0  broadcast 192.168.1.255
  • The value after “inet”, such as “192.168.1.10”, is the IPv4 address.

Limitations of ifconfig:

  • ifconfig may not display all network interfaces, particularly virtual interfaces or IPv6 addresses. The ip command provides more comprehensive and up-to-date network information.

4. Using Network Manager

4.1 nmcli Command

The nmcli command is a command-line tool for controlling NetworkManager, which can provide detailed information about network devices. If nmcli is not installed, you can install it using the following command:

Installation:

$ sudo apt install network-manager

Example Usage:

$ nmcli device show

Example Output:

IP4.ADDRESS[1]:                         192.168.1.10/24
  • The value after “IP4.ADDRESS[1]” represents the IPv4 address assigned to the device.

Checking NetworkManager Status:

  • To verify whether NetworkManager is running, use the following command:
$ systemctl status NetworkManager

5. Checking IP Address in Different Scenarios

Knowing how to check your IP address is useful in various scenarios, such as troubleshooting network issues, configuring servers, and preparing for remote access. Understanding multiple commands allows you to choose the best method based on your network environment and situation.

Troubleshooting Tips:

  • Network Issues: If the IP address is not correctly assigned, you may be unable to connect to the network. Use ip addr show to check the settings and ensure that the correct interface has an assigned IP address. In some cases, you may need to use sudo to modify network settings.
  • Remote Access Setup: When setting up remote access to a server, knowing the correct public IP address is essential. Use curl ifconfig.me to check your public IP and ensure that your router’s port forwarding settings are configured correctly.
  • Connection Testing: To verify network connectivity, use the ping command to check if you can reach a specific host. For example, running ping google.com can test if your internet connection is working. Additionally, the traceroute command can be used to analyze the path packets take through the network and identify where delays or issues occur.
$ ping google.com
$ traceroute google.com
  • Ping Output: Displays response times and packet loss information. If there is no response, it may indicate a connectivity issue.
  • Traceroute Output: Shows the path packets take through different network hops and helps identify where delays occur.

6. Conclusion

There are multiple ways to check the IP address on Ubuntu, each with its advantages. Choosing the right command depends on your specific needs.

  • ip addr show provides detailed network information and is widely recommended.
  • hostname -I is a simple way to retrieve just the IP addresses.
  • curl ifconfig.me is useful for checking the public IP address but requires an internet connection and raises privacy considerations.
  • ifconfig is deprecated but can still be used in certain cases if installed.

Mastering these commands will improve your efficiency in managing networks on Ubuntu. When troubleshooting network issues, selecting the appropriate command can help you quickly diagnose problems and find solutions. Additionally, understanding concepts like Network Address Translation (NAT) and IP address assignment will deepen your knowledge of network configurations and security.

Explore official documentation and other resources to further enhance your network management skills.

Reference Resources: