How to connect to wifi from command line?

  • I'm using latest version of Debian-based Linux Kali. Maybe it is an XY problem, as the main problem is after I log in to the system I get a blank screen and mouse pointer. Somebody on the Internet recommend me to change window manager.

    But I'm unable to do this as I can't connect to wifi.

    I found tutorial how to do this here

    And I tried to do it step by step, but it doesn't work for me. In that tutorial that author wrote that I need use the command ip link set wlan0 ip to bring up the wifi interface. In his example the output looks like this:

    [email protected]:~# ip link show w
    lan0 4: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN mode DORMANT qlen 1000
      link/ether 00:60:64:37:4a:30 brd ff:ff:ff:ff:ff:ff
    
    [email protected]:~# ip link set wlan0 up
    
    [email protected]:~# ip link show wlan0
    4: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT qlen 1000
        link/ether 00:60:64:37:4a:30 brd ff:ff:ff:ff:ff:ff
    

    On the other hand when I call:

       ip link set wlan0 up
       ip link show wlan0
    

    I get:

      4: wlan0: <NO_CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DORMANT qlen 1000
       link/ether 00:60:64:37:4a:30 brd ff:ff:ff:ff:ff:ff
    

    and after running wpa supplicant with valid network details

    wpa_supplicant -B -D wext -i wlan0 -c /etc/wpa_supplicant.conf
    

    iw wlan0 link still returns Not connected.

    How do I solve this problem and what shall I do next?

    Can you scan using `iwlist scan` ?

    @GAD3R yes i can

    some are different: so i used; `ifup wlp1s0` to bring the network up and `ifdown wlp1s0` to bring it down.

  • GAD3R

    GAD3R Correct answer

    5 years ago

    I'm assuming wpa_supplicant and iw is installed.

    1. To connect to wifi through wpa_supplicant you need to create a wpa_supplicant.conf file

      nano /etc/wpa_supplicant.conf
      

      with the following lines:

      network={
               ssid="wifi_name"
               psk="wifi_key"
      }
      

    Or you can use wpa_passphrase to create the configuration file (copy and past):

    wpa_passphrase "Your_SSID" Your_passwd 
    

    Also you can write the wpa_supplicant.conf directly through:

    wpa_passphrase "Your_SSID" Your_passwd > /etc/wpa_supplicant.conf
    

    to connect type the following command:

    sudo ip link set wlan0 down
    sudo ip link set wlan0 up
    sudo wpa_supplicant -B -iwlan0 -c /etc/wpa_supplicant.conf -Dnl80211,wext
    sudo dhclient wlan0
    

    Note: Multiple comma separated driver wrappers in option -Dnl80211,wext makes wpa_supplicant use the first driver wrapper that is able to initialize the interface (see wpa_supplicant(8)). This is useful when using mutiple or removable (e.g. USB) wireless devices which use different drivers.

    You can connect through wpa_supplicant without wpa_supplicant.conf file:

    wpa_supplicant -B -i wlan0 -c <(wpa_passphrase "Your_SSID" Your_passphrase) && dhclient wlan0
    

    You can visit the official documentation of Arch-linux to get more information about the configuration file and arguments.

    1. you can connect through nmcli

      nmcli d wifi connect Your_SSID password Your_Psswd_here iface Your_interface
      

    Example:

    nmcli d wifi connect MYSSID password 12345678 iface wlan0
    
    1. Also you can connect through wpa_cli:

    Open the terminal and type wpa_cli

    To scan, type:

    scan
    scan_results
    

    Create a network:

    add_network
    

    This will output a number, which is the network ID, for example 0 Next, we need to set the SSID and PSK for the network.

    set_network 0 ssid "SSID_here"
    set_network 0 psk "Passphrase_here"
    

    Once the wireless has connected, it should automatically get an IP address. if it doesn’t you can run the dhclient to get an IP address via DHCP.

    The dhclient command ca be replaced with 2 ip commands:

    ip addr add IP-ADDRESSE/24 dev wlan0
    ip route add default via ROUTE
    
    1. iwctl command line tools.

    The iwd package provide the iwctl command line tools . The package isn't installed by default. To avoid any conflict the wpasupplicant.service should be stopped/disabled.

    for more details see this answer on U&L: Connect to wifi from command line on linux systems through the iwd (wireless daemon for linux)

    Further reading :

    Connecting with wpa_cli

    Connecting with wpa_passphrase

    nmcli examples

    Archlinux: iwd/iwctl

    your first solution is exactly like one in that tutorial, with one difference you create file by yourself, and there they used wpa_passphrase, but connecting via nmcli solved my problem, thanks

    Please add for debian user of barebone arm system needs to add interface to `/etc/network/intefaces auto wlp1s0 iface wlp1s0 inet dhcp pre-up wpa_supplicant -i wlp1s0 -c /etc/wpa_supplicant.conf -B` Then rebooting little box and it has ip already...

    I wonder if there is a "pseudogui" available. When I setup the box, I was presented selection of ethernet or wifi. I selected wifi, then seleted SSID from a list, entered encryption, password, if I wanted dhcp or static IP, Then network worked to complete the installation. Why isn't this setup which works for the setup copied to the final system? Why is all this cli nessecary?

    nmtui is not available after install. Having to get online to get a gui to get online is a chicken-and-egg problem. I managed to save the /target/etc/network/interfaces during install; after install the lines for the wifi is removed.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM