In a headless installation you can connect to wifi networks using network-manager, iwctl etc. But wait first you need to install these, and for that you need internet.
I was stuck in this egg-chicken problem myself and then I got to know about Netplan and wpa_supplicant.
In this guide, we'll walk through two methods to connect your Linux server to Wi-Fi using only built-in tools: Netplan and wpa_supplicant.
Method 1: Netplan
Netplan is Ubuntu’s default network configuration system. You can define Wi-Fi networks in a YAML file and let the system manage them.
Step 1: Edit Netplan configuration file
sudo vim /etc/netplan/01-netcfg.yaml
The file name may vary. It will look something like this:
network:
version: 2
wifis:
# wifi interface
wlo1:
dhcp4: true
access-points:
"SSID_1":
auth:
key-management: "psk"
password: "password"
"SSID_2":
auth:
key-management: "psk"
password: "password2"
Replace wlo1
with your own wifi interface. Use ip link show
to find your interface.
Step 2: Apply the changes
sudo netplan apply
This will:
- Connect to the Wi-Fi
- Request a DHCP IP address
- Set up the default route
Step 3: Test your connection
ping google.com
Method 2: wpa_supplicant
wpa_supplicant is the backend used by most Linux systems to handle WPA/WPA2 Wi-Fi authentication. It connects your Wi-Fi interface to a protected wireless network using a configuration file.
Step 1: Generate a Secure Wi-Fi Config
wpa_passphrase "YourSSID" "YourPassword" > wpa.conf
This generated a config for your wifi. If you don't know the SSID, you can easily check it through some other device.
Step 2: Create the Configuration File
sudo vim /etc/wpa_supplicant/wpa_supplicant.conf
Copy the contents of wpa.conf
to this file.
Step 3: Start wpa_supplicant
sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
Replace wlan0
with your own wifi interface. Use ip link show
to find your interface.
Step 4: Test your connection
ping google.com
Troubleshooting
- Make sure the wifi interface is up by running
sudo ip link set wlo1 down
sudo ip link set wlo1 up
. - wpa_supplicant might interfere by creating p2p-dev-wlan0 and I found it useful to kill it's instances to resolve those conflicts.
sudo killall wpa_supplicant
.
Top comments (0)