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
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 :
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 192.168.1.1
- netmask 255.255.255.0
- gateway 192.168.0.1
- dns-nameservers 188.8.131.52
Next, save and close the file and restart the system for the changes to take place.
While the process to set up 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
More Linux articles
- Merging and sorting files in Linux: Easier than you think
- Sending email from Linux terminal: Efficient and powerful solution
- Using the Linux command line for basic file handling
- PsychOS: A retro-styled Linux distro for your old PCs
- Reading text files the easy way with PowerShell and Linux bash shell