3

I'm mantaining a post-install script for my ubuntu. I'd like to add wifi network connections by bash, so I'd automaticall connect to them later, but I can't figure out a way to do this.

Is there a simple command or config file where I should add my SSIDs and passphrases? Again, without needing to actually connect to a network at the time... Thanks!

EDIT:

As suggested by @LassePoulsen, you can create INI-style files in /etc/NetwokManager/system-connections. A bit of advice on that:

  • Use uuidgen to generate the uuid value
  • Don't forget to chmod 600! Explanation:

    For security, it will ignore files that are readable or writeable by any user or group other than 'root' since private keys and passphrases may be stored in plaintext inside the file.

  • You must reload the configuration after manual changes

Useful links:

2 Answers 2

2

You should be able to just create the configuration files for each connection, these configuration files resides in /etc/NetworkManager/system-connections and are plane INI-style text files.

Here is an example file from my system /etc/NetworkManager/system-connections/AndroidAPP:

[connection]
id=AndroidAPP
uuid=a36db5c2-293e-443c-b267-0ecd4bd6d9ce
type=wifi
autoconnect=false
permissions=
secondaries=

[wifi]
mac-address=03:51:1F:01:02:03
mac-address-blacklist=
mode=infrastructure
seen-bssids=
ssid=AndroidAPP

[wifi-security]
group=
key-mgmt=wpa-psk
pairwise=
proto=
psk=ReallyStrongPresharedKey

[ipv4]
dns-search=
method=auto

[ipv6]
dns-search=
ip6-privacy=0
method=auto

While you could create these by hand, it's much easier to create all the connections on one machine and then copy them from there. But you still have to be aware of a few things:

  • mac-address is the hardware address of the nic to use for the connection
  • uuid must be uniq as it is used for internal references in NetworkManager
  • autoconnect is always enabled if it is not set to false
  • Remember to restart NetworkManager after changing the files.
3
  • The thing is I've tried this method already as soon as I found the connection files on that dir. I've created a non-existing connection using the GUI to base my files on that. But even after restarting ubuntu, the network manager doesn't recognize them. Maybe the uuid must be a valid hash of some of the file's contents? Any suggestions?
    – fett2k
    Sep 19, 2015 at 1:09
  • Afaik the uuid just have to be the right format, I have used this method a couple of times (on Arch Linux) so I'm not 100% sure it will work on Ubuntu Sep 19, 2015 at 1:12
  • Turns out I had the wrong permissions set on the file, so I've marked your answer as a working solution! The only thing I'm missing now is a clean way of reloading the network manager config.
    – fett2k
    Sep 21, 2015 at 22:09
0

See man nm-connection-manager, and its See Also section, to see how it is done (dpkg -L network-manager-gnome to see what to read):

trusty (1) nm-connection-editor.1.gz
Provided by: network-manager-gnome_0.9.8.8-0ubuntu4_i386 

NAME
       nm-connection-editor - network connection editor for NetworkManager

SYNOPSIS
       nm-connection-editor  [ OPTIONS ]

DESCRIPTION
       nm-connection-editor  is  a  GTK‐based  application to add, remove, and
       modify network connections stored  by  NetworkManager.   NetworkManager
       must  be  running  for any network connections to be added, removed, or
       modified.
1
  • Thanks! But nm-connection-editor is the gui. I had already looked at nmcli but it only lets me add a network if I'm in it's range. So the next thing would be looking at it's source, but I'd like to avoid that.
    – fett2k
    Sep 19, 2015 at 1:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .