Configuring a wireless network adapter on Ubuntu

Connecting to a WiFi network from the Ubuntu command-line terminal, without a graphical interface.

I installed Ubuntu Server on an older machine at the office. The office only has a WiFi network, no Ethernet, and the computer does not have a wireless chip built-in. I bought a D-Link WiFi USB adapter and plugged it in. Great, but how to tell Ubuntu to use the newly installed wireless adapter via the terminal, without a GUI?

Let’s configure the wireless adapter via the command-line interface. Useful for computers without a GUI like Ubuntu Server, but this will also work for say, a Raspberry Pi over SSH or any other Linux machine.

Detect the Wireless USB Adapter

Check if the machine detected the USB adapter first. Run lsusb to list the USB devices the computer knows about:

  $ lsusb
  Bus 002 Device 003: Fitipower Integrated Technology Inc
  Bus 002 Device 002: Intel Corp. Integrated Rate Hub
  Bus 002 Device 001: Linux Foundation 2.0 root hub
  Bus 001 Device 004: D-Link Corp.
  

There it is, our D-Link Corp. wireless USB adapter.

Find the Adapter Name

Now we need to find the USB device’s logical name, the name we can use to refer to it. Meet lshw.

  $ lshw -C network
  *-network DISABLED
     description: Wireless interface
     physical id: 2
     bus info: usb@1:1.6
     logical name: wlan0
     capabilities: ethernet physical wireless
     configuration: broadcast=yes driver=rt2800usb driverversion=4.4.0-62-generic firmware=0.29 ip=x.x.x.x link=yes multicast=yes wireless=IEEE 802.11abgn
  

Look for a device with a logical name starting with a 'w' (for wireless) and take note of its name: ‘wlan0’. Notice the WiFi interface is currently disabled.

Manually Enable the Network Connection

We now have the USB device’s logical name and know how to address it. Let’s manually connect to a wireless network but make sure it’s turned on first:

  $ ifconfig wlan0 up
  

We use ‘wlan0’ as a generic device name here but remember to replace it with the logical name found in the previous step.

Time to connect to the wireless network, replace the SSID with your wireless network name. Use nmcli dev wifi for a list of available networks if you don’t know the network name.

  $ wpa_passphrase <ssid> > wlan.config
  

It might look like Ubuntu hangs but it’s waiting for you to type the password for the WiFi network. Enter the password and press enter, a ‘wlan.conf’ file will be created in the same directory with the network name and matching password. Use this file to connect to the network:

  $ wpa_supplicant -Dwext -i wlan0 -c wlan.conf -B
  

There won’t be a lot to do without an IP address so ask the router for one:

  $ dhclient -r
  $ dhclient wlan0
  

You are now connected to your local network or hotspot. Check if your Ubuntu box can reach out to the internet:

  $ ping 1.1.1.1
  PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
  64 bytes from 1.1.1.1: icmp_seq=1 ttl=42 time=16.4 ms
  

Persist the Configuration for Ubuntu

This is all fine… until you reboot and do the whole thing over again. It would be wise to store this somewhere so Ubuntu knows how to enable the network connection after a reboot. Add the following configuration settings to the /etc/network/interfaces file.

  auto lo
  iface lo inet loopback
  auto wlan0
  iface wlan0 inet dhcp
  wpa-ssid <ssid>
  wpa-psk <password>
  gateway 192.168.0.1
  dns-nameservers 1.1.1.1 192.168.0.1
  

Replace the wlan0, ssid, and password with your own and replace 192.168.0.1 with the IP address of the router. Save the config, disable the wireless interface, and turn it on once more.

  $ ifconfig wlan0 down
  $ ifconfig wlan0 up
  

Still connected? Great, the internet awaits you.

This post is open source. Did you spot a mistake? Ideas for improvements? Contribute to this post via Github. Thank you!