Setting a Static IP address in Linux

Setting a static IP address in Linux.png
Setting a static IP address in Linux.png

Introduction:
Usu­ally, IP ad­dresses are dy­nam­i­cally as­signed by the router's DHCP server on most net­work de­vices. But us­ing a sta­tic IP ad­dress on the server makes the pro­vided ser­vices more sta­ble. If the DHCP server is not work­ing, it will af­fect the com­put­ers in the lo­cal area net­work to ob­tain IP ad­dresses, thereby af­fect­ing ac­cess to ser­vices, while servers with sta­tic IP ad­dresses are not af­fected, so sta­tic IP is also nec­es­sary (on de­vices pro­vid­ing ser­vices).
Table of Contents:
  1. Configuring a static IP address in RHEL/CentOS/Fedora:
  2. Setting a static IP address in Debian / Ubuntu (versions prior to 17.10):
  3. Setting a static IP address in Ubuntu (newer versions):
  4. Setting up VLAN in Ubuntu:
  5. Setting up Bridge in Debian:

Configuring a static IP address in RHEL/CentOS/Fedora:

To set a sta­tic IP ad­dress in RHEL / CentOS / Fedora

First check the lo­cal net­work in­ter­face name:

2022-03-18_121212.png
2022-03-18_121212.png

The above fig­ure shows that the net­work in­ter­face name of the lo­cal ma­chine is eth0, rep­re­sent­ing the edited net­work file ifcfg-eth0

Use nano or vim to edit:

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

Make changes to the ifcfg-eth0 file based on your own net­work:

DEVICE="eth0"
BOOTPROTO="static"
DNS1="8.8.8.8"
DNS2="1.1.1.1"
GATEWAY="192.168.0.1"
HOSTNAME="linux.kingtam.win"
HWADDR="00:19:99:A4:46:AB"
IPADDR="192.68.0.100"
NETMASK="255.255.255.0"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="8105c095-799b-4f5a-a445-c6d7c3681f07"

You need to edit the fol­low­ing set­tings:

  • BOOTPROTO is dhcp (default) or static (static)
  • DNS1 and DNS2 can use public DNS services such as 8.8.8.8, 1.1.1.1
  • Gateway (GATEWAY) is the IP of the router or firewall
  • Host name (HOSTNAME) customizable
  • Network mask (NETMASK) is usually 255.255.255.0 (/24) modify according to your own network parameters
  • IP address (IPADDR) the local IP address
  • ONBOOT whether to start automatically is yes or no

Then edit: resolve.conf

resolve.conf is used to set the de­f­i­n­i­tions of each item when the DNS client re­quests name res­o­lu­tion.
vim /etc/resolv.conf
nameserver 8.8.8.8 # Modify with your preferred DNS address
nameserver 1.1.1.1 # Modify with your preferred DNS address

Use one of the fol­low­ing com­mands to restart the net­work and ap­ply all set­tings:

/etc/init.d/network restart
systemctl restart network

Setting a static IP address in Debian / Ubuntu (versions prior to 17.10):

To set a sta­tic IP ad­dress in Debian/Ubuntu (ver­sions prior to 17.10), open the fol­low­ing file:

vim /etc/network/interfaces

The de­fault is dhcp:

no-auto-down eth0 # or possibly auto eth0
iface eth0 inet dhcp

Edit us­ing nano or vim, and make changes based on your own net­work:

no-auto-down eth0
iface eth0 inet static
  address 192.168.0.100
  netmask 255.255.255.0
  gateway 192.168.0.1
  dns-nameservers 1.1.1.1
  dns-nameservers 8.8.8.8

The fol­low­ing set­tings need to be edited:

  • Change iface eth0 inet dhcp (default) to iface eth0 inet static (static)
  • dns-nameservers can have multiple values, such as using public DNS services like 8.8.8.8, 1.1.1.1
  • gateway is the IP address of the router or firewall
  • netmask is usually 255.255.255.0 (/24), adjust according to your own network parameters
  • address is the IP address of the local machine

Af­ter sav­ing the interfaces set­tings, edit /etc/resolv.conf:

vim /etc/resolv.conf
nameserver 8.8.8.8 # Modify with your preferred DNS address
nameserver 1.1.1.1 # Modify with your preferred DNS address

Use one of the fol­low­ing com­mands to restart the net­work and ap­ply all set­tings:

/etc/init.d/network restart
systemctl restart network

Setting a static IP address in Ubuntu (newer versions):
Start­ing from Ubuntu 17.10, Netplan is the de­fault net­work man­age­ment tool.

Net­work con­fig­u­ra­tion files are stored in *.yaml files in the di­rec­tory /etc/netplan/.

In my case, it is /etc/netplan/00-installer-config.yaml, and the de­fault con­tent is:

network:
  ethernets:
    eth0:
      dhcp4: true
  version: 2

Edit the 00-installer-config.yaml con­fig­u­ra­tion file us­ing nano or vim:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
     dhcp4: no
     addresses: [192.168.1.2/24]
     routes:
      - to: default
        via: 192.168.1.1
     nameservers:
       addresses: [8.8.8.8,1.1.1.1]

You need to edit the fol­low­ing set­tings:

  • renderer: networkd specifies that this interface is managed by the systemd-networkd service
  • dhcp4: no changes to manual static IP address setup
  • addresses the local IP address and network segment
  • nameservers DNS server

    • addresses [8.8.8.8,1.1.1.1] can be multiple IP addresses

Save the 00-installer-config.yaml set­tings.

Ap­ply the net­work set­tings us­ing the fol­low­ing com­mand:

netplan apply


Setting up VLAN in Ubuntu:
In this ex­am­ple, N1 is flashed with Arm­bian, and the net­work port is con­nected to the trunk port of the switch.

Dynamically obtain an IP address

The net­work con­fig­u­ra­tion files are stored in *.yaml files in the /etc/netplan/ di­rec­tory.

The de­fault con­tent of /etc/netplan/armbian-default.yaml is:

network:
  version: 2
  renderer: NetworkManager

Edit the /etc/netplan/armbian-default.yaml con­fig­u­ra­tion file us­ing nano or vim:

network:
  version: 2
  renderer: NetworkManager
  ethernets:
      eth0:
          dhcp4: true
  vlans:
      vlan.3:
          id: 3
          link: eth0
          dhcp4: true

The con­fig­u­ra­tion file is writ­ten in YAML for­mat, and var­i­ous set­tings are in­cluded:

  • The "version: 2" in the first line specifies the YAML version used in this file.
  • The "renderer" field specifies the network management tool to be used, which in this case is "NetworkManager", a popular tool for managing networks on Linux systems.
  • In the "ethernets" section, the "eth0" physical interface is configured, and the interface is set to obtain an IPv4 address via DHCP.
  • In the "vlans" sec­tion, the VLAN (Vir­tual Lo­cal Area Net­work) in­ter­face on the "eth0" net­work in­ter­face is con­fig­ured. The VLAN ID is 3 and is con­nected to the phys­i­cal in­ter­face "eth0" via the "link" field. The VLAN in­ter­face is also con­fig­ured to ob­tain an IPv4 ad­dress via DHCP.

    This con­fig­u­ra­tion file sets up a net­work con­nec­tion where both the phys­i­cal in­ter­face "eth0" and the VLAN in­ter­face ob­tain dy­namic IP ad­dresses via DHCP.

Set static IP address

version: 2
renderer: NetworkManager
ethernets:
    eth0:
      dhcp4: false
vlans:
    vlan.3:
        id: 3
        link: eth0
        dhcp4: no
        addresses: [10.3.3.3/24]
        routes:
         - to: default
           via: 10.3.3.1
        nameservers:
          addresses: [8.8.8.8,1.1.1.1]
The VLAN con­fig­u­ra­tion sets up a new in­ter­face vlan.3 with ID 3 as­so­ci­ated with eth0. Its dhcp4 is set to no, with the sta­tic IP ad­dress 10.3.3.3 and sub­net mask 24. The de­fault route is spec­i­fied through IP ad­dress 10.3.3.1, and 8.8.8.8 and 1.1.1.1 are set as DNS servers.

Setting up Bridge in Debian:

Without VLAN

To find out the de­fault net­work in­ter­face name of the ma­chine, use the fol­low­ing com­mand:

ip -c route | head -n 1 | cut -d ' ' -f 5
eth0

Edit the net­work in­ter­face via text ed­i­tor

sudo vim  /etc/network/interfaces

i. As DHCP:

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# Wired adapter #1
allow-hotplug eth0
no-auto-down eth0
# Bridge as DHCP
auto br0
iface br0 inet dhcp
    bridge_ports eth0

Then restart the networking ser­vice:

sudo systemctl restart networking.service

i. As Static:

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# Wired adapter #1
allow-hotplug eth0
no-auto-down eth0
# Bridge setup to static IP
auto br0
iface br0 inet static
    address 10.1.1.10
    broadcast 10.1.1.255
    netmask 255.255.255.0
    gateway 10.1.1.1
    bridge_ports eth0
    bridge_stp off       # disable Spanning Tree Protocol
    bridge_waitport 0    # no delay before a port becomes available
    bridge_fd 0          # no forwarding delay

Then restart the networking ser­vice:

sudo systemctl restart networking.service

With VLAN (Ad­vanced Ex­am­ple)

Make sure the vlan pack­age is in­stalled on the sys­tem:

sudo apt install vlan -y

If the host is a hy­per­vi­sor con­sider adding be­low sysctl con­fig­u­ra­tions:

echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.conf.all.arp_filter=0" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.conf.all.rp_filter=2" | sudo tee -a /etc/sysctl.conf

Load con­fig­u­ra­tions:

$ sudo sysctl -p
net.ipv4.ip_­for­ward = 1
net.ipv4.conf.all.arp_­fil­ter = 0
net.ipv4.conf.all.rp_­fil­ter = 2

Then mod­ify in­ter­faces con­fig­u­ra­tions:

sudo vim  /etc/network/interfaces

The in­ter­face con­tent is as fol­low:

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# Wired adapter #1
allow-hotplug eth0
iface eth0 inet manual
    pre-up ifconfig $IFACE up
    pre-down ifconfig $IFACE down

# VLAN 3
auto eth0.3
iface eth0.3 inet manual

# Bridge br0
auto br0
iface br0 inet static
    bridge_ports eth0.3
    bridge_stp off
    bridge_waitport 0
    bridge_fd 0
    address 10.3.3.3
    netmask 255.255.255.0
    gateway 10.3.3.1
    dns-nameservers 1.1.1.1 8.8.4.4

Then restart the networking ser­vice:

sudo systemctl restart networking.service

Conclusion:
Each Linux dis­tri­b­u­tion may have dif­fer­ent meth­ods for set­ting a sta­tic IP ad­dress, but the process is gen­er­ally sim­i­lar.

Reference: