2020-10-20-Headless 狀態(tài)ubuntu設(shè)置無線網(wǎng)卡

直接 copy 過來抡草,https://itsfoss.com/connect-wifi-terminal-ubuntu/

Step 1: Identify your wireless network interface name

There are several ways to identify your network interface name. You can use the ip command, the deprecated ipconfig command or check this file:

ls /sys/class/net

This should give you all the available networking interface (Ethernet, wifi and loopback). The wireless network interface name starts with ‘w’ and it is usually named similar to wlanX, wlpxyz.

abhishek@itsfoss:~$ ls /sys/class/net
eth0 lo wlan0

Take a note of this interface name. You’ll use it in the next step.

Step 2: Edit the Netplan configuration file with the wifi interface details

The Netplan configuration file resides in /etc/netplan directory. If you check the contents of this directory, you should see files like 01-network-manager-all.yml or 50-cloud-init.yaml.

If it is Ubuntu server, you should have cloud-init file. For desktops, it should be network-manager file.

The Network Manager on the Linux desktop allows you to choose a wireless network. You may hard code the wifi access point in its configuration. This could help you in some cases (like suspend) where connection drops automatically.

Whichever file it is, open it for editing. I hope you are a tad bit familiar with Nano editor because Ubuntu comes pre-installed with it.

sudo nano /etc/netplan/50-cloud-init.yaml

YAML files are very sensitive about spaces, indention and alignment. Don’t use tabs, use 4 (or 2, whichever is already used in the YAML file) spaces instead where you see an indention.

Basically, you’ll have to add the following lines with the access point name (SSID) and its password (usually) in quotes:

wifis:
    wlan0:
        dhcp4: true
        optional: true
        access-points:
            "SSID_name":
                password: "WiFi_password"

Again, keep the alignment as I have shown or else YAML file won’t be parsed and it will throw an error.

Your complete configuration file may look like this:

# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            dhcp4: true
            optional: true
    version: 2
    wifis:
        wlan0:
            dhcp4: true
            optional: true
            access-points:
                "SSID_name":
                    password: "WiFi_password"

I find it strange that despite the message that changes will not persist across an instance reboot, it still works.

Anyway, generate the configuration using this command:

sudo netplan generate

And now apply this:

sudo netplan apply

If you are lucky, you should have network connected. Try to ping a website or run apt update command.

However, things may not go as smooth and you may see some errors. Try some extra steps if that’s the case.

Possible troubleshooting

It is possible that when you use the netplan apply command, you see an error in the output that reads something like this:

Failed to start netplan-wpa-wlan0.service: Unit netplan-wpa-wlan0.service not found.
Traceback (most recent call last):
  File "/usr/sbin/netplan", line 23, in <module>
    netplan.main()
  File "/usr/share/netplan/netplan/cli/core.py", line 50, in main
    self.run_command()
  File "/usr/share/netplan/netplan/cli/utils.py", line 179, in run_command
    self.func()
  File "/usr/share/netplan/netplan/cli/commands/apply.py", line 46, in run
    self.run_command()
  File "/usr/share/netplan/netplan/cli/utils.py", line 179, in run_command
    self.func()
  File "/usr/share/netplan/netplan/cli/commands/apply.py", line 173, in command_apply
    utils.systemctl_networkd('start', sync=sync, extra_services=netplan_wpa)
  File "/usr/share/netplan/netplan/cli/utils.py", line 86, in systemctl_networkd
    subprocess.check_call(command)
  File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['systemctl', 'start', '--no-block', 'systemd-networkd.service', 'netplan-wpa-wlan0.service']' returned non-zero exit status 5.

It is possible that wpa_supplicant service is not running. Run this command:

sudo systemctl start wpa_supplicant

Run netplan apply once again. If it fixes the issue well and good. Otherwise, shutdown your Ubuntu system using:

shutdown now

Start your Ubuntu system again, log in and generate and apply netplan once again:

sudo netplan generate
sudo netplan apply

It may show warning (instead of error) now. It is warning and not an error. I checked the running systemd services and found that netplan-wpa-wlan0.service was already running. Probably it showed the warning because it was already running and ‘netplan apply’ updated the config file (even without any changes).

Warning: The unit file, source configuration file or drop-ins of netplan-wpa-wlan0.service changed on disk. Run 'systemctl daemon-reload' to reload units.

It is not crtical and you may check that the internet is probably working already by running apt update.

I hope you were able to connect to wifi using the command line in Ubuntu with the help of this tutorial. If you are still facing trouble with it, do let me know in the comment section.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末根竿,一起剝皮案震驚了整個(gè)濱河市瞻坝,隨后出現(xiàn)的幾起案子唠粥,更是在濱河造成了極大的恐慌灾茁,老刑警劉巖蝶柿,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脱茉,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡壕鹉,警方通過查閱死者的電腦和手機(jī)剃幌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晾浴,“玉大人负乡,你說我怎么就攤上這事〖够耍” “怎么了抖棘?”我有些...
    開封第一講書人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)狸涌。 經(jīng)常有香客問我切省,道長(zhǎng),這世上最難降的妖魔是什么帕胆? 我笑而不...
    開封第一講書人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任朝捆,我火速辦了婚禮,結(jié)果婚禮上懒豹,老公的妹妹穿的比我還像新娘芙盘。我一直安慰自己驯用,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開白布儒老。 她就那樣靜靜地躺著蝴乔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪贷盲。 梳的紋絲不亂的頭發(fā)上淘这,一...
    開封第一講書人閱讀 51,573評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音巩剖,去河邊找鬼铝穷。 笑死,一個(gè)胖子當(dāng)著我的面吹牛佳魔,可吹牛的內(nèi)容都是我干的曙聂。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼鞠鲜,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼宁脊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起贤姆,我...
    開封第一講書人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤榆苞,失蹤者是張志新(化名)和其女友劉穎任内,沒想到半個(gè)月后股缸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡职员,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年碧信,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了赊琳。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡砰碴,死狀恐怖躏筏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情呈枉,我是刑警寧澤趁尼,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站猖辫,受9級(jí)特大地震影響酥泞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜住册,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一婶博、第九天 我趴在偏房一處隱蔽的房頂上張望瓮具。 院中可真熱鬧荧飞,春花似錦凡人、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至耳幢,卻和暖如春岸晦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背睛藻。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工启上, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人店印。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓冈在,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親按摘。 傳聞我的和親對(duì)象是個(gè)殘疾皇子包券,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355