Setting a static IP address on Linux is more than just picking a number; it’s about giving a machine a permanent, predictable identity on the network, crucial for servers and devices that need to be reliably found.
Let’s see this in action. Imagine we have a server, webserver-01, that needs a fixed IP address 192.168.1.100 on its eth0 interface.
First, we need to know the current state.
ip addr show eth0
This will show us the current IP configuration, which might be dynamic (assigned by DHCP) or non-existent.
On modern Linux systems using systemd-networkd, configuration is done via .network files in /etc/systemd/network/.
Let’s create a file named 10-eth0.network for our eth0 interface.
[Match]
Name=eth0
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
The [Match] section tells systemd-networkd which interface this configuration applies to. Name=eth0 is straightforward.
The [Network] section is where the magic happens.
Address=192.168.1.100/24 assigns the static IP address and its subnet mask. The /24 is CIDR notation, equivalent to 255.255.255.0.
Gateway=192.168.1.1 specifies the default gateway, the router that all traffic destined for outside the local network will be sent through.
DNS=8.8.8.8 sets the primary DNS server. You can add more DNS servers, separated by spaces.
After creating or modifying this file, we need to enable and restart systemd-networkd.
sudo systemctl enable systemd-networkd
sudo systemctl restart systemd-networkd
Now, let’s verify the configuration.
ip addr show eth0
ip route show
ping -c 3 google.com
The first command should now show inet 192.168.1.100/24. The second will show default via 192.168.1.1 dev eth0. The third will confirm external connectivity.
For systems using the older ifupdown (Debian/Ubuntu) or network-scripts (RHEL/CentOS), the configuration files are different.
On Debian/Ubuntu with ifupdown, edit /etc/network/interfaces:
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8
Then apply with:
sudo ifdown eth0 && sudo ifup eth0
On RHEL/CentOS with network-scripts, edit /etc/sysconfig/network-scripts/ifcfg-eth0:
BOOTPROTO=none
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
ONBOOT=yes
Then apply with:
sudo systemctl restart network
The Gateway directive is crucial. Without it, your server can communicate with other machines on its local subnet (e.g., 192.168.1.0/24), but it won’t know how to reach anything outside of it, like the internet. The default gateway is essentially the exit ramp from your local network.
A common pitfall is forgetting to set the Gateway or setting it incorrectly. If you can ping 192.168.1.101 but not google.com, your gateway is almost certainly the problem.
Another subtle point is the subnet mask, represented as /24 or 255.255.255.0 in our examples. This defines the boundaries of your local network. If your server’s IP is 192.168.1.100/24 and the gateway is 192.168.1.1/24, they are on the same subnet. If you tried to set the gateway to 10.0.0.1 with a /24 mask on eth0, the system would think 10.0.0.1 is on a different network and would require a specific route to reach it, which you likely haven’t configured.
The surprising thing about static IP configuration is how much it ties into the routing table. When you set a static IP and gateway, you’re not just assigning an address; you’re implicitly telling the kernel to add a default route. If you later need to reach a different network that isn’t directly connected and isn’t on the path through your default gateway, you’ll need to add a static route.
For instance, if you have a private network 10.10.0.0/16 that is accessible via a router at 192.168.1.254 (which is not your default gateway), you’d add a route like this:
sudo ip route add 10.10.0.0/16 via 192.168.1.254
This command tells the system: "To get to any IP address in the 10.10.0.0/16 range, send the traffic through the router at 192.168.1.254."
This ability to define specific paths for traffic is what makes static routing so powerful for complex network setups, beyond just ensuring a server is reachable.
The next logical step after ensuring your machine has a stable IP and can reach the internet is to understand how to manage multiple network interfaces and their associated IP addresses and routes.