Manage IP Addressing in Linux

CompTIABeginner
Practice Now

Introduction

In this lab, you will learn the fundamental skills for managing IP addressing in a Linux environment. You will start by using the modern ip command to inspect your system's network interfaces. Following this, you will manually configure a static IP address, set a default gateway to enable communication with external networks, and then verify your connection using the ping utility.

Next, you will explore dynamic IP configuration by releasing the static address and using the dhclient command to automatically obtain a new IP address from a DHCP server. To conclude the lab, you will use the arp and traceroute commands to further inspect and verify your network configuration, ensuring all components are working correctly.

Inspect Network Interfaces with ip a

In this step, you will learn how to inspect the network interfaces on your Linux system. Before you can configure an IP address, you must first identify the name of the network interface you want to configure. Every connection, whether wired or wireless, is managed through a network interface.

The modern and standard command for viewing and manipulating network interfaces in Linux is ip. We will use it with the a (or address) argument to display all network interfaces and their assigned IP addresses.

  1. Open your terminal. You should already be in the ~/project directory.

  2. To list all network interfaces and their current configurations, run the following command:

    ip a

    You will see an output similar to the following. The exact details, such as interface names (eth0, enp0s5), IP addresses, and MAC addresses, will vary each time you start a new lab environment.

    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
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 00:16:3e:0f:23:a5 brd ff:ff:ff:ff:ff:ff
        altname enp0s5
        inet 172.16.50.11/24 metric 100 brd 172.16.50.255 scope global dynamic eth0
           valid_lft 1892159972sec preferred_lft 1892159972sec
    3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
        link/ether 02:42:bb:cb:56:62 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
           valid_lft forever preferred_lft forever

Understanding the Output:

  • 1: lo: This is the loopback interface, a virtual interface the system uses to communicate with itself. It always has the IP address 127.0.0.1.
  • 2: eth0: This is your primary Ethernet (wired) interface. Your interface might have a different name or show altname (alternative names) like enp0s5. This is the interface we will be working with.
  • 3: docker0: You might see other interfaces like docker0, which is used by the Docker container engine. You can ignore this for the purposes of this lab.
  • inet 172.16.50.11/24: This is the IPv4 address assigned to your interface. The address you see will be different. This is the address that allows you to connect to this remote lab.
  • link/ether ...: This is the MAC address of the interface, a unique hardware identifier.

Now that you can identify your primary network interface (eth0), you are ready to learn how to configure it.

Configure a Secondary Static IP Address

In this step, you will manually assign an additional, static IP address to your network interface. A static IP is useful for servers that need to be consistently reachable at the same address.

Important Note: Because you are in a remote lab session, you must not remove the existing IP address or take the network interface down. Doing so would sever your connection. We will instead add a second IP address to the interface, which is a common and safe practice.

  1. Assign the new static IP address 192.168.1.10 with a /24 subnet mask to the eth0 interface. If your interface has a different name, use that name instead.

    sudo ip addr add 192.168.1.10/24 dev eth0

    This command adds the new address without affecting the existing one. It produces no output if successful.

  2. Finally, verify that the new IP address has been assigned. Use the ip a command again, specifying the eth0 interface.

    ip a show eth0

    You should now see both the original dynamic IP address and the new static IP address listed for the eth0 interface.

    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 00:16:3e:0f:23:a5 brd ff:ff:ff:ff:ff:ff
        altname enp0s5
        inet 172.16.50.11/24 metric 100 brd 172.16.50.255 scope global dynamic eth0
           valid_lft 1892159595sec preferred_lft 1892159595sec
        inet 192.168.1.10/24 scope global eth0
           valid_lft forever preferred_lft forever
        inet6 fe80::216:3eff:fe0f:23a5/64 scope link
           valid_lft forever preferred_lft forever

    Notice that a second inet field now shows 192.168.1.10/24. You have successfully added a temporary static IP address while keeping your connection alive.

Set a Default Gateway and Verify Connectivity with ping

In this step, you will set a default gateway and test network connectivity. The default gateway is the router your system sends traffic to when the destination is on another network (like the internet).

Your system already has a default gateway provided by the lab environment. Changing or removing it would break your connection. To safely practice this command, we will add a new, second default route with a higher metric. A higher metric means it has a lower priority, so the system will not use it for real traffic, keeping our connection safe.

  1. Let's add a default gateway route for the 192.168.1.0/24 network. We'll set the gateway's IP to 192.168.1.1 and give it a metric of 200.

    sudo ip route add default via 192.168.1.1 dev eth0 metric 200
    • default: Specifies this is the default route.
    • via 192.168.1.1: The IP of the gateway router.
    • dev eth0: The route is accessible via the eth0 interface.
    • metric 200: Makes this route less preferred than the primary, existing route (which has a lower metric).
  2. Verify the route has been added by displaying the routing table.

    ip route

    The output will be complex and vary, but it should now include two lines starting with default. Your original gateway will have a lower metric (e.g., 100) and our new one will have a metric of 200.

    default via 172.16.50.253 dev eth0 proto dhcp src 172.16.50.11 metric 100
    default via 192.168.1.1 dev eth0 metric 200
    172.16.50.0/24 dev eth0 proto kernel scope link src 172.16.50.11 metric 100
    192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
    ... (other routes may be present)
  3. Now, let's test connectivity with ping. It sends an ICMP "echo request" to a host to see if it's reachable. Let's try to ping the gateway we just configured for our imaginary network.

    ping -c 3 192.168.1.1

    Expected Outcome: This command is expected to fail. Since we added the IP 192.168.1.10/24 to eth0, the system assumes 192.168.1.1 is on the same local network. It will try to reach it directly, but no device with that address exists, so you will see "Destination Host Unreachable".

    PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
    From 192.168.1.10 icmp_seq=1 Destination Host Unreachable
    From 192.168.1.10 icmp_seq=2 Destination Host Unreachable
    From 192.168.1.10 icmp_seq=3 Destination Host Unreachable
    
    --- 192.168.1.1 ping statistics ---
    3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2039ms

    This is the correct and expected outcome. You have learned how to add a default route and use ping for testing.

Revert to a Dynamic IP Address using dhclient

In this step, you will remove the static configurations you created and then use dhclient to refresh the dynamic IP lease from the DHCP server.

First, the dhclient tool, which is part of the isc-dhcp-client package, may not be installed by default. Let's ensure it's available.

  1. Update your system's package list and install the DHCP client:

    sudo apt-get update
    sudo apt-get install -y isc-dhcp-client

    Now, let's safely remove the static IP and route we added in the previous steps without disrupting your connection.

  2. Delete the static IP address from eth0:

    sudo ip addr del 192.168.1.10/24 dev eth0
  3. Delete the static default route we added:

    sudo ip route del default via 192.168.1.1 dev eth0

    Now your network configuration is back to its original state. To demonstrate dhclient, let's request a refresh of the IP address from the DHCP server.

  4. First, release the current DHCP lease.

    sudo dhclient -r eth0
  5. Next, request a new lease from the DHCP server.

    sudo dhclient eth0

    You may see the message RTNETLINK answers: File exists. This is normal. It simply means dhclient is trying to add routes that are already present, so no changes are needed. Your connection will remain stable.

  6. Verify your configuration by viewing the eth0 interface again.

    ip a show eth0

    The output should now show only your original dynamic IP address, likely in the 172.16.x.x or 172.17.x.x range. The static IP 192.168.1.10 should be gone.

    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 00:16:3e:0f:23:a5 brd ff:ff:ff:ff:ff:ff
        altname enp0s5
        inet 172.16.50.11/24 metric 100 brd 172.16.50.255 scope global dynamic eth0
           valid_lft 1892159460sec preferred_lft 1892159460sec
        inet6 fe80::216:3eff:fe0f:23a5/64 scope link
           valid_lft forever preferred_lft forever

    You have successfully cleaned up your static configuration and refreshed your dynamic IP lease.

Verify Network Configuration with arp and traceroute

In this final step, you will learn to use arp and traceroute to inspect your network configuration.

First, let's ensure the necessary tools are installed. arp is in net-tools, and traceroute is in its own package.

  1. Install net-tools and traceroute:

    sudo apt-get update
    sudo apt-get install -y net-tools traceroute

Using arp to View the ARP Cache

The Address Resolution Protocol (ARP) maps an IP address to a physical MAC address on a local network. The results are stored in the ARP cache.

  1. To populate your ARP cache, first find your default gateway's IP from the routing table.

    ip route | grep default

    The output will show you the gateway IP. Note that the IP address will vary.

    default via 172.16.50.253 dev eth0
    default via 172.16.50.253 dev eth0 proto dhcp src 172.16.50.11 metric 100
  2. Now, ping that gateway IP (e.g., 172.16.50.253 in the example above) to generate traffic.

    ping -c 1 <your-gateway-ip>
  3. Display the ARP cache using arp -a.

    arp -a

    You will see the IP and MAC address of your gateway. You may also see an <incomplete> entry for the 192.168.1.1 address we tried to ping earlier, which is normal.

    ? (192.168.1.1) at <incomplete> on eth0
    _gateway (172.16.50.253) at ee:ff:ff:ff:ff:ff [ether] on eth0

    This confirms your system resolved the gateway's IP to a MAC address.

Using traceroute to Trace Network Paths

The traceroute command shows the sequence of routers ("hops") packets take to reach a destination.

  1. Let's trace the route to a public domain like google.com.

    traceroute google.com

    The command will print the path. Note: In many cloud environments, the first hop shown by traceroute may be an internal IP address and not the same as the default gateway in your routing table. This is normal behavior.

    traceroute to google.com (142.250.189.174), 30 hops max, 60 byte packets
     1  10.220.9.2 (10.220.9.2)  0.345 ms  ...
     2  11.73.5.1 (11.73.5.1)  1.978 ms ...
     3  ...
     8  sfo03s24-in-f14.1e100.net (142.250.189.174)  3.001 ms ...
    • The first hop is a router inside the lab provider's network.
    • Subsequent hops show the path across the internet to the final destination.
    • Asterisks (* * *) can appear if a router along the path does not respond to traceroute probes, which could be due to a firewall or other network policy.

Congratulations! You have now learned how to inspect, statically configure, and dynamically configure IP addressing on a Linux system, as well as use key tools like ping, arp, and traceroute to verify and troubleshoot your network.

Summary

In this lab, you learned the fundamental skills for managing IP addressing in a Linux environment. You started by inspecting network interfaces and their current configurations using the ip a command to identify target interfaces like eth0. You then practiced manually assigning a static IP address and subnet mask with ip addr add, followed by setting a default gateway using ip route add to enable communication with external networks. To contrast with manual configuration, you also learned how to automatically obtain an IP address, subnet mask, and gateway from a DHCP server using the dhclient command.

Furthermore, you explored essential network verification and troubleshooting tools. You used the ping command to confirm basic network connectivity to both the default gateway and external hosts. To gain deeper insight into the network layer, you examined the Address Resolution Protocol (ARP) cache with the arp command to see IP-to-MAC address mappings. Finally, you utilized traceroute to trace the network path hop-by-hop to a destination, verifying your routing configuration.