Skip to Content

3 Ways to change ip address in Linux

Do you need to change your IP address frequently? Are you having trouble finding a way to do it on Linux? If so, you have come to the right place. In this blog post, we will discuss three different ways that you can change your IP address on Linux.

We will also provide instructions on how to do it. So, whether you are a beginner or an expert Linux user, you should be able to follow along without any problems!

  • ifconfig eth0 192.168.0.100 netmask 255.255.255.0
  • ip addr add 192.168.0.100/24 dev eth0
  • change ip address in file /etc/sysconfig/network-scripts/ifcfg-eth0 and restart network systemctl restart network

 

understanding network interface and ip address in Linux

Network interfaces are devices that allow communication with other devices on a computer network. A computer can have multiple network interfaces, each of which has its own IP address.

In Linux, you can view a list of all the network interfaces and their associated IP addresses by running the ifconfig -a or ip addr command. Check this post to get more info about how to find ip address in Linux.

change ip address with ifconfig Command in Linux

To change IP address in Linux, we can use “ifconfig” command. Type ifconfig followed by the name of your network interface and the new IP address. Press enter. The new ip address will be assigned to this interface.

For example, ifconfig eth0 192.168.0.100 netmask 255.255.255.0 will change the ip address on eth0 to 192.168.0.100.

The “interface” parameter specifies the name of the network interface. The “ip address” parameter specifies the IP address for the network interface. The “netmask” parameter specifies the netmask for the network interface.

The changes made with ifconfig will not persist after a reboot, which means that every time the system is restarted. The IP address will be reset to the value it had before.

In addition, ifconfig command is considered as deprecated command and it is replaced by iproute2 package in many distributions. 

procedure to change IP address with ifconfig command in Linux

The procedure to change IP address in Linux is as follows:

  • Open the terminal application.
  • List the current IP addresses for all network interfaces with command ifconfig -a
  • Take the network interface down with command: ifconfig <interface> down
  • Change the IP address with command ifconfig <interface> <ip address> <netmask>
  • Press Enter to run the command.
  • Verify that the new IP address is correct with command ifconfig -a
  • Take the interface up with command ifconfig <interface> up

 

change ip address with ip Command in Linux

Another way to change your IP address on Linux is to use the ip command. This is a more advanced method to change your IP address. You will need to open a terminal window and type in the following command:

sudo ip addr add 192.168.0.100 255.255.255.0 dev eth0

Replace 192.168.0.100 with the IP address and netmask that you want to use.  This will add an IP address to your system. You can then close the terminal window and continue using your computer as normal.

This command adds the new IP address to the specified interface.

Note: It does not remove or update the existing IP address. It appends the current IP configuration.You need to run sudo ip addr del command to remove the old ip address.

The new settings take effect immediately – however they are not persistent and will be lost after a reboot.

 Ip command is more up-to-date, and it is considered as the standard tool for managing the network on Linux systems.

change ip address permanently in Linux

You can change the IP Address permanently by modifying the configuration file. Under the /etc/sysconfig/network-scripts directory, you’ll see a configuration file for every network interface on your system. For example, if your interface is “eth0″, you’ll see ifcfg-eth0 file under this directory.

Modify the ifcfg-eth0 file with your favorite text editor (a lot of people prefer to use vi or nano) and change the IPADDR field accordingly as shown below to change the ip-address.

#   vi /etc/sysconfig/network-scripts/ifcfg-eth0

Change the IP address to the correct IP, make sure that the Netmask and Gateway are configured correctly as well and save.

#  vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO=none
ONBOOT=yes
TYPE="Ethernet"
IPADDR="192.168.0.100"
NETMASK="255.255.255.0"
GATEWAY="192.168.0.1"

After you save the ifcfg-eth0 file you will have to restart the network. You can restart the network with the following command: systemctl restart network

FAQ about ip address in Linux

How to find your IP address in Linux

To find your IP address in Linux, use the ip addr command. This will print the IP address and other information about your network interfaces.

If you want to find out your public IP address, you can use the curl command. This will send a request to a web server and return your public IP address. curl ifconfig.me

What is the netmask for your ip address in Linux?

The netmask in Linux is a bitmask that determines what IP addresses are on the same network as your computer. If two computers have the same netmask, they are on the same network. If they have different netmasks, they are on different networks.

how to find the gateway for ip address in Linux

To find the gateway in Linux, you can use the route command. This will print the IP address of the gateway for your default route. You can also use the ip addr command to find the gateway.

what is difference between a private and public IP address in Linux

The private IP address space is reserved for internal networks only and cannot be routed on the Internet as addressing like a public IP address space can be, which makes it much more difficult to use this form of addressing on the Internet.

A public IP address can be easily found by looking up the domain name of a website. Conversely, a private IP address is much harder to find as it requires special software and tools to do so.

Additionally, most home users will not have a static public IP address but their private IP addresses will remain the same unless they take specific action to change it.

ifconfig command vs ip command

The ip command is a replacement for the ifconfig command. In earlier versions of Linux, the ifconfig command was the default utility for checking and verifying IP configuration. The ifconfig command does not support many modern features that were not relevant in earlier times.

The ifconfig command is deprecated. Although some Linux distributions still include the ifconfig command for backward compatibility in their current version of Linux, they may consider gradually removing it from their upcoming Linux versions.

Because of this, even if the ifconfig command is still available on some Linux distributions, you should use the ip command for all IP validation and verification-related tasks. The ip command is designed to meet the requirements of modern networks.

Neno Laiz

Friday 17th of November 2023

Modifying network configuration file is most reliable way to change ip address in Linux.

But the process may vary slightly depending on the Linux distribution and the network management system in use.

The location and name of the network configuration file can vary. Common locations include /etc/network/interfaces in Debian-based distributions, or files within /etc/sysconfig/network-scripts/ in Red Hat-based distributions.