树莓派3B+ubunt server配置WiFi

基于树莓派的ubuntu server系统默认是没有开启WiFi的,如果想要在ubuntu下使用树莓派的WiFi功能就必须要手动配置。

  1. 安装iwconfig工具和net-tools
sudo apt install wireless-tools
sudo apt-get install net-tools
  1. 启动树莓派的无线网卡,网卡名称默认为wlan0
sudo iwconfig wlan0 power on
sudo ifconfig wlan0 up
  1. 配置树莓派需要连接的路由器名称和密码
sudo iwconfig wlan0 essid "无线名称"
sudo su
wpa_passphrase 无线名称 密码 > /etc/wpa_supplicant/wpa_supplicant.conf
  1. 以上步骤操作完成后,/etc/wpa_supplicant/wpa_supplicant.conf的内容如下
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
network={
        ssid="ap"
        #psk="12345678"
        psk=37f0aab65e8664c4524c3966ae54ca1ecf2266682db8753714d6d03040e1a267
}
~

ssid表示无线的名称,psk表示无线的密码,无线密码有两种表示方式,一种是明文的密码,一种是加密后的十六进制数据,两种表示方式均可
5. 手动配置使配置生效

sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
sudo dhclient wlan0
  1. 使用ifconfig命令查看wlan0是否获取到ip地址
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.150  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::ba27:ebff:feb0:2c69  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:b0:2c:69  txqueuelen 1000  (Ethernet)
        RX packets 3  bytes 1180 (1.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 1580 (1.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  1. 接下来需要将上面的配置设置为开机启动,ubuntu在/etc目录下是没有rc.local文件的,我们需要创建它并写入如下内容:
#!/bin/bash
ip link set wlan0 up &&
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf &&
dhclient wlan0
exit 0
  1. 修改rc.local为可执行:
chmod a+x /etc/rc.local

此时如果我们执行systemctl enable rc-local.service命令会报如下错误:

The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
 
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.
  1. 我们需要修改etc/systemd/system目录下的rc-local.service文件,添加最后面两行:
#  SPDX-License-Identifier: LGPL-2.1+
# 
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
  1. 使能rc-local服务
sudo systemctl enable rc-local.service

执行成功后会有提示:

Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /lib/systemd/system/rc-local.service.

如果没有提示说明之前已经启动过该服务。
11. 设置开机启动

sudo systemctl daemon-reload
sudo systemctl start rc-local.service

第一条命令如果不执行可能出现如下错误:

Warning: The unit file, source configuration file or drop-ins of rc-local.service changed on disk. Run 'sys>
● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: failed (Result: exit-code) since Wed 2021-05-26 11:45:53 UTC; 5min ago
       Docs: man:systemd-rc-local-generator(8)
    Process: 2241 ExecStart=/etc/rc.local start (code=exited, status=203/EXEC)

May 26 11:45:53 ubuntu systemd[1]: Starting /etc/rc.local Compatibility...
May 26 11:45:53 ubuntu systemd[2241]: rc-local.service: Failed to execute command: Exec format error
May 26 11:45:53 ubuntu systemd[2241]: rc-local.service: Failed at step EXEC spawning /etc/rc.local: Exec fo>
May 26 11:45:53 ubuntu systemd[1]: rc-local.service: Control process exited, code=exited, status=203/EXEC
May 26 11:45:53 ubuntu systemd[1]: rc-local.service: Failed with result 'exit-code'.
May 26 11:45:53 ubuntu systemd[1]: Failed to start /etc/rc.local Compatibility.
  1. 查看自启动设置是否成功
sudo systemctl status rc-local.service

成功后会出现如下内容:

● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: active (exited) since Wed 2021-05-26 12:05:56 UTC; 2min 8s ago
       Docs: man:systemd-rc-local-generator(8)
      Tasks: 0 (limit: 973)
     CGroup: /system.slice/rc-local.service

May 26 12:05:56 ubuntu systemd[1]: Starting /etc/rc.local Compatibility...
May 26 12:05:56 ubuntu rc.local[1694]: Successfully initialized wpa_supplicant
May 26 12:05:56 ubuntu rc.local[1694]: Failed to open config file '/etc/wpa_supplicant.conf', error: No suc>
May 26 12:05:56 ubuntu rc.local[1694]: Failed to read or parse configuration '/etc/wpa_supplicant.conf'.
May 26 12:05:56 ubuntu systemd[1]: Started /etc/rc.local Compatibility.

猜你喜欢

转载自blog.csdn.net/weixin_39270987/article/details/117368034