首页 > 系统相关 >Linux server设置开机自动连接WIFI

Linux server设置开机自动连接WIFI

时间:2022-11-27 18:35:21浏览次数:49  
标签:systemd network -- networkd WIFI server online Linux target

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 www.baidu.com

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 ifupdown
auto wlp3s0
iface wlp3s0 inet dhcp
wpa-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 interfaces
Documentation=man:interfaces(5)
DefaultDependencies=no
Wants=network.target
After=local-fs.target network-pre.target apparmor.service systemd-sysctl.service systemd-modules-load.service
Before=network.target shutdown.target network-online.target
Conflicts=shutdown.target

[Install]
WantedBy=multi-user.target
WantedBy=network-online.target

[Service]
Type=oneshot
EnvironmentFile=-/etc/default/networking
ExecStartPre=-/bin/sh -c '[ "$CONFIGURE_INTERFACES" != "no" ] && [ -n "$(ifquery --read-environment --list --exclude=lo)" ] && udevadm settle'
ExecStart=/sbin/ifup -a --read-environment
ExecStop=/sbin/ifdown -a --read-environment --exclude=lo
RemainAfterExit=true
TimeoutStartSec=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 Configured
Documentation=man:systemd-networkd-wait-online.service(8)
DefaultDependencies=no
Conflicts=shutdown.target
Requires=systemd-networkd.service
After=systemd-networkd.service
Before=network-online.target shutdown.target

[Service]
Type=oneshot
ExecStart=/lib/systemd/systemd-networkd-wait-online
RemainAfterExit=yes
TimeoutStartSec=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 --help
systemd-networkd-wait-online [OPTIONS...]

Block until network is configured.

  -h --help                 Show this help
     --version              Print version string
  -q --quiet                Do not show status information
  -i --interface=INTERFACE  Block until at least these interfaces have appeared
     --ignore=INTERFACE     Don't take these interfaces into account
     --timeout=SECS         Maximum time to wait for network connectivity
root@ubuntu:~# 

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

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

标签:systemd,network,--,networkd,WIFI,server,online,Linux,target
From: https://blog.51cto.com/u_13589616/5890241

相关文章

  • Eureka Server配置-集群版
    ①建modulecloud-eureka-server7001cloud-eureka-server7002②改pom<!--eureka-server--><dependency><groupId>org.springframework.cloud</groupId><arti......
  • Eureka Server配置-单机版
    ①建modulecloud-eureka-server7001②改pom<!--eureka-server--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-star......
  • kalilinux开启端口、关闭防火墙方法
    一、首先需要安装ufw命令apt-getinstallufw二、ufw命令使用实例如下:检查防火墙的状态(默认inactive)ufwstatus防火墙版本ufwversion启动ufw防火墙ufw......
  • linux CentOS 6.5 中安装与配置JDK-7
    系统环境:centos-6.5安装方式:rpm安装软件:jdk-7-linux-i586.rpm下载地址:​​http://www.oracle.com/technetwork/java/javase/downloads/index.ht......
  • 使用数字证书登陆阿里云Linux
    我们在购买并配置好阿里云linux服务器后,怎么登录云服务器。阿里云控制台提供了在线登录服务器的几种方式,但是个人感觉在网页中登录服务器不是很好的选择。以下看下我们从w......
  • 使用 Linux 命令 curl 和 telnet 测试接口连通性
    摘要:接口可用性诊断利器curl和Telnet。综述  Linux中的命令curl是利用URL语法在命令行模式下工作的开源文件传输工具,它可以被用于测试API接口,查看响应头和发出HTT......
  • Linux面试题3:Linux零拷贝技术
    zero-copy技术Linux网络IO数据传输过程图整个操作过程中,做了四次用户态和内核态的状态切换,数据从网卡copy到内核缓冲区,再从内核缓冲区copy到user-space;写入时从user-spa......
  • linux卸载redis
    linux卸载redis的方法:1、打开终端命令行模式;2、输入以下命令查看reids是否在运行;3、将redis-server服务停止;4、删除/usr/local/lib目录下与redis相关的文件;5、删除掉......
  • 适用于 Linux 的 Windows 子系统(WSL)安装指南
    (目录)WindowsSubsystemforLinuxWSL提供了一个微软开发的Linux兼容内核接口(不包含Linux代码),来自Ubuntu的用户模式二进制文件在其上运行。简单来说就是用Linux系统去......
  • 观察者模式(Observer)
    多个观察者关注同一个目标,当目标发生改变时,观察者们可以收到消息。(立刻更新消息,或者想知道的时候更新消息。)实现:1、自定义观察者:观察者与目标有一致的状态属性,目标......