Info
Content

Configuring a Static IPv4 Address in Linux

Debian Based (Debian & Ubuntu up to 17.04)

Use your favourite text editor to edit the network interfaces file

nano /etc/network/interfaces

An example of a network configuration would be as follows, ensure to adjust it accordingly (device name and IP address settings)

auto eth0
iface eth0 inet static
 address 192.168.1.2
 netmask 255.255.255.0
 gateway 192.168.1.254
 dns-nameservers 77.247.176.114 77.247.176.115 85.159.238.17

For changes to take effect, the networking service will have to be restarted.

service networking restart
RHEL Based (CentOS)

Use your favourite text editor the edit the relevant network configuration file.

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

An example of a network configuration would be as follows, ensure to adjust it accordingly (device name and IP address settings)

DEVICE=eth0
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=192.168.1.2
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
DNS1=77.247.176.114
DNS2=77.247.176.115
DNS3=85.159.238.17

For changes to take effect, the networking service will have to be restarted.

service network restart
Ubuntu 18.04 (Netplan)

Use your favourite text editor the edit the relevant network configuration file.

nano /etc/netplan/01-netcfg.yaml

An example of a network configuration would be as follows, ensure to adjust it accordingly (device name and IP address settings)
Ensure the correct amount of spaces, and no tabs are used, as netplan is very picky with it's syntax. If you are running into issues, incorrect spacing is most likely the culprit.

network:
 version: 2
 renderer: networkd
 ethernets: 
   eth0:
     dhcp4: no 
     dhcp6: no 
     addresses: [192.168.1.2/24] 
     gateway4: 192.168.1.254 
     nameservers: 
       addresses: [77.247.176.114,77.247.176.115,85.159.238.17]

For changes to take effect, the configuration will have to be applied. Appending --debug may help you troubleshoot if you're running into any issues.

netplan apply

Multiple IP addresses can easily be added by defining multiple IP addresses to 'addresses' dividing them by commas.
Example:

addresses: [192.168.1.2/24,192.168.1.3/24]
No Comments
Back to top