在Ubuntu 17.10 中 引入了一种新的命令行配置实用工具。它实用YAML格式来配置和描述网络设置。
Ubuntu 18.04 默认使用该工具。
关于YAML格式,跟json和xml类似,语法是不能用TAB(切记),空格代表层级,key:value “:”后面加一个英文空格,区分大小写。这几个是入门容易犯的错误。wtf ,这是一种倒退吗。简单了解请点击下面:
YAML 语言教程
想学习netplan的,可以去官网(好的文档都是英文的)
https://netplan.io/
使用netplan 搞定所有网络配置,简单粗暴
(1)编辑 /etc/netplan/*yaml文件
sudo vim /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by
# the datasource. Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
#renderer:NetworkManager
ethernets:
ens33:
addresses: [192.168.21.131/24]
dhcp4: no
gateway4: 192.168.21.2
#optional: true
nameservers:
addresses: [127.0.0.53, 202.106.0.20]
search: [localdomain]
optional: true
version: 2
~
我的是VMware的虚拟机下环境
addresses: 是一个数组,使用,号分开可以配置多个IP,一般一个就够了。格式[0.0.0.0/24,1.1.1.1/26]还可以使用-0.0.0./24
dhcp4:true 使用dhcp动态获取 这里使用no 试了false 不可以 ,难道true的反义词不是 false 是no
gateway4: ipv4网管
nameservers: dns 配置 这里的addresses 前面需要空格,表示nameservers的子项
addresses:[127.0.0.53, 202.106.0.20] // dns地址
search:[localdomain] //虚拟机所在的domain
(2)立即生效
sudo netplan apply
(3)ping 测试
ping www.baidu.com 看是否可以ping通
ubuntu@s1:~$ ping baidu.com
ping: baidu.com: Name or service not known
ubuntu@s1:~$ ping www.baidu.com
PING www.a.shifen.com (119.75.216.20) 56(84) bytes of data.
64 bytes from 127.0.0.1 (119.75.216.20): icmp_seq=1 ttl=128 time=4.71 ms
64 bytes from 127.0.0.1 (119.75.216.20): icmp_seq=2 ttl=128 time=4.75 ms
64 bytes from 127.0.0.1 (119.75.216.20): icmp_seq=3 ttl=128 time=5.36 ms
64 bytes from 127.0.0.1 (119.75.216.20): icmp_seq=5 ttl=128 time=6.87 ms
64 bytes from 127.0.0.1 (119.75.216.20): icmp_seq=6 ttl=128 time=7.90 ms
64 bytes from 127.0.0.1 (119.75.216.20): icmp_seq=7 ttl=128 time=6.19 ms
64 bytes from 127.0.0.1 (119.75.216.20): icmp_seq=8 ttl=128 time=8.19 ms
^C
--- www.a.shifen.com ping statistics ---
8 packets transmitted, 7 received, 12% packet loss, time 7024ms
rtt min/avg/max/mdev = 4.715/6.286/8.190/1.324 ms
ping baidu.com 居然不行 ,好扎心 。
另外这里用到了一些网络知识,简单说一下
(1)ifconfig查看IP地址 和子网掩码
192.168.21.131/24 这个24 就是 255.255.255.0 的子网掩码
ubuntu@s1:~$ ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.21.131 netmask 255.255.255.0 broadcast 192.168.21.255
inet6 fe80::20c:29ff:fe9f:70a prefixlen 64 scopeid 0x20
ether 00:0c:29:9f:07:0a txqueuelen 1000 (Ethernet)
RX packets 64989 bytes 5945447 (5.9 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2997 bytes 410154 (410.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 834083 bytes 53476526 (53.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 834083 bytes 53476526 (53.4 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
(2)dns查看
cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
# 上面这句话 说明 这个配置文件 由系统配置,不能修改,使用netplan配置。
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
search localdomain
(3)查看网关
使用 netstat -rn 或者 route -n
ubuntu@s1:~$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.21.2 0.0.0.0 UG 0 0 0 ens33
192.168.21.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
ubuntu@s1:~$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.21.2 0.0.0.0 UG 0 0 0 ens33
192.168.21.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
ubuntu@s1:~$
0.0.0.0 对应的 gateway就是网关
对应 gateway4的配置