ip
本文介绍的静态 ip
的配置方法适用于 Ubuntu
系统的 18.04
及其以上版本。
一般情况下的个人 PC
只会有一张网卡,但在服务器中可能存在多张网卡的情况,使用 ifconfig
命令查看对应 ip
的网络接口。
若提示未找到 ifconfig
命令则使用如下命令按装该工具。
imaginemiracle:~$ sudo apt-get install net-tools
查看需要修改的网卡,如下可以查到当前所有的网络接口信息,由于我的主机只有一块网卡,因此就只对它修改即可。
imaginemiracle:~$ ifconfig
imaginemiracle:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.253.129 netmask 255.255.255.0 broadcast 192.168.253.255
inet6 fe80::bbd9:a68d:6982:fa33 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b8:7a:f4 txqueuelen 1000 (Ethernet)
RX packets 11433 bytes 15280828 (15.2 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5919 bytes 391957 (391.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 911 bytes 68550 (68.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 911 bytes 68550 (68.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
其默认配置信息的路径如下。
imaginemiracle:~$ sudo vim /etc/netplan/01-network-manager-all.yaml
若为修改过的文件应该会是这样:
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
将其修改为静态 ip
的模式。
network:
ethernets:
ens33:
dhcp4: no
addresses: [192.168.1.10/24]
optional: true
gateway4: 192.168.1.1
nameservers:
addresses: [114.114.114.114,8.8.8.8]
version: 2
应用该配置
iamginemiracle:~$ sudo netplan apply
network:
ethernets:
ens33:
dhcp4: no
dhcp6: no
addresses:
- 192.168.0.10/24
routes:
- to: default
via: 192.168.0.1
nameservers:
addresses:
- 114.114.114.114
- 8.8.8.8
version: 2
renderer: networkd
应用该配置
iamginemiracle:~$ sudo netplan apply
若 Ubuntu22.04
应用 ubuntu20.04 /18.04
的配置来设置静态 IP
,当使用 netplan apply
则会得到如下警告。
imaginemiracle:~$ sudo netplan apply
** (generate:3591): WARNING **: 11:29:00.676: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.
** (process:3589): WARNING **: 11:29:00.788: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.