Setting up static IP address for Linux: Easier than you think

There are two types of developers: Those who use Linux and those who will use Linux someday. The open-source operating system is becoming more and more popular because it is free — and because after years of ignoring or actively fighting against Linux, Microsoft has become almost friendly to the upstart penguin. For developers making the jump to Linux, this entry in our 20 Best of 2020 series from Sukesh Mudrakola, originally published in July, shows you how to set up a static IP address in Linux. And the key takeaway, as the headline says, is it’s easier than you think.

A static IP address, as the name suggests, is an IP address that does not change. It is also known as a fixed IP address. Having a static IP address can be crucial for several reasons, such as setting up a home file server, port forwarding, sharing a printer, or any other network-connected device over a network. A static IP address can also be used to remotely access a PC and helps in avoiding IP address conflicts in office environments. IP addresses on Linux systems are usually assigned automatically by Dynamic Host Configuration Protocol (DHCP) servers. But these IP addresses assigned are dynamic, meaning they may change every time you restart your Linux system or when the server reassigns the IP addresses. For the variety of reasons mentioned above, having a static IP address can be beneficial. Setting up a static IP address for Windows and macOS is pretty straightforward and can often be easily configured using several methods. However, setting up a static IP address in a Linux environment can be a bit tricky.

The process of setting up a static IP address in different Linux systems is slightly different but very simple nonetheless. Here is a step-by-step guide to setting up a static IP address on a Linux system.

Setting up a static IP address for Red Hat Enterprise Linux, CentOS, Scientific Linux, and Fedora

In Red Hat Enterprise Linux (RHEL), Fedora, and its other clones, including Scientific Linux and CentOS, configuration to set up a static IP address will be stored in its network interface card (NIC) configuration. The NIC can be found under /etc/sysconfig/network-scripts/ directory.

Before we proceed, it is important to know that all the commands we will be using needs root access. You can either log in as root user and proceed with the process or add “sudo” before every command.

To make changes to the configuration of the network card, we need to know the name of the network card. To get the NIC’s name, you can use any of the below commands:

$ip addr or $ip link show

Linux static IP address

If your device is connected via a wired network, then look for the network card’s name starting with the letter “e.” If you are connected via a wireless network such as WiFi, then the NIC’s name usually starts with the letter “w.”

Now, once you have the network card’s name, we need to open its configuration to set up a static IP address. You can edit this configuration file in either the command-line editor using the regular vi text editor or choose to do it graphically using some of the network utility packages such as NetworkManager TUI (nmtui).

To edit the configuration, use this command:

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

Note: Here eth0 is the network card name, and it can be different for different computers.

Add or modify the configuration below :

  • BOOTPROTO=static
  • DNS1=
  • DNS2=

static IP in linux

Then press “Esc” to exit insert mode and type :wq to save file and exit the editor. Once you have made the changes, restart the network either by restarting your system or simply using this command:

$ systemctl restart network

The system is now configured with a static IP address. If you want to switch back to a dynamic IP address, you can do so by simply setting “BOOTPROTO=dhcp” and removing the lines you added above.

Configuring Ubuntu/Debian systems

Just like the method we showed you above, setting up static IP in Debian or Ubuntu systems also needs slight configuration changes to the network interface card.

In Debian-based Linux systems, NIC’s configuration is stored under /etc/network/ directory. To edit it, you can use any editor or use this command:

$ sudo nano /etc/network/interfaces

Now, change or update the configuration to:

  • auto eth0
  • iface eth0 inet static
  • address
  • netmask
  • gateway
  • dns-nameservers

Next, save and close the file and restart the system for the changes to take place.

While the process to set up a static IP address varies slightly for every Linux distro, the core concept of changing certain parameters in the network configuration remains the same. Users can easily switch back to a dynamic IP address by reverting the settings to default.

Featured image: Shutterstock

About The Author

3 thoughts on “Setting up static IP address for Linux: Easier than you think”

  1. Hello

    What Will happened if I type as command
    ip add in the command window
    I am new to Linux system.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top