Ubuntu Server18.04 开机自动连接wifi

2021-03-03 22:56:39

首先确保你的网卡已被驱动, 使用ip addr 或者 ifconfig查看网卡名称,我的是wlan0

还需确保你有wpa_supplicant程序, 用于生成wifi配置文件, 这个程序在可以通过安装wpasupplicant获取

进入你想保存配置文件的路径, 执行命令,生成wifi配置文件

wpa_passphrase PDCN 12345678 > wlan.conf

执行命令,拷贝自动连接服务配置文件

sudo cp /lib/systemd/system/wpa_supplicant.service /etc/systemd/system/wpa_supplicant.service
编辑
sudo vim/etc/systemd/system/wpa_supplicant.service

修改这一行: ExecStart=/sbin/wpa_supplicant -u -s -O /run/wpa_supplicant

ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wlan.conf -i wlan0
在这一行下面添加, 启用断开重连
Restart=always

注释服务别名(If you can find the following line in this file, comment it out (Add the # character at the beginning of the line))

#Alias=dbus-fi.w1.wpa_supplicant1.service

使配置生效,启动服务

sudo systemctl daemon-reload
sudo systemctl enable wpa_supplicant.service

新增自动获取ip服务

sudo vim /etc/systemd/system/dhclient.service

填入

[Unit]
Description= DHCP Client
Before=network.target
After=wpa_supplicant.service

[Service]
Type=forking
ExecStart=/sbin/dhclient wlan0 -v
ExecStop=/sbin/dhclient wlan0 -r
Restart=always
 
[Install]
WantedBy=multi-user.target

启用它

sudo systemctl enable dhclient.service

重启享受!!


参考文档

https://www.linuxbabe.com/ubuntu/connect-to-wi-fi-from-terminal-on-ubuntu-18-04-19-04-with-wpa-supplicant

https://www.cnblogs.com/milton/p/10133850.html

https://blog.csdn.net/u012503786/article/details/79541811


作者 zxq, 发表于 2021-03-03 22:56:39, 最后修改于2021-03-05 22:11:48