Page 1 of 1

How to connect to wifi from the terminal?

Posted: Mon Aug 30, 2021 10:21 pm
by je55eah

I attempted to uninstall openbox and jwm because I am trying to replace the window manager. I'll be posting about that soon because it isn't going too well.

What is the best way to connect to wifi from the terminal? I attempted to use nmtui after installing network-manager but the wifi is not listed.

I experimented with adding

Code: Select all

auto wlan0
iface wlan0 inet dhcp

to /etc/network/interfaces
and the wifi did appear in nmtui once after I removed those lines and restarted the network manager service, but I haven't been able to repeat it so I don't know why it worked the one time. Is there a better tool already installed or a reliable way to set the system up to automatically connect to a preferred network and fallback to other know networks in a logical way?


Re: How to connect to wifi from the terminal?

Posted: Tue Aug 31, 2021 12:45 pm
by Trapster

I've had good luck with wicd in Debiandog-Jessie

https://wiki.debian.org/WiFi/HowToUse#Wicd


Re: How to connect to wifi from the terminal?

Posted: Thu Sep 02, 2021 2:26 pm
by je55eah

Thanks Trapster.

Rather than installing depreciated software I worked out a way to do it with the preinstalled packages.

Method 1 (open network):

Code: Select all

cat > /etc/wpa_supplicant/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
    ssid="Channel2B"
    key_mgmt=NONE
}
EOF

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

Method 1 (cleartext password):

Code: Select all

cat > /etc/wpa_supplicant/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
    ssid="Channel2B"
    psk="password"
}
EOF

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

Method 1 (hashed password):

Code: Select all

echo -n password | iconv -t utf16le | openssl md4

use the hash below

Code: Select all

cat > /etc/wpa_supplicant/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
    ssid="Channel2B"
    password=hash:hash_from_above
}
EOF

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

Method 1 (alternative hashed password):

Code: Select all

wpa_passphrase "Channel2B" password

that will produce output like:

Code: Select all

network={
	ssid="Channel2B"
	#psk="password"
	psk=1b964719888513fdebf19047efdf4b6f182eef9d2d58639eb9e25f437cd30da8
}

remove the line with your password so you get

Code: Select all

network={
	ssid="Channel2B"
	psk=1b964719888513fdebf19047efdf4b6f182eef9d2d58639eb9e25f437cd30da8
}

then use that stanza below

Code: Select all

cat > /etc/wpa_supplicant/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
	ssid="Channel2B"
	psk=1b964719888513fdebf19047efdf4b6f182eef9d2d58639eb9e25f437cd30da8
}
EOF

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

the line that reads
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
could also simply be
ctrl_interface=/run/wpa_supplicant

Method 2 (open network)

Code: Select all

cat > /etc/wpa_supplicant/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
EOF

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

wpa_cli scan
wpa_cli scan_results
wpa_cli add_network
wpa_cli set_network 0 ssid '"Channel2B"'
wpa_cli set_network 0 key_mgmt NONE

wpa_cli enable_network 0

#optional
wpa_cli save_config

ip a

Method 2 (cleartext password)

Code: Select all

cat > /etc/wpa_supplicant/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
EOF

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

wpa_cli scan
wpa_cli scan_results
wpa_cli add_network
wpa_cli set_network 0 ssid '"Channel2B"'
wpa_cli set_network 0 psk '"password"'

wpa_cli enable_network 0

#optional
wpa_cli save_config

ip a

Method 2 (hashed password)

Generate your hash with one of the methods used above in method 1 then use it instead of the password.

Code: Select all

cat > /etc/wpa_supplicant/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
EOF

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

wpa_cli scan
wpa_cli scan_results
wpa_cli add_network
wpa_cli set_network 0 ssid '"Channel2B"'
wpa_cli set_network 0 psk '"password_hash"'

wpa_cli enable_network 0

#optional
wpa_cli save_config

ip a

When the config is saved then Method 2 can be started after a reboot with

Code: Select all

wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
wpa_cli enable_network 0

I didn't fully test every option, but I tested a lot of them to the point where it was working for me.

You can create a shell script to run any of these by starting the file with #!/bin/sh


Re: How to connect to wifi from the terminal?

Posted: Fri Sep 03, 2021 3:40 am
by je55eah

Additionally,

With either method you can add the pertinent commands to /etc/profile to automatically connect at startup.

The built in startup process also tries to connect and you will see that it reports a failure with method 1 and it tries but fails to call ifup with method 2. Neither results in an automatic start. I attempted to tap into that and get it to start the wifi as someone must have intended, but I failed. Oddly, I did discover that if you add something to /etc/network/interfaces such as

Code: Select all

wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

The startup script reports an error but the wifi is actually connected... If you just want the result and you don't care if the path to it is broken and poorly understood then that is one solution.

Code: Select all

iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

also works and it fails with a green error instead of a red one.

If someone can explain the right way to hook into this I'd appreciate it. Without better documentation, the best solution is probably just to initialize wifi with /etc/profile.


Re: How to connect to wifi from the terminal?

Posted: Fri Sep 03, 2021 8:30 am
by gumanzoy

I use nmcli:

Code: Select all

nmcli d wifi connect WIFISSID password WIFIPASS