ubuntu 22.04中配置静态IP
001、 系统
root@ubuntu01:/etc/netplan# lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.1 LTS Release: 22.04 Codename: jammy
002、网络模式,nat模式
003、配置静态IP, 修改配置文件:/etc/netplan/01-network-manager-all.yaml(提前对文件进行备份,防止出现意外)
修改文件内容为:
network: version: 2 #renderer: NetworkManager ethernets: ens32: #配置的网卡的名称, 此处的网卡一英语虚拟机一致,可以使用ifconfig命令查看 addresses: [192.168.74.129/24] #配置的静态ip地址和掩码 dhcp4: no #关闭DHCP,如果需要打开DHCP则写yes optional: true routes: - to: default via: 192.168.74.2 #网关地址, 此处NAT模式,最后的网关为2. nameservers: addresses: [114.114.114.114] #DNS服务器地址,多个DNS服务器地址需要用 英文
004、使配置生效
root@ubuntu01:/etc/netplan# netplan apply
005、查看IP,测试网络
root@ubuntu01:/etc/netplan# netplan apply ## 使配置生效 root@ubuntu01:/etc/netplan# ifconfig | head ## 查看ip ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.74.129 netmask 255.255.255.0 broadcast 192.168.74.255 inet6 fe80::58b6:20bb:5329:19cb prefixlen 64 scopeid 0x20<link> ether 00:0c:29:bf:59:5d txqueuelen 1000 (Ethernet) RX packets 429 bytes 272719 (272.7 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 282 bytes 29794 (29.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 root@ubuntu01:/etc/netplan# ping -c 3 www.baidu.com ## 测试网络的联通性 PING www.a.shifen.com (110.242.68.4) 56(84) bytes of data. 64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=1 ttl=128 time=15.9 ms 64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=2 ttl=128 time=13.6 ms 64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=3 ttl=128 time=12.9 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 12.916/14.147/15.924/1.287 ms root@ubuntu01:/etc/netplan#
参考:http://www.ppmy.cn/news/12650.html