Introduction: |
Usually, IP addresses are dynamically assigned by the router’s DHCP server on most network devices. But using a static IP address on the server makes the provided services more stable. If the DHCP server is not working, it will affect the computers in the local area network to obtain IP addresses, thereby affecting access to services, while servers with static IP addresses are not affected, so static IP is also necessary (on devices providing services).
Table of Contents: |
- Configuring a static IP address in RHEL/CentOS/Fedora:
- Setting a static IP address in Debian / Ubuntu (versions prior to 17.10):
- Setting a static IP address in Ubuntu (newer versions):
- Setting up VLAN in Ubuntu:
- Setting up Bridge in Debian:
Configuring a static IP address in RHEL/CentOS/Fedora: |
To set a static IP address in RHEL / CentOS / Fedora
First check the local network interface name:
The above figure shows that the network interface name of the local machine is eth0
, representing the edited network file ifcfg-eth0
Use nano or vim to edit:
|
|
Make changes to the ifcfg-eth0
file based on your own network:
|
|
You need to edit the following settings:
- BOOTPROTO is
dhcp
(default) orstatic
(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
orno
Then edit: resolve.conf
resolve.conf
is used to set the definitions of each item when the DNS client requests name resolution.
|
|
|
|
Use one of the following commands to restart the network and apply all settings:
|
|
|
|
Setting a static IP address in Debian / Ubuntu (versions prior to 17.10): |
To set a static IP address in Debian/Ubuntu (versions prior to 17.10), open the following file:
|
|
The default is dhcp
:
|
|
Edit using nano or vim, and make changes based on your own network:
|
|
The following settings need to be edited:
- Change
iface eth0 inet dhcp
(default) toiface eth0 inet static
(static) dns-nameservers
can have multiple values, such as using public DNS services like8.8.8.8
,1.1.1.1
gateway
is the IP address of the router or firewallnetmask
is usually 255.255.255.0 (/24), adjust according to your own network parametersaddress
is the IP address of the local machine
After saving the interfaces
settings, edit /etc/resolv.conf
:
|
|
Use one of the following commands to restart the network and apply all settings:
|
|
|
|
Setting a static IP address in Ubuntu (newer versions): |
Starting from Ubuntu 17.10, Netplan is the default network management tool.
Network configuration files are stored in *.yaml
files in the directory /etc/netplan/
.
In my case, it is /etc/netplan/00-installer-config.yaml
, and the default content is:
|
|
Edit the 00-installer-config.yaml
configuration file using nano or vim:
|
|
You need to edit the following settings:
renderer: networkd
specifies that this interface is managed by thesystemd-networkd
servicedhcp4: no
changes to manual static IP address setupaddresses
the local IP address and network segmentnameservers
DNS serveraddresses
[8.8.8.8,1.1.1.1] can be multiple IP addresses
Save the 00-installer-config.yaml
settings.
Apply the network settings using the following command:
|
|
Setting up VLAN in Ubuntu: |
In this example, N1 is flashed with Armbian, and the network port is connected to the trunk port of the switch.
Dynamically obtain an IP address
The network configuration files are stored in *.yaml
files in the /etc/netplan/
directory.
The default content of /etc/netplan/armbian-default.yaml
is:
|
|
Edit the /etc/netplan/armbian-default.yaml
configuration file using nano or vim:
|
|
The configuration file is written in YAML format, and various settings are included:
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” section, the VLAN (Virtual Local Area Network) interface on the “eth0” network interface is configured. The VLAN ID is 3 and is connected to the physical interface “eth0” via the “link” field. The VLAN interface is also configured to obtain an IPv4 address via DHCP.
This configuration file sets up a network connection where both the physical interface “eth0” and the VLAN interface obtain dynamic IP addresses via DHCP.
Set static IP address
|
|
The VLAN configuration sets up a new interface vlan.3 with ID 3 associated with eth0. Its dhcp4 is set to no, with the static IP address 10.3.3.3 and subnet mask 24. The default route is specified through IP address 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 default network interface name of the machine, use the following command:
|
|
eth0
Edit the network interface via text editor
|
|
i. As DHCP:
|
|
Then restart the networking service:
|
|
i. As Static:
|
|
Then restart the networking service:
|
|
With VLAN (Advanced Example)
Make sure the vlan package is installed on the system:
|
|
If the host is a hypervisor consider adding below sysctl
configurations:
|
|
Load configurations:
|
|
net.ipv4.ip_forward = 1 net.ipv4.conf.all.arp_filter = 0 net.ipv4.conf.all.rp_filter = 2
Then modify interfaces configurations:
|
|
The interface content is as follow:
|
|
Then restart the networking service:
|
|
Conclusion: |
Each Linux distribution may have different methods for setting a static IP address, but the process is generally similar.
Reference: |