How to connect to a WiFi from command line under Ubuntu without .conf file?

Solution 1:

You can control a running wpa_supplicant using it's control interface, which you already specify in your .conf file. While this still needs a .conf file, you don't have to put any wireless networks in it, and don't have to change it. You can then configure it with wpa_cli.

wpa_cli may need to be told which wpa_supplicant instance and interface to configure:

wpa_cli -p /var/run/wpa_supplicant -i wlan0 command ...

For clarity, I'll use just wpa_cli here. Basically, you need to create a network, set its variables, and enable it:

# wpa_cli add_network
4                                          <--- note the network ID!
# wpa_cli set_network 4 ssid '"Your SSID"'
OK
# wpa_cli set_network 4 scan_ssid 1
OK
# wpa_cli set_network 4 key_mgmt WPA-PSK
OK
# wpa_cli set_network 4 psk '"1234567890"' <--- note the single quotes around
OK
# wpa_cli enable_network 4
OK