1. 查看wifi连接名
$ ls /sys/class/net
如果是ubuntu18或者20的话,该命令显示的应该有
eth0 lo wlan0
其中wlan0为wifi的连接名
2. 编辑配置文件
$ cd /etc/netplan
在该目录下,如果是服务器,则会有一个05-cloud-init.yaml的文件,如果是桌面环境,会有一个01-network-manager-all.yaml
这里以05-cloud-init.yaml为例,编辑文件:
注意文件的缩进格式,
network:
version: 2
renderer: NetworkManager
wifis:
wlan0:
dhcp4: true
optional: true
access-points:
"你的wifi名称":
password: "wifi密码"
看有的说还要安装wpasupplicant和network-manager,之前因为搞了那么就也没脸上,我也装了这个
$ sudo apt-get install wpasupplicant
$ sudo apt-get install network-manager
3. 使文件生效
然后执行如下命令:
$ sudo netplan generate
$ sudo netplan apply
使用ifconfig -a查看连接情况:
看到wlan0中的inet,如果出现ip信息,则表示连接成功
eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether xx:xx:xxx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
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 208 bytes 16100 (16.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 208 bytes 16100 (16.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.192 netmask 255.255.255.0 broadcast 192.168.2.255
inet6 xxxx:xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x0<global>
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:f6:ea:8a txqueuelen 1000 (Ethernet)
RX packets 654 bytes 81544 (81.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 574 bytes 100354 (100.3 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0