DEV Community

Livia Lima
Livia Lima

Posted on • Originally published at blog.livialima.net on

How to Set Static IP Address on Linux

Different from desktop machines where you can use dynamic IP addresses, static IP address are often required on a server infrastructure.

RHEL / CentOS / Fedora:

You will need to edit:

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

Enter fullscreen mode Exit fullscreen mode

Where ifcfg-eth0 relates to your network interface eth0. If your interface name is enp1s0 then the file that you will need to edit is ifcfg-enp1s0.

Add the following fields:

DEVICE="eth0"
BOOTPROTO="static"
IPADDR="192.168.1.1"
NETMASK="255.255.255.0"
TYPE="Ethernet"

Enter fullscreen mode Exit fullscreen mode

Debian / Ubuntu:

To setup static IP address in Debian/ Ubuntu, edit the following file:

/etc/network/interfaces

Enter fullscreen mode Exit fullscreen mode

You may see a line looking like this:

auto eth0
iface eth0 inet dhcp

Enter fullscreen mode Exit fullscreen mode

Change it so it looks like this:

auto eth0
iface eth0 inet static
  address 192.168.1.1
  netmask 255.255.255.0

Enter fullscreen mode Exit fullscreen mode

Restart the networking on your system:

SysVinit : /etc/init.d/network restart

SystemD : systemctl restart network

If unsure of what which system manager your distro uses, try this:

pidof systemd && echo "systemd" || echo "sysvinit"

Enter fullscreen mode Exit fullscreen mode

Conclusion

You now know how to configure a static IP address on a Linux distro.

Top comments (0)