Wifi via commandline instellen: verschil tussen versies
Geen bewerkingssamenvatting |
Geen bewerkingssamenvatting |
||
| (14 tussenliggende versies door dezelfde gebruiker niet weergegeven) | |||
| Regel 12: | Regel 12: | ||
Run iwconfig command to find the name of your wireless interface. | Run iwconfig command to find the name of your wireless interface. | ||
<code>iwconfig</code> | |||
wlan0 is a common name for a wireless network interface on Linux systems. On systemd-based Linux distros, you might have a wireless interface named wlp4s0. | wlan0 is a common name for a wireless network interface on Linux systems. On systemd-based Linux distros, you might have a wireless interface named wlp4s0. | ||
| Regel 20: | Regel 20: | ||
As you can see, the wireless interface isn’t associated with any access point right now. Then run the following command to bring up the wireless interface. | As you can see, the wireless interface isn’t associated with any access point right now. Then run the following command to bring up the wireless interface. | ||
<code>sudo ip link set dev wlp4s0 up</code> | |||
If you encounter the following error, | If you encounter the following error, | ||
| Regel 28: | Regel 28: | ||
you need to unblock Wi-Fi with the following command. | you need to unblock Wi-Fi with the following command. | ||
<code>sudo rfkill unblock wifi</code> | |||
Next, find your wireless network name by scanning nearby networks with the command below. Replace wlp4s0 with your own wireless interface name. ESSID is the network name identifier. | Next, find your wireless network name by scanning nearby networks with the command below. Replace wlp4s0 with your own wireless interface name. ESSID is the network name identifier. | ||
<code>sudo iwlist wlp4s0 scan | grep ESSID</code> | |||
debian connect to wifi command line wpa supplicant | debian connect to wifi command line wpa supplicant | ||
| Regel 39: | Regel 39: | ||
Now install wpa_supplicant on Debian 11/10 from the default software repository. | Now install wpa_supplicant on Debian 11/10 from the default software repository. | ||
<code>sudo apt install wpasupplicant</code> | |||
We need to create a file named wpa_supplicant.conf using the wpa_passphrase utility. wpa_supplicant.conf is the configuration file describing all networks that the user wants the computer to connect to. Run the following command to create this file. Replace ESSID (network name) and Wi-Fi passphrase with your own. | We need to create a file named wpa_supplicant.conf using the wpa_passphrase utility. wpa_supplicant.conf is the configuration file describing all networks that the user wants the computer to connect to. Run the following command to create this file. Replace ESSID (network name) and Wi-Fi passphrase with your own. | ||
wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf | wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf | ||
<code>wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf</code> | |||
debian wpa_passphrase | debian wpa_passphrase | ||
| Regel 52: | Regel 52: | ||
The output of wpa_passphrase command will be piped to tee, and then written to the /etc/wpa_supplicant/wpa_supplicant.conf file. Now use the following command to connect your wireless card to the wireless access point. | The output of wpa_passphrase command will be piped to tee, and then written to the /etc/wpa_supplicant/wpa_supplicant.conf file. Now use the following command to connect your wireless card to the wireless access point. | ||
<code>sudo wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlp4s0</code> | |||
The following output indicates your wireless card is successfully connected to an access point. | The following output indicates your wireless card is successfully connected to an access point. | ||
| Regel 66: | Regel 66: | ||
Note that if you are using Debian desktop edition, then you need to stop Network Manager with the following command, otherwise it will cause a connection problem when using wpa_supplicant. | Note that if you are using Debian desktop edition, then you need to stop Network Manager with the following command, otherwise it will cause a connection problem when using wpa_supplicant. | ||
<code>sudo systemctl stop NetworkManager</code> | |||
And disable NetworkManager auto-start at boot time by executing the following command. | And disable NetworkManager auto-start at boot time by executing the following command. | ||
<code>sudo systemctl disable NetworkManager-wait-online NetworkManager-dispatcher NetworkManager</code> | |||
By default, wpa_supplicant runs in the foreground. If the connection is completed, then open up another terminal window and run | By default, wpa_supplicant runs in the foreground. If the connection is completed, then open up another terminal window and run | ||
<code>iwconfig</code> | |||
You can see that the wireless interface is now associated with an access point. | You can see that the wireless interface is now associated with an access point. | ||
| Regel 83: | Regel 83: | ||
<code>sudo wpa_supplicant -B -c /etc/wpa_supplicant.conf -i wlp4s0</code> | |||
Although we’re authenticated and connected to a wireless network, we don’t have an IP address yet. To obtain a private IP address from DHCP server, use the following command: | Although we’re authenticated and connected to a wireless network, we don’t have an IP address yet. To obtain a private IP address from DHCP server, use the following command: | ||
<code>sudo dhclient wlp4s0</code> | |||
Now your wireless interface has a private IP address, which can be shown with: | Now your wireless interface has a private IP address, which can be shown with: | ||
<code>ip addr show wlp4s0</code> | |||
debian dhclient obtain private ip address | debian dhclient obtain private ip address | ||
| Regel 98: | Regel 98: | ||
Now you can access the Internet. To release the private IP address, run | Now you can access the Internet. To release the private IP address, run | ||
<code>sudo dhclient wlp4s0 -r</code> | |||
Connecting to Hidden Wireless Network | Connecting to Hidden Wireless Network | ||
| Regel 108: | Regel 108: | ||
Like below: | Like below: | ||
network={ | <code>network={ | ||
ssid="LinuxBabe.Com Network" | ssid="LinuxBabe.Com Network" | ||
#psk="12345qwert" | #psk="12345qwert" | ||
psk=68add4c5fee7dc3d0dac810f89b805d6d147c01e281f07f475a3e0195 | psk=68add4c5fee7dc3d0dac810f89b805d6d147c01e281f07f475a3e0195 | ||
scan_ssid=1 | scan_ssid=1 | ||
} | }</code> | ||
Step 3: Auto-Connect At System Boot Time | Step 3: Auto-Connect At System Boot Time | ||
| Regel 119: | Regel 119: | ||
To automatically connect to wireless network at boot time, we need to edit the wpa_supplicant.service file. It’s a good idea to copy the file from /lib/systemd/system/ directory to /etc/systemd/system/ directory, then edit the file content, because we don’t want a newer version of wpa_supplicant to override our modifications. | To automatically connect to wireless network at boot time, we need to edit the wpa_supplicant.service file. It’s a good idea to copy the file from /lib/systemd/system/ directory to /etc/systemd/system/ directory, then edit the file content, because we don’t want a newer version of wpa_supplicant to override our modifications. | ||
<code>sudo cp /lib/systemd/system/wpa_supplicant.service /etc/systemd/system/wpa_supplicant.service</code> | |||
Edit the file with a command-line text editor, such as Nano. | Edit the file with a command-line text editor, such as Nano. | ||
<code>sudo nano /etc/systemd/system/wpa_supplicant.service</code> | |||
Find the following line. | Find the following line. | ||
<code>ExecStart=/sbin/wpa_supplicant -u -s -O /run/wpa_supplicant</code> | |||
Change it to the following. Here we added the configuration file and the wireless interface name to the ExecStart command. | Change it to the following. Here we added the configuration file and the wireless interface name to the ExecStart command. | ||
<code>ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlp4s0</code> | |||
It’s recommended to always try to restart wpa_supplicant when failure is detected. Add the following right below the ExecStart line. | It’s recommended to always try to restart wpa_supplicant when failure is detected. Add the following right below the ExecStart line. | ||
Restart=always | <code>Restart=always</code> | ||
Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.) Then reload systemd. | Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.) Then reload systemd. | ||
sudo systemctl daemon-reload | <code>sudo systemctl daemon-reload</code> | ||
Enable wpa_supplicant service to start at boot time. | Enable wpa_supplicant service to start at boot time. | ||
sudo systemctl enable wpa_supplicant.service | <code>sudo systemctl enable wpa_supplicant.service</code> | ||
We also need to start dhclient at boot time to obtain a private IP address from DHCP server. This can be achieved by creating a systemd service unit for dhclient. | We also need to start dhclient at boot time to obtain a private IP address from DHCP server. This can be achieved by creating a systemd service unit for dhclient. | ||
sudo nano /etc/systemd/system/dhclient.service | <code>sudo nano /etc/systemd/system/dhclient.service</code> | ||
Put the following text into the file. | Put the following text into the file. | ||
<code> | |||
[Unit] | [Unit] | ||
Description= DHCP Client | Description= DHCP Client | ||
| Regel 164: | Regel 165: | ||
[Install] | [Install] | ||
WantedBy=multi-user.target | WantedBy=multi-user.target | ||
</code> | |||
Save and close the file. Then enable this service. | Save and close the file. Then enable this service. | ||
sudo systemctl enable dhclient.service | <code>sudo systemctl enable dhclient.service</code> | ||
How to Obtain a Static IP Address | How to Obtain a Static IP Address | ||
| Regel 173: | Regel 175: | ||
If you want to obtain a static IP address, then you need to disable dhclient.service. | If you want to obtain a static IP address, then you need to disable dhclient.service. | ||
sudo systemctl disable dhclient.service | <code>sudo systemctl disable dhclient.service</code> | ||
Create a network config file. | Create a network config file. | ||
sudo nano /etc/systemd/network/static.network | <code>sudo nano /etc/systemd/network/static.network</code> | ||
Add the following lines. | Add the following lines. | ||
<code> | |||
[Match] | [Match] | ||
Name=wlp4s0 | Name=wlp4s0 | ||
| Regel 187: | Regel 189: | ||
Address=192.168.1.8/24 | Address=192.168.1.8/24 | ||
Gateway=192.168.1.1 | Gateway=192.168.1.1 | ||
</code> | |||
Save and close the file. Then create a .link file for the wireless interface. | Save and close the file. Then create a .link file for the wireless interface. | ||
| Regel 193: | Regel 195: | ||
Add the following lines in this file. You need to use your own MAC address and wireless interface name. This is to prevent the system from changing the wireless interface name. | Add the following lines in this file. You need to use your own MAC address and wireless interface name. This is to prevent the system from changing the wireless interface name. | ||
<code> | |||
[Match] | [Match] | ||
MACAddress=a8:4b:05:2b:e8:54 | MACAddress=a8:4b:05:2b:e8:54 | ||
| Regel 200: | Regel 202: | ||
NamePolicy= | NamePolicy= | ||
Name=wlp4s0 | Name=wlp4s0 | ||
</code> | |||
Save and close the file. Then disable the networking.service and enable systemd-networkd.service, which will take care of networking. | Save and close the file. Then disable the networking.service and enable systemd-networkd.service, which will take care of networking. | ||
sudo systemctl disable networking | <code>sudo systemctl disable networking | ||
sudo systemctl enable systemd-networkd | sudo systemctl enable systemd-networkd</code> | ||
You can now restart systemd-networkd to see if your configuration works. | You can now restart systemd-networkd to see if your configuration works. | ||
sudo systemctl restart systemd-networkd | <code>sudo systemctl restart systemd-networkd</code> | ||
Another way to obtain a static IP address is by logging into your router’s management interface and assigning a static IP to the MAC address of your wireless card, if your router supports this feature. | Another way to obtain a static IP address is by logging into your router’s management interface and assigning a static IP to the MAC address of your wireless card, if your router supports this feature. | ||
| Regel 223: | Regel 225: | ||
To add a second Wi-Fi network, run: | To add a second Wi-Fi network, run: | ||
wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf | <code>wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf</code> | ||
Note that you need to use the -a option with the tee command, which will append, instead of deleting the original content, the new Wifi-network to the file. | Note that you need to use the -a option with the tee command, which will append, instead of deleting the original content, the new Wifi-network to the file. | ||
| Regel 232: | Regel 234: | ||
I hope this tutorial helped you connect Debian 11/10 to Wi-Fi network from the command line with WPA Supplicant. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks 🙂 | I hope this tutorial helped you connect Debian 11/10 to Wi-Fi network from the command line with WPA Supplicant. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks 🙂 | ||
Je kunt ook gebruik maken van de meegeleverde manager: | |||
<code>sudo raspi-config</code> | |||
Hiermee kun je de meeste wijzigen doorvoeren. | |||
Huidige versie van 22 feb 2024 om 07:50
Connect to Wi-Fi From Terminal on Debian 11/10 with WPA Supplicant
Last Updated: November 8th, 2022 Xiao Guoan (Admin) 20 Comments
Debian
This tutorial is going to show you how to connect to Wi-Fi network from the command line on Debian 11/10 server and desktop using wpa_supplicant, which is an implementation of the supplicant component for the WPA protocol. A supplicant in wireless LAN is client software installed on end-user’s computer that needs to be authenticated in order to join a network.
Please note that you will need to install the wpa_supplicant software before connecting to Wi-Fi, so you need to connect to Wired Ethernet first, which is done for just one time. If you don’t like this method, please don’t be mad at me. Maybe someday Debian will ship wpa_supplicant out of the box. Step 1: Find The Name of Your Wireless Interface And Wireless Network
Run iwconfig command to find the name of your wireless interface.
iwconfig
wlan0 is a common name for a wireless network interface on Linux systems. On systemd-based Linux distros, you might have a wireless interface named wlp4s0.
debian server connect to wifi terminal
As you can see, the wireless interface isn’t associated with any access point right now. Then run the following command to bring up the wireless interface.
sudo ip link set dev wlp4s0 up
If you encounter the following error,
RTNETLINK answers: Operation not possible due to RF-kill
you need to unblock Wi-Fi with the following command.
sudo rfkill unblock wifi
Next, find your wireless network name by scanning nearby networks with the command below. Replace wlp4s0 with your own wireless interface name. ESSID is the network name identifier.
sudo iwlist wlp4s0 scan | grep ESSID
debian connect to wifi command line wpa supplicant Step 2: Connect to Wi-Fi Network With WPA_Supplicant
Now install wpa_supplicant on Debian 11/10 from the default software repository.
sudo apt install wpasupplicant
We need to create a file named wpa_supplicant.conf using the wpa_passphrase utility. wpa_supplicant.conf is the configuration file describing all networks that the user wants the computer to connect to. Run the following command to create this file. Replace ESSID (network name) and Wi-Fi passphrase with your own. wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf
wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf
debian wpa_passphrase
If your ESSID contains whitespace such as (linuxbabe WiFi), you need to wrap the ESSID with double-quotes ("linuxbabe WiFi") in the above command.
The output of wpa_passphrase command will be piped to tee, and then written to the /etc/wpa_supplicant/wpa_supplicant.conf file. Now use the following command to connect your wireless card to the wireless access point.
sudo wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlp4s0
The following output indicates your wireless card is successfully connected to an access point.
Successfully initialized wpa_supplicant wlp4s0: SME: Trying to authenticate with c5:4a:21:53:ac:eb (SSID='CMCC-11802' freq=2437 MHz) wlp4s0: Trying to associate with c5:4a:21:53:ac:eb (SSID='CMCC-11802' freq=2437 MHz) wlp4s0: Associated with c5:4a:21:53:ac:eb wlp4s0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0 wlp4s0: WPA: Key negotiation completed with c5:4a:21:53:ac:eb [PTK=CCMP GTK=CCMP] wlp4s0: CTRL-EVENT-CONNECTED - Connection to c5:4a:21:53:ac:eb completed [id=0 id_str=]
Note that if you are using Debian desktop edition, then you need to stop Network Manager with the following command, otherwise it will cause a connection problem when using wpa_supplicant.
sudo systemctl stop NetworkManager
And disable NetworkManager auto-start at boot time by executing the following command.
sudo systemctl disable NetworkManager-wait-online NetworkManager-dispatcher NetworkManager
By default, wpa_supplicant runs in the foreground. If the connection is completed, then open up another terminal window and run
iwconfig
You can see that the wireless interface is now associated with an access point.
enable wifi on debian using terminal command
You can press CTRL+C to stop the current wpa_supplicant process and run it in the background by adding the -B flag.
sudo wpa_supplicant -B -c /etc/wpa_supplicant.conf -i wlp4s0
Although we’re authenticated and connected to a wireless network, we don’t have an IP address yet. To obtain a private IP address from DHCP server, use the following command:
sudo dhclient wlp4s0
Now your wireless interface has a private IP address, which can be shown with:
ip addr show wlp4s0
debian dhclient obtain private ip address
Now you can access the Internet. To release the private IP address, run
sudo dhclient wlp4s0 -r
Connecting to Hidden Wireless Network
If your wireless router doesn’t broadcast ESSID, then you need to add the following line in /etc/wpa_supplicant/wpa_supplicant.conf file.
scan_ssid=1
Like below:
network={
ssid="LinuxBabe.Com Network"
#psk="12345qwert"
psk=68add4c5fee7dc3d0dac810f89b805d6d147c01e281f07f475a3e0195
scan_ssid=1
}
Step 3: Auto-Connect At System Boot Time
To automatically connect to wireless network at boot time, we need to edit the wpa_supplicant.service file. It’s a good idea to copy the file from /lib/systemd/system/ directory to /etc/systemd/system/ directory, then edit the file content, because we don’t want a newer version of wpa_supplicant to override our modifications.
sudo cp /lib/systemd/system/wpa_supplicant.service /etc/systemd/system/wpa_supplicant.service
Edit the file with a command-line text editor, such as Nano.
sudo nano /etc/systemd/system/wpa_supplicant.service
Find the following line.
ExecStart=/sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
Change it to the following. Here we added the configuration file and the wireless interface name to the ExecStart command.
ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlp4s0
It’s recommended to always try to restart wpa_supplicant when failure is detected. Add the following right below the ExecStart line.
Restart=always
Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.) Then reload systemd.
sudo systemctl daemon-reload
Enable wpa_supplicant service to start at boot time.
sudo systemctl enable wpa_supplicant.service
We also need to start dhclient at boot time to obtain a private IP address from DHCP server. This can be achieved by creating a systemd service unit for dhclient.
sudo nano /etc/systemd/system/dhclient.service
Put the following text into the file.
[Unit]
Description= DHCP Client
Before=network.target
After=wpa_supplicant.service
[Service]
Type=forking
ExecStart=/sbin/dhclient wlp4s0 -v
ExecStop=/sbin/dhclient wlp4s0 -r
Restart=always
[Install]
WantedBy=multi-user.target
Save and close the file. Then enable this service.
sudo systemctl enable dhclient.service
How to Obtain a Static IP Address
If you want to obtain a static IP address, then you need to disable dhclient.service.
sudo systemctl disable dhclient.service
Create a network config file.
sudo nano /etc/systemd/network/static.network
Add the following lines.
[Match]
Name=wlp4s0
[Network]
Address=192.168.1.8/24
Gateway=192.168.1.1
Save and close the file. Then create a .link file for the wireless interface.
sudo nano /etc/systemd/network/10-wifi.link
Add the following lines in this file. You need to use your own MAC address and wireless interface name. This is to prevent the system from changing the wireless interface name.
[Match]
MACAddress=a8:4b:05:2b:e8:54
[Link]
NamePolicy=
Name=wlp4s0
Save and close the file. Then disable the networking.service and enable systemd-networkd.service, which will take care of networking.
sudo systemctl disable networking
sudo systemctl enable systemd-networkd
You can now restart systemd-networkd to see if your configuration works.
sudo systemctl restart systemd-networkd
Another way to obtain a static IP address is by logging into your router’s management interface and assigning a static IP to the MAC address of your wireless card, if your router supports this feature.
Recommended Reading:
How to Use Systemd on Linux – Manage Services, Run Levels and Logs
Multiple Wi-Fi Networks
The /etc/wpa_supplicant.conf configuration file can include multiple Wi-Fi networks. wpa_supplicant automatically selects the best network based on the order of network blocks in the configuration file, network security level, and signal strength.
To add a second Wi-Fi network, run:
wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf
Note that you need to use the -a option with the tee command, which will append, instead of deleting the original content, the new Wifi-network to the file. Wi-Fi Security
Do not use WPA2 TKIP or WPA2 TKIP+AES as the encryption method in your Wi-Fi router. TKIP is not considered secure anymore. You can use WPA2-AES as the encryption method. Wrapping Up
I hope this tutorial helped you connect Debian 11/10 to Wi-Fi network from the command line with WPA Supplicant. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks 🙂
Je kunt ook gebruik maken van de meegeleverde manager:
sudo raspi-config
Hiermee kun je de meeste wijzigen doorvoeren.