How to Change IP Address in Linux?
Overview
Changing IP addresses in Linux is a common task for system administrators and users who require network configuration flexibility. Whether you need to switch from dynamic to static IP addressing or adjust network settings for specific distributions like Debian, Ubuntu, CentOS, or Red Hat, this guide will walk you through the process step-by-step. In this article, we will answer the question of how to change your IP address in linux and explore various methods to change your IP address in Linux with instructions for both command-line and graphical user interface (GUI) approaches.
Introduction
An IP address is a unique identifier assigned to each device connected to a network. It allows devices to communicate with each other over the internet or local networks. An IP address consists of network portion and host portion, whereas, the network portion identifies the network to which a device belongs and the host portion represents the unique identifier assigned to a device within a specific network.
The IP addresses are associated with an interface, which is a physical or virtual connection between a device and a network. The interface is identified in a network using its IP address but also has a unique identifier known as an interface name. Whenever a device wants to communicate with another device or access network resources, it performs the following steps,
- Encapsulates data into IP packets, including the source and destination IP addresses.
- The packets are then transmitted through the interface, following the network routing rules and utilizing the IP address associated with the interfaces.
Note: The IP address is used in the network layer (Layer 3) of the TCP/IP protocol stack to route data packets across networks. The interfaces operate at the data link layer(Layer 2) of the OSI model. The data link layer is responsible for the reliable transmission of data frames between directly connected devices on a local network.
Change IP Address Using ifconfig
In context with how to change IP address in linux, the ifconfig command is a widely used utility in Linux for managing network interfaces. To change your IP address using ifconfig, follow these steps,
- Open a terminal or command prompt.
- Use the following command to display the current network interface configurations,
- The eth0 and lo are the names of the network interfaces. eth0 represents an Ethernet interface, while lo stands for the loopback interface.
- Thi flags indicates the status and characteristics of the network interface. In the above output,
- UP signifies that the interface is up and operational
- BROADCAST means the interface supports broadcasting.
- RUNNING indicates that the interface is functioning correctly.
- MULTICAST denotes that the interface supports multicast communication.
- The mtu or Maximum Transmission Unit (MTU) specifies the largest data packet size that can be transmitted over the network interface without fragmentation.
- The inet gives the IPv4 address and inet6 indicates an IPv6 address.
- The netmask determines the network portion of an IP address and is used to separate the network address from the host address.
- The broadcast represents the last IP address in the network range and is used for network communication purposes.
- The ether field displays the MAC (Media Access Control) address of the network interface.
- The txqueuelen indicates the length of the transmit queue or the number of packets waiting to be sent.
- The loop refers to the loopback interface, which allows network communications within the same machine.
- The number of received (RX) and transmitted (TX) packets are represented by the RX packets and TX packets.
- Other fields show various statistics related to packet transmission and reception, including error counts, dropped packets, overruns, frame errors, carrier errors, and collisions.
- Identify the network interface you wish to modify.
- We can use the following syntax to change the Ip address of an interface_name with new_IP_address,
From DHCP to Static
By default, most Linux systems use DHCP (Dynamic Host Configuration Protocol) to obtain IP addresses automatically. However, there may be instances where you need to switch from DHCP to a static IP address. To find if your IP address is assigned by DHCP, you can use the ifconfig command and check if the inet dhcp or inet6 dhcp is mentioned in the output. We can follow the following steps to change the IP address from DHCP to static,
- Open the terminal and select the interface to be configured.
- Locate the network configuration file for the desired interface. The location of these files may vary depending on the Linux distribution you are using,
- For Debian and Ubuntu distros, it can be found at /etc/network/interfaces.
- For CentOS and Red Hat distros, it can be found at /etc/sysconfig/network-scripts/ifcfg-<interface> where <interface> is the actual name of your interface.
We will explore the steps of a method of configuration for Debian and Ubuntu in this section and for CentOS and Red Hat distros in the next section.
- Open the network interface configuration file.
- You can find the data similar to the following lines in that file where <interface> is the name of your interface. For example, eth0 or wlan0.
- Change it to the following format where <interface> is the name of your interface.
- The <new_IP_address> is the new IP address you want to assign to the network interface.
- The <netmask_value> is the subnet mask for the network interface which determines the network portion and the host portion of the IP address.
- The <gateway_address> specifies the default gateway for the network interface.
- Save the changes to the file (Ctrl + O in nano) and exit the editor (Ctrl + X in nano).
- Restart your network service. Follow the steps to restart your network service,
- Check the init system used in your distro to manage services.
- If the output is systemd, use the following command,
- If the output is init and your system is CentOS or Red Hat, use the first command or else go with the second command.
You have successfully configured the Static IP for your system.
In context with how to change IP address in linux, we can also use the ip to change the IP Address of the Linux machine.
- The ip command is a powerful networking command in Linux that provides detailed information about network interfaces, IP addresses, and configurations.
- It is part of the iproute2 package, which is a modern replacement for the older ifconfig command.
- The ip command is recommended over ifconfig because it offers more advanced functionality and is being actively maintained and developed.
The following steps are followed to change the IP address using the ip command,
- Check the current network interface configuration by executing the following command
- Disable the desired network interface with the name <interface_name> to make changes by running the following command.
- Change the IP address by executing the following command,
Replace <new_IP_address> with the desired IP address, <netmask_value> with the appropriate subnet mask, and <interface_name> with the name of the network interface.
- Enable the network interface to apply the changes by running the following command,
- Verify the new IP address by executing the following command,
You should be able to see the changed IP address in the output of the above command.
Change IP Address Permanently Using ifupdown
When you think about how to change IP address in linux permanently, the ifupdown package provides a more permanent solution for changing IP addresses in Linux. It simplifies network interface configuration by utilizing network files. The process varies slightly depending on the Linux distribution.
Network Files on Debian & Ubuntu
On Debian and Ubuntu systems, the network configuration files are located in the /etc/network/ directory. These files contain the necessary settings to configure network interfaces and set up IP addresses. The main configuration file is /etc/network/interfaces. We have seen how to update the IP address in the configuration file in the previous section. Now let us explore the best way to perform the same action using the ifupdown command.
To use the ifupdown command to change the IP address permanently, follow the following steps,
- Use the following command to check the current status of the network interface you want to configure. The <interface> is the name of the interface.
- If the interface is already up (active), you need to bring it down before making any changes.
- Edit the network configuration file using a text editor. Follow the instruction in the previous section until saving the configured file.
- After modification, bring the network interface back up with the new configuration.
Network Files on CentOS & Red Hat
On CentOS and Red Hat Linux distributions, network configurations are located in the /etc/sysconfig/network-scripts/ directory. These files contain settings for individual network interfaces and can be edited to change the IP address permanently. The specific file to modify depends on the interface you want to configure, usually named ifcfg-<interface>, where <interface> is the name of the interface like eth0 or ens33. The steps are,
- Open the network configuration file for the desired interface,
The file has multiple configuration parameters that can be modified. The important parameters for changing the IP address are,
- BOOTPROTO: Set this to static to use a static IP address.
- IPADDR: Specify the desired IP address.
- NETMASK: Set the subnet mask for the network.
- GATEWAY: Specify the IP address of the gateway.
- Edit the file and make the necessary changes to the desired parameters.
- Save the changes and exit the editor.
- Restart the network service to apply the new configuration using the appropriate command such as,
Change IP Address Using Network Manager
For those who prefer a graphical user interface, we can answer the question of how to change IP address in linux permanently using the Network Manager, which provides a user-friendly method to change IP addresses in Linux:
- Go to settings in your Linux machine and select the Network settings.
- Choose the desired network connection and click the Edit option.
- You can see a window, and navigate to the IPV4 or IPV6 tab based on your requirements.
- Select Manual instead of Automatic (DHCP). Leave the other options like DNS and Routes as default.
- Enter the new IP address, respective netmask, and gateway addresses.
- Save the changes and restart the network service to see the updated IP address in effect. You can check the change using the ifconfig command.
We can also use the Network Manager command line utility known as nmcli to resolve the problem of how to change IP address in linux permanently. The steps to change the IP address in Linux using the nmcli tool are,
- Check if the nmcli tool is installed by running the following command,
If the nmcli tool is not installed, you can install it using your package manager.
- List the available network connections by executing the following command.
You can get more information on a connection using the command,
- Identify the connection for which you want to change the IP address. To modify the IP address for the selected connection, use the following command,
- Apply the changes and apply the modified connection using the following command,
Now you have configured a new IP address and you can check it using the ifconfig or ip addr show command.
Conclusion
- Changing your IP address in Linux is a very essential skill for network configuration.
- The ifconfig command is a well-known tool in Linux for managing network interfaces.
- Most Linux systems use DHCP to obtain IP addresses automatically and we can convert the IP address to static by modifying the interface configuration files.
- It is important to know the location of network files and their configuration terms as they change according to the Linux distro.
- The Network Manager is a graphical user interface that can be used to change IP addresses.
- We can also use the nmcli tool to change the Ip address from the command line.