PCHIFI.ORG

 找回密码
 立即注册
搜索
查看: 4997|回复: 0

ubuntu20服务器版在命令行netplan配置无线网卡

[复制链接]

58

主题

115

帖子

379

积分

版主

Rank: 7Rank: 7Rank: 7

积分
379
发表于 2021-8-18 08:50:58 | 显示全部楼层 |阅读模式
对于无图形界面的服务器版Server,使用netplan配置网络可以在重启后仍然能够正常启动配置并获取IP地址。
有2个步骤,修改/etc/netplan/目录下的.yaml配置文件,并用netplan apply生效。
配置难点在于配置文件的格式。man netplan有很长的说明,功能强大,也有例子,但不好看懂。

在配置wifi无线网卡之前,首先查看下网络。
xxm@ubuntu20:/etc/netplan# ip ad
用以下命令尝试修改并测试配置文件,只是缩进不同,如出现报错,多半是缩进问题:
xxm@ubuntu20:/etc/netplan# sudo netplan try --debug
xxm@ubuntu20:/etc/netplan# sudo netplan generate

设置固定IP地址并设置自动获取IP地址的无线网络
xxm@ubuntu20:/etc/netplan# sudo nano 00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    enp1s0:
      addresses: [192.168.1.6/24]
      dhcp4: false
      optional: true
      gateway4: 192.168.1.1
      nameservers:
        addresses: [114.114.114.114]
  version: 2
  renderer: NetworkManager
  wifis:
    wlx1cbfce9fddce:
      dhcp4: true
      access-points:
        "Your wifi name":
          password: "Your wifi password"

设置自动获取IP地址的有线和无线网络
xxm@ubuntu20:/etc/netplan# sudo nano 00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
    ethernets:
        enp1s0:
            dhcp4: true
            optional: true
    wifis:
        wlx1cbfce9fddce:
            dhcp4: true
            optional: true
            access-points:
                "Your wifi name":
                      password: "Your wifi password"
    version: 2

xxm@ubuntu20:/etc/netplan# sudo netplan try --debug
Warning: Stopping systemd-networkd.service, but it can still be activated by:
  systemd-networkd.socket
Do you want to keep these settings?
Press ENTER before the timeout to accept the new configuration         //这里按回车接受新的配置

Changes will revert in 117 seconds
Configuration accepted.

配置好之后
xxm@ubuntu20:/etc/netplan# sudo ip ad
可以列出正确配置好的网络

剖析一下netplan:
xxm@ubuntu20:~# sudo which netplan
/usr/sbin/netplan
xxm@ubuntu20:~# ll /usr/sbin/netplan
lrwxrwxrwx 1 root root 31 Jan  8  2021 /usr/sbin/netplan -> ../share/netplan/netplan.script*
xxm@ubuntu20:~# file /usr/share/netplan/netplan.script
/usr/share/netplan/netplan.script: Python script, ASCII text executable
xxm@ubuntu20:~# netplan -h
usage: /usr/sbin/netplan  [-h] [--debug]  ...
Network configuration in YAML
optional arguments:
  -h, --help  show this help message and exit
  --debug     Enable debug messages
Available commands:
    help      Show this help message
    apply     Apply current netplan config to running system
    generate  Generate backend specific configuration files from /etc/netplan/*.yaml
    get       Get a setting by specifying a nested key like "ethernets.eth0.addresses", or "all"
    info      Show available features
    ip        Retrieve IP information from the system
    set       Add new setting by specifying a dotted key=value pair like ethernets.eth0.dhcp4=true
    try       Try to apply a new netplan config to running system, with automatic rollback

总结:
ubuntu20.04服务器版(仅命令行shell)的网络服务是systemd-networkd 。
配置文件是/etc/netplan/50-cloud-init.yaml 该文件遵循yaml语法,必须用空格缩进,不能用tab。
netplan命令是一个python脚本。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|PCHIFI.ORG

GMT+8, 2026-6-2 10:56 , Processed in 0.041357 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表