Use Network-Manager to connect to a WiFi Access Point on the command-line

38,870

Solution 1

You can use cnetworkmanager for this.

Edit:

After some investigation it becomes clear that it is impossible to run two "user configuration providers" (an application listening at a certain dbus address) at the same time, and cnetworkmanager needs to act as such a "configuration provider" (at least until the connection is set up).

Now, what you can do is add the information for the connection to gconf under the /system/networking/connections key (you can do that from the commandline or a script), and then use dbus or a command-line tool like cnetworkmanager or nmcli to connect to it. Maybe you'll also have to add the key to the user's keyring first (to avoid password popups).

But of course that won't work on a desktop that uses another NM-client than nm-applet (e.g. KDE). You would have to figure out how to provide a config for them too...

Edit 2:

It seems like it might be possible via org.freedesktop.NetworkManagerUserSettings on the system DBus, but this is all poorly documented IMO...

You might want to read about configuration settings and the DBus interface spec.

And here is a relevant example (maybe you want to set up a user connection instead of a system connection though)

Solution 2

You can do this directly using nmcli, even if the connection isn't configured in Network Manager yet:

nmcli dev wifi con "myssid" password "myssidpassword"

If you don't want to have the wireless password stored in your history in recent versions (maybe newer than mentioned below) you can add --ask to have nmcli prompt you for missing information it needs.

nmcli --ask device wifi connnect "myssid"

This facility was added in Network Manager 0.9.6, so this only works in Ubuntu 12.10 or later.

You can also get a list of available access points with:

nmcli dev wifi list

Solution 3

You can use iwconfig (wifitools) for wep encryption, or wpa_supplicant for wpa and wep, also you use wicd-curses if you're looking for something easier, good luck =)

Solution 4

You can check out iwconfig and iwlist:

  • iwconfig manipulate the basic wireless parameters
  • iwlist allow to initiate scanning and list frequencies, bit-rates, encryption keys...

There are many options. Check out Wireless Tools for Linux for more.

Share:
38,870

Related videos on Youtube

flo
Author by

flo

I'm a Software Developer in Berlin, Germany.

Updated on September 17, 2022

Comments

  • flo
    flo over 1 year

    I'd like to connect to a wireless access point from the command-line. Ideally, I'd only need the name of the AP. But the hardware-address would work as well.

    I know I can use nmcli to connect to a managed network connection, but in my case, the access point may not be configured for Network-Manager yet (See the difference between the output of nm-tool and nmcli con).

    Example output of nmcli:

    Auto pwln       3a3d62b1-bbdf-4f76-b4d2-c211fd5cfb03   802-11-wireless [...]  
    Wired Network   aa586921-accf-4932-98c4-c873c310f08e   802-3-ethernet [...]  
    Cisco-UDP Uni   7f94847b-04dc-40b7-9955-5246fb77cc65   vpn [...]  
    T-mobile (D1)   867f345a-cbbf-4bd4-b883-a5e5ae0932f0   gsm  [...]
    

    Example output of nm-tool:

    State: connected
    - Device: eth1  [Auto pwln] ----------------------------------------------------
    [...]
      Wireless Access Points (* = current AP)
        *pwln: Infra, [...], Freq 2472 MHz, Rate 54 Mb/s, Strength 80 WPA WPA2
        WLAN:  Infra, [...], Freq 2422 MHz, Rate 54 Mb/s, Strength 20 WPA WPA2
    
    [...]
    

    How do I connect to an access point that may or may not be known to NM?

    Extra: Finding out if the connection needs a pass-phrase, and submitting it on the command-line as well would be great too (that is to say It'd be nice if network-manager wouldn't pop open any keyring dialogues or errors on the gui)

  • flo
    flo over 13 years
    I'd really want to use network-manager, maybe via dbus. I can't mess about with the operating system too much, since the program i'm writing isn't only going to run on my own machine. All the configuration, where possible, should remain unchanged.
  • flo
    flo over 13 years
    As I said, I'd like to integrate with network-manager, not replace it.
  • flo
    flo over 13 years
    Still nothing. Unfortunately, cnetworkmanager will not work properly besides the networkmanager-applet ("Another applet is running")
  • JanC
    JanC over 13 years
    I did read up on how NM & nm-applet works & edited my answer.
  • kaiser
    kaiser over 9 years
    con is not recognized by nmcli dev wifi
  • Anish
    Anish over 9 years
    What version are you using? It works for me, and is documented as an example in the manpage.
  • kaiser
    kaiser over 9 years
    nmcli -v is 0.9.4.0
  • Anish
    Anish over 9 years
    So you're using 12.04? As I said in my answer, this only works on Ubuntu 12.10 or later.
  • kaiser
    kaiser over 9 years
    This might be the case. I wonder if I could sudo apt-get update it.
  • Cerin
    Cerin almost 9 years
    This gets me close, but even though ifconfig says my wlan0 has an IP, I can't ping or resolve any addresses.
  • dragon788
    dragon788 over 6 years
    @Cerin what does cat /etc/resolv.conf show you? If NetworkManager is managing it sometimes it gets dumb, you can edit /etc/NetworkManager/NetworkManager.conf and comment out the dnsmasq line and restart the service (or the computer) and try again, that should show you the actual nameservers you are trying to use.
  • Michael
    Michael almost 4 years
    how can i make nmcli use the saved password in /etc/NetworkManager system-connections?