Ubuntu Server 20.04: Connect to WiFi from command line

In this tutorial, you will learn how to connect to WiFi from command line on Ubuntu using Netplan. While this method can be used on Ubuntu desktop systems with a GUI, it is especially useful if you’re running a headless Ubuntu 20.04 system like a Raspberry Pi or need to connect to Wifi on Ubuntu Server. Follow along with us below as we connect to a WiFi on command line via SSID and network key.

In this tutorial you will learn:

  • How to identify the name of your wireless network interface
  • How to configure Netplan to connect to wireless network (SSID)
Ubuntu Server 20.04: Connect to WiFi from command line
Ubuntu Server 20.04: Connect to WiFi from command line

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed Ubuntu 20.04 or upgraded Ubuntu 20.04 Focal Fossa
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Ubuntu 20.04: Connect to WiFi from command line with Netplan step by step instructions



  1. First step is to identify the name of your wireless network interface. To do so execute:
    $ ls /sys/class/net
    enp0s25  lo  wlp3s0
    

    Depending on your Ubuntu 20.04 system the wireless network interface name would be something like: wlan0 or like in this case it is wlp3s0.

  2. Next, navigate to the /etc/netplan directory and locate the appropriate Netplan configuration files. The configuration file might have a name such as 01-network-manager-all.yaml or 50-cloud-init.yaml.
    $ ls /etc/netplan/
    
  3. Edit the Netplan configuration file:
    $ sudoedit /etc/netplan/50-cloud-init.yaml
    

    and insert the following configuration stanza while replacing the SSID-NAME-HERE and PASSWORD-HERE with your SSID network name and password:

        wifis:
            wlan0:
                optional: true
                access-points:
                    "SSID-NAME-HERE":
                        password: "PASSWORD-HERE"
                dhcp4: true
    

    Make sure that the wifis block is aligned with the above ethernets or version block if present. The entire configuration file may look similar to the one below:

    # 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:
            wlp3s0:
                optional: true
                access-points:
                    "SSID-NAME-HERE":
                        password: "PASSWORD-HERE"
                dhcp4: true
    

    Ubuntu 20.04: Connect to wifi from command line with Netplan

    Ubuntu 20.04: Connect to wifi from command line with Netplan

    Alternatively, you may also wish to configure a static IP address to your wireless interface.

  4. Once ready, apply the changes and connect to your wireless interface by executing the bellow command:
    $ sudo netplan apply
    

    Alternatively, if you run into some issues execute:

    $ sudo netplan --debug apply
    
  5. If all went well you would be able to see your wireless adapter connected to the wireless network by executing the ip command:
    $ ip a
    

Closing Thoughts




In this tutorial, we saw how to connect to a wifi network via command line on Ubuntu. You now know how to connect your Ubuntu server to wifi or your Raspberry Pi.