Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Netplan to connect to WiFi in Linux terminal

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Netplan to connect WiFi in the Linux terminal". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Netplan to connect WiFi in the Linux terminal.

Netplan Abstract Network configuration Generator is a simple tool for configuring Linux networks. With Netplan, you only need to use a YAML file to describe what each network interface needs to be configured. According to this configuration description, Netplan can help you generate all the configurations you need, regardless of the underlying management tool you choose.

Connect to WiFi from the terminal in Ubuntu

When you use the Ubuntu desktop, it's easy to connect to WiFi because you have a graphical user interface and you can do this easily. But it's different when you use a Ubuntu server, because you can only use the command line.

Ubuntu uses the Netplan tool to easily configure the network. In Netplan, you can create a YAML file that contains a description of the network interface, and then generate all the required configurations with the help of the netplan command line tool.

Let's see how to connect to a wireless network from a terminal using Netplan.

Step 1: determine your wireless network interface name

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

Ls / sys/class/net

This should show all available network interfaces (Ethernet, WiFi, and loopback). The wireless network interface name begins with w and is usually named like wlanX or wlpxyz.

[email protected]: ~ $ls / sys/class/neteth0 lo wlan0

Write down this interface name. You will use it in the next step.

Step 2: edit the wifi interface details in the Netplan configuration file

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

If it is a Ubuntu server, you should have a 50-cloud-init.yaml file. If it is a desktop computer, it should be a 01-network-manager-all.yml file.

The Network Manager of your Linux desktop computer allows you to choose a wireless network. You can hard-code the WiFi access point in its configuration. This can help you in the case of automatic disconnection (such as hanging).

No matter which file it is, you can open it and edit it. I hope you are a little familiar with the Nano editor, because it is pre-installed with Ubuntu.

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

YAML files are very sensitive to spaces, indentation, and alignment. Instead of using tabs, use 4 spaces (or 2, whichever is already used in the YAML file) where you see the indentation.

Basically, you need to add the following lines, with the access point name (SSID) and password (usually) in quotation marks:

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

Again, keep the alignment I showed, otherwise the YAML file cannot be parsed and it will throw an error.

Your complete profile might 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 version: 2 wifis: wlan0: dhcp4: true optional: true access-points: "SSID_name": password: "WiFi_password"

I find it strange that although there is news that the change will not be retained after the instance is restarted, it still works.

Anyway, use this command to generate the configuration:

Sudo netplan generate

If you are lucky, you should connect to the Internet. Try ping a website or run the apt update command.

However, things may not go so well, and you may see some mistakes. If this is the case, try some extra steps.

Possible troubleshooting

When you use the netplan apply command, you may see errors like this in the output.

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 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 the wpa_supplicant service is not running. Run this command:

Sudo systemctl start wpa_supplicant

Run netplan apply again. If it can solve this problem, that would be great. Otherwise, shut down the Ubuntu system using the following command:

Shutdown now

Restart the Ubuntu system, log in and generate and run netplan apply again:

Sudo netplan generatesudo netplan apply

Warnings (instead of errors) may now be displayed. This is a warning, not a mistake. I checked the running systemd service and found that netplan-wpa-wlan0.service was already running. It's probably because it's already running, and netplan apply updates the configuration file (even if nothing changes), so a warning is displayed.

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 doesn't matter, you can check whether the network is working properly by running apt update.

Thank you for your reading, the above is the content of "how to use Netplan to connect WiFi in Linux terminals". After the study of this article, I believe you have a deeper understanding of how to use Netplan to connect WiFi in Linux terminals, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report