As a network administrator, you are probably managing various Linux machines over different subnets of your company infrastructure.
As network topology changes, you may need to change the IP address already implemented on some machines.
Also, if you switched from DHCP to static IP addressing, you will also need to change the IP address on some of your computers.
Luckily for you, Linux has multiple ways of changing your IP address, whether you want it to be dynamic or static.
You will see how it is possible to have multiple IP addresses for a single machine and how you can assign IP addresses to virtual network adapters.
Prerequisites
Before changing your IP address, make sure to have a look at your current IP address.
To find your current IP address, you can use the “ip” command with the “a” option for address.
$ ip a
As you can see from the screenshot, my host is equipped with two network adapters :
- the loopback address (or localhost) which is used to test the network connectivity of your own computer;
- the “enp0s3” interface : acting as a main network adapter, the network card has multiple IP addresses associated with it (IPv4 and IPv6) followed by the IP address assigned to them.
In the present situation, my computer can be reached on “192.168.178.31/24” via the “192.168.178.1/24” gateway.
Change IP Address using ifconfig
On modern distributions, the “ifconfig” command has been completely deprecated and it is now advised to use the “ip” command.
However, you should still be able to use the “ifconfig” to change your IP address.
$ which ifconfig /usr/sbin/ifconfig
To change your IP address on Linux, use the “ifconfig” command followed by the name of your network interface and the new IP address to be changed on your computer.
To assign the subnet mask, you can either add a “netmask” clause followed by the subnet mask or use the CIDR notation directly.
$ ifconfig <interface_name> <ip_address> netmask <netmask_address>
Note : in order to change your IP address, you will need to be an administrator on your computer (part of the sudo group on Debian/Ubuntu or wheel on CentOS/RedHat)
For example, given the IP addresses used in the previous sections, if we want to change our IP address (to 192.168.178.32/24), we would run the following command
$ ifconfig enp0s3 192.168.178.32/24 $ ifconfig enp0s3 192.168.178.32 netmask 255.255.255.0
In order to verify that your IP address was correctly changed, you can run the “ifconfig” command followed by the name of your network adapter.
$ ifconfig <interface_name>
From DHCP to Static
When manually changing your IP address, Linux automatically understands that you want to change from using a DHCP server to static IP addressing.
This information is materialized in the “ifconfig” command : in the first screenshot, you can see that my IP address was assigned with a “dynamic” parameter also called DHCP.
This is not the case anymore after assigning the IP address manually.
Note that your changes are not made permanent by modifying your IP settings with the “ifconfig” : they are only modified for the current session.
Change IP Address Permanently using ifupdown
On Linux, changing your IP address using network utilities does not mean that your IP configuration will be saved on reboots.
Network Files on Debian & Ubuntu
In order to change your IP address on Linux, you will have to add your network configuration in the “/etc/network/interfaces” or create this file if it does not exist already.
# Content of /etc/network/interfaces iface eth0 inet static address <ip_address> netmask <network_mask> gateway <gateway_ip>
For example, let’s say that you want to change your IP to be “192.168.178.32” with a subnet mask of “255.255.255.0” and a default gateway of “192.168.178.1”.
To change your IP address to reflect those changes, you would edit the content of your interfaces file and add the following content
$ vim /etc/network/interfaces # Content of /etc/network/interfaces iface eth0 inet static address 192.168.178.32 netmask 255.255.255.0 gateway 192.168.178.1
In order for the changes to be applied, you will need to restart your networking service (managed by ifupdown)
# For systemd hosts $ sudo systemctl restart networking.service # For pre-systemd hosts sudo /etc/init.d/networking restart
After restarting your networking service, you should be able to see your new IP by running the “ifconfig” or the “ip” command.
$ ifconfig
$ ip address
Network Files on CentOS & Red Hat
In order to change your IP address on Linux, you will have to add your network configuration in the “/etc/sysconfig/network-scripts” directory.
In the “/etc/sysconfig/network-scripts”, identify the network interface to be modified and start editing it.
$ ls -l /etc/sysconfig/network-scripts $ nano <file>
In order to set an IP to be static on CentOS or RHEL, you want to modify the “BOOTPROTO” parameter from “dhcp” to “static” and add your network information such as the netmask or the default gateway.
On recent distributions such as CentOS 8 or RHEL 8, you have to use the nmcli utility in order for the changes to be effective.
However, if you are still using the network service (for distributions such as CentOS 7 or RHEL 7), you can restart the network service for the changes to be applied.
$ nmcli device reapply <interface_name> (on CentOS 8) $ systemctl restart network.service (on CentOS 7/RHEL 7)
Awesome!
You successfully changed your IP address on Linux.
Make sure to execute the “ip” command again to verify that your changes were applied.
$ ip a $ ifconfig
Change IP Address using Network Manager
On modern distributions, equipped with systemd, you may have come across the Network Manager many times.
The Network Manager is an all-in-one tool that exposes multiple utility tools in order to change connections, devices or connectivity settings (even wireless) on your host.
One of those utilities is called “nmcli” and this is what we are going to use in order to change our IP address.
To change your IP address, use “nmcli” on the “device” section and specify that you want to “modify” the “ipv4.address” of your network card.
$ nmcli device modify <interface_name> ipv4.address <ip_address>
When using the “nmcli device modify” command, your Network Manager will automatically create a new connection file in the /etc/NetworkManager/system-connections folder.
In order for the changes to be effective, you will need to “reapply” parameters to your current connection settings.
$ nmcli device reapply <interface_name>
Congratulations, you successfully changed your IP using the Network Manager!
However, changing settings using the nmcli tool won’t make your changes persistent over multiple reboots.
Change IP Address Permanently using Network Manager
In order for changes to be persistent, you need to edit the connection files located at /etc/NetworkManager/system-connections.
In order to change your IP address, edit the Network Manager configuration file, identify the line to be modified and set the IP address accordingly.
Save the file and make sure to reapply the device configuration by using the “nmcli” command with the “device reapply” options.
$ nmcli device reapply
Now that your changes are effective, you can check your IP address by running the “ifconfig” or “ip” commands.
Modify IP Address using Graphical Interface
In some cases, you may want to modify your IPv4 address by navigating through graphical windows.
On modern distributions, the network parameters can be managed by the “network” icon (which is called nm-applet) located at the top right corner of your screen.
In your network settings, click on the “gear wheel” next to the connection to be modified.
Next, in the IPv4 section of your connection settings, you can set your IP method to manual and attribute your static IP address.
To change your IP address, simply click on “Apply” and restart the networking services by using nmcli.
$ nmcli networking off $ nmcli networking on
That’s it! You just changed your IP address on Linux.
How networking is managed on Linux
As of January 2020, on recent distributions, you may deal with several tools that are used by your distribution to configure networking.
Most of the time, the Network Manager and ifupdown are managing networking.
$ sudo systemctl status NetworkManager $ sudo systemctl status networking
In some distributions, “ifupdown” might not be installed at all and interfaces are only managed by the NetworkManager.
However, if the two services exist on your computer, you will be able to declare interfaces in the /etc/network/interfaces file without the NetworkManager interfering with those settings.
If you want the Network Manager to manage interfaces declared in the interfaces file, you will have to modify the “managed” parameter to true in the NetworkManager.conf configuration file.
Conclusion
In this tutorial, you learnt how you can successfully change your IP address on Linux : either using the Network Manager or the ifupdown utility.
You also learnt how networking is managed and architectured on Linux and how you should configure it to avoid IP address conflicts.
If you are interested in Linux system administration, we have a complete section dedicated to it on the website, so make sure to check it out!