• 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 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
    
  • 相关阅读:
    ros2与windows入门教程-控制walking机器人移动
    shell小练习2
    两数乘积:输出1~100整数乱序列表中两数乘积是目标整数的最小下标对
    go语言使用grpc
    技术分享 | 接口自动化测试如何处理 Header cookie
    安卓APP源码和报告——学生信息管理系统
    Go : for 语句简单使用
    Ceph入门到精通-iptables 限制多个ip 的多个端口段访问
    【蓝桥杯专项】动态规划_背包问题合集(Java)
    吃鸡大作战:绝密攻略《揭秘吃鸡要领与细节,国际服全方位指南只在此!
  • 原文地址:https://www.cnblogs.com/Hellowshuo/p/16930106.html