根据系统情况选择不同的方法
要设置静态IP地址,你可以编辑/etc/network/interfaces
文件或使用netplan
工具来配置网络接口。下面我将为你提供两种方法。
方法一:使用/etc/network/interfaces
文件
-
使用文本编辑器(如
nano
或vim
)打开/etc/network/interfaces
文件:sudo nano /etc/network/interfaces
1 -
在文件中找到你要配置静态IP的网络接口(通常是
eth0
或enpXsY
,其中X和Y是数字),并将其配置为静态IP。例如:auto eth0 iface eth0 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1
1
2
3
4
5将
address
、netmask
和gateway
的值替换为你的网络配置。 -
保存并关闭文件。
-
重启网络服务以应用更改:
sudo systemctl restart systemd-networkd
1
方法二:使用netplan
工具
-
使用文本编辑器打开
/etc/netplan/
目录下的配置文件(通常是以.yaml
结尾的文件):sudo nano /etc/netplan/01-netcfg.yaml
1 -
在文件中找到你要配置静态IP的网络接口(通常是
eth0
或enpXsY
,其中X和Y是数字),并将其配置为静态IP。例如:
# `gateway4` has been deprecated, use default routes instead.
# See the 'Default routes' section of the documentation for more details.
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: [192.168.1.100/24]
# gateway4: 192.168.1.1
routes:
- to: default
via: 192.168.1.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
将addresses
和gateway4
的值替换为你的网络配置。
-
保存并关闭文件。
-
应用更改:
sudo netplan apply
1
无论你选择哪种方法,设置静态IP后,你的网络接口将使用指定的IP地址、子网掩码和网关。记得根据你的网络环境进行适当的配置。