The cart is empty

In today's networking scenarios, it's common to need to alter network settings on operating systems like Ubuntu due to various requirements such as network configuration, security, or troubleshooting needs. This article focuses on the process of changing the IP address, gateway, and network configuration on the Ubuntu operating system. We'll cover both static and dynamic IP address allocation using the netplan tool, which is the default network configuration management tool in Ubuntu since version 17.10.

Prerequisites

Before making changes to the network configuration, ensure you have root access or a user with sudo privileges, and you know which network interface you want to configure (e.g., eth0, wlan0).

Finding the Network Interface Name

To find available network interfaces on your system, use the command:

ip link show

Identify the interface you wish to configure, such as eth0 for Ethernet or wlan0 for wireless.

Changing IP Address, Gateway, and Network Configuration

Ubuntu uses netplan for network configuration, which replaced the older ifupdown system. Netplan configuration files are located in /etc/netplan/ and are in YAML format. Making changes requires editing or creating a file in this directory.

1. Finding the Configuration File

You should typically find one or more configuration files in /etc/netplan/. The filename may vary; common ones include 01-netcfg.yaml, 50-cloud-init.yaml, etc. If you're unsure which file to edit, it's good to check all files in this directory.

2. Editing the Configuration File

Open the configuration file in a text editor, such as sudo nano /etc/netplan/01-netcfg.yaml. An example configuration file for a static IP address looks like this:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses: [192.168.1.10/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

This example sets a static IP address 192.168.1.10 with a /24 subnet mask (equivalent to subnet mask 255.255.255.0) on the eth0 interface, with gateway 192.168.1.1, and DNS servers 8.8.8.8 and 8.8.4.4.

3. Applying Changes

After saving changes in the configuration file, apply the changes with:

sudo netplan apply

This command instructs netplan to read the new configuration and apply it to the system.

4. Checking Configuration

To verify that the IP address and other network settings were correctly applied, you can use the command ip addr show, which displays the current settings of network interfaces, including assigned IP addresses. Use ip addr show dev eth0 to display settings for a specific interface, replacing eth0 with your network interface name.

If you need to verify the functionality of the gateway, you can use the command ip route or the more specific ip route show default, which shows the default gateway of the system. The response should contain a record of the default gateway with the IP address you set.

Dynamic IP Address Allocation

If you prefer dynamic IP address allocation using DHCP, you can edit your configuration file to have the interface use DHCP to obtain IP address, subnet mask, gateway, and DNS servers automatically. An example configuration file for DHCP:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes

After applying changes with sudo netplan apply, the eth0 interface will automatically acquire network settings from the DHCP server.

Troubleshooting

If you encounter issues after changing the network configuration, check the following:

  • YAML Syntax in /etc/netplan/: YAML is sensitive to indentation, so ensure correct indentation and formatting.
  • Interface Status: Use ip link show to verify that the network interface is active (state UP).
  • System Logs: Examine system logs for any errors or warnings related to network configuration.

If networking issues persist even after addressing common problems, consider consulting Ubuntu documentation or forums where you may find additional guides and solutions for specific issues.

This article provides a basic overview of how to change IP address, gateway, and network settings on Ubuntu. Every network infrastructure and configuration requirements are unique, so it's essential to tailor settings to your network's specific needs.