硬件:Raspberry Pi 4b
系统:Ubuntu 19.10
系统下载链接:https://ubuntu.com/download/iot/raspberry-pi
ssh [email protected]
#备份apt源文件
sudo cp -p /etc/apt/sources.list /etc/apt/sources.list.bak
#编辑apt源文件,更换为阿里源
sudo vim /etc/apt/sources.list
#将文件内容全部清除,将一下内容粘贴进去
deb http://mirrors.aliyun.com/ubuntu-ports eoan main restricted
deb http://mirrors.aliyun.com/ubuntu-ports eoan-updates main restricted
deb http://mirrors.aliyun.com/ubuntu-ports eoan universe
deb http://mirrors.aliyun.com/ubuntu-ports eoan-updates universe
deb http://mirrors.aliyun.com/ubuntu-ports eoan multiverse
deb http://mirrors.aliyun.com/ubuntu-ports eoan-updates multiverse
deb http://mirrors.aliyun.com/ubuntu-ports eoan-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports eoan-security main restricted
deb http://mirrors.aliyun.com/ubuntu-ports eoan-security universe
deb http://mirrors.aliyun.com/ubuntu-ports eoan-security multiverse
#保存退出vim后执行更新
sudo apt-get update
或者直接使用替换指令
#进入vim编辑界面
sudo vim /etc/apt/sources.list
#在命令模式下执行以下指令
:1,$s/ports.ubuntu.com\/ubuntu-ports/mirrors.aliyun.com\/ubuntu-ports\//g
#保存退出vim后执行更新
sudo apt-get update
iwconfig
一般而言,初次设置,无线网卡的名字为wlan0
sudo ip link set wlan0 up
#将扫描结果存储到文件
sudo iw dev wlan0 scan > wireless.txt
#打开文件在命令模式下查找有没有你要连接的网络
vim wireless.txt
:/awk-panda
sudo iw dev wlan0 connect awk-panda key d:0:12345678
其中d:0:12345678中d代表默认选项,0代表第1个密码,12345678为wifi密码
iwconfig
sudo apt install wpasupplicant
wpa_passphrase awk-panda little..panda | sudo tee /etc/wpa_supplicant.conf
语法:tee [-ai][--help][--version][文件...]
参数:
-a或--append 附加到既有文件的后面,而非覆盖它.
-i或--ignore-interrupts 忽略中断信号。
--help 在线帮助。
--version 显示版本信息。
sudo wpa_supplicant -c /etc/wpa_supplicant.conf -i wlan0
然后iwconfig查看连接状态
wpa_supplicant命令:连接、配置WIFI的工具
常用参数:
-B 后台运行
-c 配置文件
-i 网络设备
因为上述连接wifi的语句会占据控制台,为了后台执行,可以在命令中添加 -B 选项
sudo wpa_supplicant -B -c /etc/wpa_supplicant.conf -i wlan0
sudo dhclient wlan0
ip addr show wlan0
#拷贝wpa_supplicant.service文件
sudo cp /lib/systemd/system/wpa_supplicant.service /etc/systemd/system/wpa_supplicant.service
#编辑service文件
sudo vim /etc/systemd/system/wpa_supplicant.service
#编辑以下内容
ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wpa_supplicant.conf - i wlan0
#删除开机启动默认配置
sudo systemctl disable wpa_supplicant.service
#设置wpa_supplicant服务为开机启动
sudo systemctl enable wpa_supplicant.service
#为了保证开机就能获取私有IP,需要设置和开机启动dhclieng服务
sudo vim /etc/systemd/system/dhclient.service
#添加以下内容
[Unit]
Description= DHCP Client
Before=network.target
After=wpa_supplicant.service
[Service]
Type=simple
ExecStart=sudo /sbin/dhclient wlan0 -v
[Install]
WantedBy=multi-user.target
#保存后设置服务为开机启动
sudo systemctl enable dhclient.service
#打开dhclient配置文件
sudo vim /etc/dhcp/dhclient.conf
#在文件最后添加以下内容
interface "wlan0" {
send dhcp-requested-address 192.168.3.80;
}
#保存后重启dhclient服务
sudo systemctl restart dhclient.service
sudo reboot