600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > linux server设置开机自动连接WIFI

linux server设置开机自动连接WIFI

时间:2020-10-31 13:55:40

相关推荐

linux server设置开机自动连接WIFI

1.前言

之前买了一个工控机,装过几个OS(linux 发行版),但是一直没有细研究过流程,只是停留在能用就不管了,工控机自带无线网卡(和俩个有线网口),所以这篇文章好好介绍如何开机自动连接WIFI(无图形化界面)。

2.安装软件

首先系统我安装的是 ubuntu 18.04 server版 ,安装过程不必赘述。

要连接家里的wifi,首先要知道家里wifi的加密模式,是否为WEP模式。

无密码/WEP模式可直接使用以下命令连接:

sudo iw dev wlan0 connect [网络 SSID]

sudo iw dev wlan0 connect [网络 SSID] key 0:[WEP 密钥]

如果是WPA 或WPA2 需要安装wpasupplicant软件包

sudo apt install wpasupplicant

3.生成配置文件

sudo wpa_passphrase [网络SSID] [KEY秘钥] > /etc/wpa_supplicant.conf

4.连接WIFI

wpa_supplicant -s -i wlp3s0 -D nl80211,wext -c /etc/wpa_supplicant.conf

看到很多文章说 加&放到后台执行,其实wpa_supplicant-B参数就是放到后台去执行。

动态获取ip地址:

dhclinet

验证是否可以连接网络:

curl

4.开机自动连接WIFI

修改/etc/network/interfaces文件:

# ifupdown has been replaced by netplan(5) on this system. See# /etc/netplan for current configuration.# To re-enable ifupdown on this system, you can run:# sudo apt install ifupdownauto wlp3s0iface wlp3s0 inet dhcpwpa-conf /etc/wpa_supplicant.conf

可以看到这个配置文件前面说明了ifupdown工具已经被netplan替换掉了,如果使用ifupdown这个工具就需要安装一下:

sudo apt install ifupdown

ifupifdown命令会读取/etc/network/interfaces进行设置:

ifup wlp3s0 #启用wlp3s0 并连接wifi

ifdown wlp3s0 #关闭wlp3s0

ifupifdown命令也是通过服务来控制的:(/etc/systemd/system/network-online.target.wants/networking.service)

[Unit]Description=Raise network interfacesDocumentation=man:interfaces(5)DefaultDependencies=noWants=network.targetAfter=local-fs.target network-pre.target apparmor.service systemd-sysctl.service systemd-modules-load.serviceBefore=network.target shutdown.target network-online.targetConflicts=shutdown.target[Install]WantedBy=multi-user.targetWantedBy=network-online.target[Service]Type=oneshotEnvironmentFile=-/etc/default/networkingExecStartPre=-/bin/sh -c '[ "$CONFIGURE_INTERFACES" != "no" ] && [ -n "$(ifquery --read-environment --list --exclude=lo)" ] && udevadm settle'ExecStart=/sbin/ifup -a --read-environmentExecStop=/sbin/ifdown -a --read-environment --exclude=loRemainAfterExit=trueTimeoutStartSec=5min

networking这个服务是开机自启服务,每次开机就会执行/sbin/ifup -a --read-environment,从而连接wifi。

5. 其它问题

A start job is running for wait for network to be Configured开机卡住2分钟左右。

很多文章也说过,修改/etc/systemd/system/network-online.target.wants/systemd-networkd-wait-online.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.[Unit]Description=Wait for Network to be ConfiguredDocumentation=man:systemd-networkd-wait-online.service(8)DefaultDependencies=noConflicts=shutdown.targetRequires=systemd-networkd.serviceAfter=systemd-networkd.serviceBefore=network-online.target shutdown.target[Service]Type=oneshotExecStart=/lib/systemd/systemd-networkd-wait-onlineRemainAfterExit=yesTimeoutStartSec=2sec #其它文章加入的[Install]WantedBy=network-online.target

简单来说 systemd-networkd-wait-online 这个服务可以检查所有网络接口网络是否处于就绪状态,如果没就绪就会阻塞住,但是直接设置服务的 timeout时间并不是好的做法。

我们简单执行一下/lib/systemd/systemd-networkd-wait-online这个命令:

root@ubuntu:~# /lib/systemd/systemd-networkd-wait-online --helpsystemd-networkd-wait-online [OPTIONS...]Block until network is configured.-h --help Show this help--version Print version string-q --quietDo not show status information-i --interface=INTERFACE Block until at least these interfaces have appeared--ignore=INTERFACEDon't take these interfaces into account--timeout=SECS Maximum time to wait for network connectivityroot@ubuntu:~#

可以看到,这个命令是可以带有一些参数的,-i判断指定的网络接口,不必判断所有的网络接口,所以这地方我只有指定我连接wifi的网络接口就合理了:

ExecStart=/lib/systemd/systemd-networkd-wait-online -i wlp3s0

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。