How to set a static IP address in Linux

The steps to set a static IP address in Linux are as follows:

1. Confirm the network card name

Use the command ifconfigor ip addrto view the name of the network card in the current system, which usually starts with ethor enp, enssuch as eth0or enp3s0.

2. Edit the network configuration file

Open the file with an editor /etc/sysconfig/network-scripts/ifcfg-<网卡名称>, eg /etc/sysconfig/network-scripts/ifcfg-eth0. If the file does not exist, it needs to be created manually.

Add the following to that file:

BOOTPROTO=static   # 静态 IP 地址
IPADDR=<IP 地址>   # 设置 IP 地址
NETMASK=<子网掩码> # 设置子网掩码
GATEWAY=<网关地址> # 设置网关地址
DNS1=<DNS 服务器1>  # 设置 DNS 服务器1
DNS2=<DNS 服务器2>  # 设置 DNS 服务器2

Note that the specific values ​​of the above parameters need to be set according to the actual situation.

3. Restart the network service

Execute the following command to restart the network service:

systemctl restart network

 4. Verify the IP address

Use the command ifconfigor ip addrto verify whether the set static IP address takes effect.

In addition, if you need to set the IP addresses of multiple network cards, you need to create multiple /etc/sysconfig/network-scripts/ifcfg-<网卡名称>files and configure them separately.

In general, the above are the basic steps for setting a static IP address in Linux, which can be adjusted according to the actual situation.

Guess you like

Origin blog.csdn.net/weixin_42279822/article/details/130641851