Configuring Static IP Address from the Command Line in Linux: A Comprehensive Guide
Before we dive into the comprehensive steps for configuring a static IP address from the Linux command line, let’s get a quick understanding of the most important commands:
TL;DR:
Firstly, use the ip link
command to find out the interfaces. The interface showing up typically is the one you want to use.
ip link
Then, add your static IP address (for example, 198.32.160.100
) using the ip addr
command:
sudo ip addr add 198.32.160.100/24 dev eth0
Next, set the default gateway (for example, 198.32.160.1
) using the ip route
command:
sudo ip route add default via 198.32.160.1 dev eth0
Please note that the netmask is given in CIDR notation (it is the /24
right after the IP of the device in the ip addr
command).
With this brief guide in hand, let’s delve into the finer details.
Why Set a Static IP?
Before proceeding, let’s understand why setting a static IP is necessary. Computers use IP addresses to communicate over a network. By default, your system will likely receive a dynamic IP address from the DHCP server. However, for server environments, it’s often preferable to have a static IP that doesn’t change, facilitating seamless network access and management. Dedicated servers will call these static IP addresses or just an IP address.
Configuring Static IP Address
Let’s first check your existing network interfaces using the ip addr
command:
ip addr show
In the output, find the network interface you want to configure. It will typically be something like eth0
or ens160
.
Next, let’s configure a static IP. Suppose we want to assign the IP address 192.168.1.100
to the interface eth0
. We can do this by using the ip addr
command:
sudo ip addr add 192.168.1.100/24 dev eth0
Here, 192.168.1.100
is the IP address, /24
is the subnet mask equivalent to 255.255.255.0
, and eth0
is the interface name.
Tip: To remove the existing IP address, you can use the ip addr del
command in a similar way.
Trick: If you want to check whether the IP has been assigned successfully, you can run the ip addr show eth0
command.
Setting the Default Gateway
After assigning the IP, you should set up the default gateway using the ip route
command:
sudo ip route add default via 192.168.1.1 dev eth0
Here, 192.168.1.1
is the IP address of the gateway, and eth0
is the interface name.
Tip: To delete an existing default gateway, use the ip route del default
command.
Resolving DNS
Edit the /etc/resolv.conf
file to set the DNS servers:
sudo nano /etc/resolv.conf
Then, add the following lines to the file, replacing 8.8.8.8
and 8.8.4.4
with your DNS servers:
nameserver 8.8.8.8
nameserver 8.8.4.4
Save and close the file.
Trick: Always keep a backup of your original resolv.conf
file before editing.
Making the Changes Permanent
These changes will be lost upon reboot. To make them permanent, you need to edit the interface configuration file. This process varies across different Linux distributions.
On Ubuntu and other Debian-based distributions, this file is usually located in /etc/network/interfaces
. On RHEL, CentOS, and Fedora, it’s in /etc/sysconfig/network-scripts/
, and the file name would be something like ifcfg-eth0
.
Tip: Always keep a backup of your original configuration file before making changes.
Conclusion
Configuring a static IP from the command line in Linux might seem daunting initially, but with a clear understanding of the process and some helpful tips and tricks, it can become a smooth operation. Remember, a comprehensive understanding of your network setup is crucial before making any changes.
For additional assistance, consider PureVoltage’s dedicated server solutions with 24/7 technical support to help ensure your server always stays online and performs optimally.