Managing WiFi and wireless connections in Linux can be easily accomplished from the terminal using a powerful tool called rfkill. Whether you want to enable or disable wireless devices like WiFi or Bluetooth, rfkill offers a simple and efficient way to do it without needing a graphical user interface.
In this blog post, we will walk you through how to use the rfkill command to manage WiFi from the Linux command line. We’ll explain it in simple language, providing examples so that even beginners can follow along.
What is rfkill?
rfkill is a command-line tool in Linux that provides an interface for enabling and disabling wireless communication devices, such as WiFi and Bluetooth, on your system. It interacts with the Linux kernel’s RF (Radio Frequency) device blocking subsystem to control these devices.
Key use cases for rfkill include:
- Disabling WiFi when you’re not using it to save power.
- Enabling WiFi after it has been disabled for any reason.
- Controlling Bluetooth and other wireless interfaces.
Step-by-Step Guide to Using rfkill to Manage WiFi
Step 1: Install rfkill (if not already installed)
Many Linux distributions come with rfkill pre-installed. However, if it’s not installed on your system, you can easily install it.
For Ubuntu/Debian systems:
sudo apt update
sudo apt install rfkill
For Fedora/RHEL systems:
sudo dnf install rfkill
Once installed, you can start managing your WiFi and wireless devices.
Step 2: List All Wireless Devices
To view all the wireless devices on your system (including WiFi and Bluetooth), use the following command:
rfkill list
The output will look like this:
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
1: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
- Soft blocked: Indicates that the device is disabled by software.
- Hard blocked: Indicates that the device is disabled by hardware, such as a physical switch on your laptop.
In this example, phy0 represents the WiFi interface, and hci0 represents the Bluetooth interface. You’ll use the device index (e.g., 0 for phy0) to enable or disable the wireless interface.
Step 3: Disable WiFi Using rfkill
To disable WiFi, you can use the following command. This will soft block the WiFi interface, meaning it’s disabled by software:
sudo rfkill block wifi
Alternatively, if you want to disable both WiFi and Bluetooth at once:
sudo rfkill block all
This will block all wireless interfaces (WiFi, Bluetooth, etc.).
Step 4: Enable WiFi Using rfkill
To enable WiFi after it has been blocked, you can use the following command:
sudo rfkill unblock wifi
This command will remove the soft block on your WiFi interface, enabling it.
If you want to enable all wireless devices (WiFi, Bluetooth, etc.), use:
sudo rfkill unblock all
Step 5: Check the Status of Wireless Devices
After enabling or disabling WiFi, you can check the current status of your wireless devices with:
rfkill list
The output will show whether your devices are soft or hard blocked:
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
1: hci0: Bluetooth
Soft blocked: no
Hard blocked: yes
In this example, Soft blocked: no means that the WiFi device is enabled, while Hard blocked: yes for Bluetooth indicates that it is disabled due to a hardware switch or BIOS setting.
Example of Automating WiFi Control Using rfkill
If you want to automate the enabling or disabling of WiFi, you can add these rfkill commands to a shell script or use them in combination with other Linux commands.
Here’s a simple example of a script that disables WiFi when your system starts:
#!/bin/bash
# Disable WiFi on startup
rfkill block wifi
Save the script as disable_wifi.sh
and make it executable:
chmod +x disable_wifi.sh
You can then schedule this script to run automatically at system startup using cron or by placing it in /etc/rc.local
.
Troubleshooting rfkill
- Hard block issues: If your device is hard blocked (due to a physical switch), software methods like rfkill won’t be able to unblock it. You’ll need to physically turn on the switch or check the BIOS settings.
- Persistent soft block: If you keep encountering soft block issues even after unblocking with rfkill, it might be due to power management settings. In such cases, you may need to modify network management settings or check for system-level configurations.
rfkill is an essential tool for anyone managing wireless devices from the Linux command line. Whether you’re enabling WiFi on a headless server, troubleshooting connectivity issues, or simply saving power by disabling unused wireless devices, rfkill provides a quick and efficient way to control your system’s wireless interfaces.
With the steps and examples outlined in this guide, you should now be able to confidently enable and disable WiFi and other wireless devices from the terminal.