NetworkManager

From ParabolaWiki
Jump to: navigation, search
Summary
Covers installation and configuration of NetworkManager – a set of co-operative tools that make networking simple and straightforward.
Overview
Parabola provides netctl for network configuration. netctl supports wired connections on desktops and servers, as well as wireless setups and roaming for mobile users, facilitating easy management of network profiles. NetworkManager and Wicd are popular third-party alternatives.

NetworkManager is a program for providing detection and configuration for systems to automatically connect to network. NetworkManager's functionality can be useful for both wireless and wired networks. For wireless networks, NetworkManager prefers known wireless networks and has the ability to switch to the most reliable network. NetworkManager-aware applications can switch from online and offline mode. NetworkManager also prefers wired connections over wireless ones, has support for modem connections and certain types of VPN. NetworkManager was originally developed by RedHat and now is hosted by the GNOME project.

1 Base install

NetworkManager is available in the [extra] repository:

# pacman -S networkmanager

2 Graphical Front-ends

To configure and have easy access to NetworkManager most people will want to install an applet. This GUI front-end usually resides in the system tray (or notification area) and allows network selection and configuration of NetworkManager. Various applets exist for different types of desktops.

2.1 GNOME

GNOME's applet (formerly gnome-network-manager) is lightweight enough and works across all environments:

# pacman -S network-manager-applet

If you want to store authentication details (Wireless/DSL) and enable global connection settings, i.e "available to all users":

# pacman -S gnome-keyring

2.2 KDE4

The KNetworkManager front-end has been made available in KDE version 4.4 as a plasma widget:

# pacman -S kdeplasma-applets-networkmanagement

The GNOME counterpart works just as nicely, or even better (has more features and detects more hardware).

Note: If you are changing from another network managing tool like Wicd, do not forget to set the default 'Network Management Backend' in System Settings -> Hardware -> Information Sources

If you have both KNetworkManager and nm-applet installed and do not want to start nm-applet when using KDE, add the following line to /etc/xdg/autostart/nm-applet.desktop.

NotShowIn=KDE

2.3 Openbox

The GNOME applet with the xfce4-notifyd notification daemon works well:

# pacman -S network-manager-applet xfce4-notifyd hicolor-icon-theme gnome-icon-theme

If you want to store authentication details (Wireless/DSL):

# pacman -S gnome-keyring

To prevent nm-applet dbus errors, edit ~/.xinitrc and change "exec openbox-session" to "exec ck-launch-session openbox-session".

Note: If networkmanager daemon is in rc.conf, the following settings are obsolete or the applet will be started twice.

To have Openbox's autostart start nm-applet properly, you may need to delete the file /etc/xdg/autostart/nm-applet.desktop (You may need to delete this file again after every update to network-manager-applet)

Then in autostart, start nm-applet with this line:

(sleep 3 && /usr/bin/nm-applet --sm-disable) &

2.4 Other Desktops and Window Managers

It is recommended to use the GNOME applet. You'll also need to be sure that the GNOME hicolor theme is installed to be able to display the applet:

# pacman -S hicolor-icon-theme gnome-icon-theme

In order to run nm-applet without a systray, you can use trayer or stalonetray. For example, you can add a "nmgui" script like this one in your path :

#!/bin/sh
nm-applet    > /dev/null 2>/dev/null &
stalonetray  > /dev/null 2>/dev/null
killall nm-applet

When you close the stalonetray window, it closes nm-applet too, so no extra memory is used once you're done with network settings.

2.5 Command line

The networkmanager package since version 0.8.1 contains nmcli

3 Configuration

NetworkManager will require some additional steps to be able run properly.

Verify that your /etc/hosts is correct before continuing. If you previously tried to connect before doing this step, NetworkManager may have altered it. An example hostname line in /etc/hosts:

#<ip-address> <hostname.domain.org>           <hostname>                        
127.0.0.1     localhost.localdomain localhost dell-latitude

3.1 Disable current network setup

You'll want to disable your current network setup to be able to properly test NetworkManager. First (if using the Parabola network scripts) stop the network:

/etc/rc.d/network stop

Bring down your NIC's (Network Interface Controllers, i.e. network cards). For example (using the iproute2 package):

ip link set down eth0
ip link set down wlan0

Edit /etc/rc.conf and where you defined DHCP or a static IP address, comment them out:

Note: Following settings are obsolete in the most recent rc.conf.
#eth0="dhcp"                                                                    
#wlan0="dhcp"                                                                   
INTERFACES=(!eth0 !wlan0)

3.2 Edit daemons

You must remove the default network daemon and add the networkmanager daemon, after the dbus daemon:

DAEMONS=( ...dbus networkmanager... )

Be sure that the package dbus is installed as NetworkManager will require it. To start other services (daemons) that require a network connection see the next section on how to set them up. Though the NetworkManager daemon has been started here, it will not (by default) connect onto a network until an applet is loaded and the applet specifies to do so. This means that networking services will need to be specified to NetworkManager on when to run.

3.3 Set up PolicyKit permissions

In the default setup, non-root users are not allowed to add or enable network connections. They will see errors mentioning missing privileges.

With a working session, you have several options for granting the necessary privileges to NetworkManager:

  • Option 1. Run a PolicyKit authentication agent when you log in, such as /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 (part of polkit-gnome). You will be prompted for your password whenever you add or remove a network connection.
  • Option 2. Add yourself to the wheel group. You will not have to enter your password, but your user account may be granted other permissions as well, such as the ability to use sudo without entering the root password.
usermod -aG wheel user
  • Option 3. Add yourself to the network group and create the following file:
usermod -aG network user
/etc/polkit-1/rules.d/50-org.freedesktop.NetworkManager.rules
polkit.addRule(function(action, subject) {
  if (action.id.indexOf("org.freedesktop.NetworkManager.") == 0 && subject.isInGroup("network")) {
    return polkit.Result.YES;
  }
});

All users in the network group will be able to manage networks without a password.

3.4 Network Services with NetworkManager Dispatcher

There are quite a few network services that you will not want running until NetworkManager brings up an interface. Good examples are openntpd and network filesystem mounts of various types (e.g. netfs). NetworkManager has the ability to start these services when you connect to a network (interface up), and stop them when you are no longer using them (interface down).

To use this feature, scripts can be added to the /etc/NetworkManager/dispatcher.d directory. These scripts will need to have executable, user permissions. For security, it's good practice to make them owned by root:root and writable only by the owner.

Warning: For security reason. You should disable write access for group and other. For example use 755 mask. In other case it can refuse to execute script, with error message "nm-dispatcher.action: Script could not be executed: writable by group or other, or set-UID." in /var/log/messages.log

The scripts will be run in alphabetical order at connection time (with arguments interface up), and in reverse alphabetical order at disconnect time (interface down). To ensure what order they come up in, it is common to use numerical characters prior to the name of the script (e.g. 10_portmap or 30_netfs (which ensures that the portmapper is up before NFS mounts are attempted).

The following starts openntpd when an interface is brought up. Save the file as /etc/NetworkManager/dispatcher.d/20_openntpd and make it executable.

#!/bin/sh

INTERFACE=$1 # The interface which is brought up or down
STATUS=$2 # The new state of the interface

case "$STATUS" in
    'up') # $INTERFACE is up
	exec /etc/rc.d/openntpd start
	;;
    'down') # $INTERFACE is down
	# Check for active interface and down if no one active
	if [ ! `nm-tool|grep State|cut -f2 -d' '` = "connected" ]; then
		exec /etc/rc.d/openntpd stop
	fi
	;;
esac
Warning: if you connect to foreign or public networks, be aware of what services you are starting and what servers you expect to be available for them to connect to. You could make a security hole by starting the wrong services while connected to a public network.

3.4.1 Use dispatcher to connect to a vpn after a network-connection is established

In this example we want to connect automatically to a vpn-connection we defined previously with NetworkManager. First thing to to is to create the dispatcher-script that defines what to do after we connected to the network.

1. Create the dispatcher script in /etc/NetworkManager/dispatcher.d/vpn-up

case "$2" in
       up)
               sudo -u username DISPLAY=:0 /usr/bin/python /etc/NetworkManager/vpn-up.py
               ;;
esac

Remember to make it executable with chmod +x and change username to the right one.

2. Create the /etc/NetworkManager/vpn-up.py and change network-ESSID to the desired one.

Now NetworkManager should try to connect to your vpn which you had defined in your profile.

More recent versions of NetworkManager have seen a change in the python interface used in the above script, so it may no longer function. An alternative solution, however, can be found in clever use of nmcli.

#! /bin/bash

REQUIRED_CONNECTION_NAME=""
VPN_CONNECTION_NAME=""


activ_con=$(nmcli con status | grep "${REQUIRED_CONNECTION_NAME}")
activ_vpn=$(nmcli con status | grep "${VPN_CONNECTION_NAME}")
if [ "${activ_con}" -a ! "${activ_vpn}" ];
then
    nmcli con up id "${VPN_CONNECTION_NAME}"
fi

4 Testing

NetworkManager applets are designed to load upon login so no further configuration should be necessary for most users. If you have already disabled your previous network settings and disconnected from your network, you can now test if NetworkManager will work. First start the daemon:

/etc/rc.d/networkmanager start

Some applets will provide you with a .desktop file so that the NetworkManager applet can be loaded through the application menu. If it doesn't, you're going to either have to discover the command to use or logout and login again to start the applet. Once the applet is started, it will likely begin polling network connections with for auto-configuration with a DHCP server.

To start the GNOME applet in non-xdg-compliant Window Managers like Awesome:

nm-applet --sm-disable &

For static IPs you will have to configure NetworkManager to understand them. The process usually involves right-clicking the applet and selecting something like 'Edit Connections'.

5 Troubleshooting

Some fixes to common problems.

5.1 Permission problems

Make sure PolicyKit is set up (see #Set up PolicyKit permissions).

If you use SLiM, follow SLiM#PolicyKit. This sometimes may still not be enough. You may have to manually start polkit-gnome/polkit-gnome-authentication-agent-1 for network manager to work.

5.2 Network Management Disabled

Sometimes when NM shuts down the pid (state) file does not get removed and you will get a 'Network management disabled' message. If this happens, you'l have to remove it manually:

rm /var/lib/NetworkManager/NetworkManager.state

If this happens upon reboot, you can add an action to your etc/rc.local to have it removed upon bootup:

nmpid=/var/lib/NetworkManager/NetworkManager.state
[ -f $nmpid ] && rm $nmpid

5.3 Preserving changes to resolv.conf

NetworkManager will attempt to write DNS information from DHCP into /etc/resolv.conf, overwriting the existing contents. To prevent this, you can set the immutable bit on the file (as root):

# chattr +i /etc/resolv.conf

To modify the file in the future, first remove the immutable bit:

# chattr -i /etc/resolv.conf

5.4 DHCP problems

If you have problems with getting an IP via DHCP, try to add the following to your /etc/dhclient.conf:

 interface "eth0" {
   send dhcp-client-identifier 01:aa:bb:cc:dd:ee:ff;
 }

Where aa:bb:cc:dd:ee:ff is the MAC-adress of this NIC.

For some (incompliant?) routers, you will not be able to connect properly unless you comment the line

require dhcp_server_identifier

in /etc/dhcpcd.conf (note that this file is distinct from dhcpd.conf). This shouldn't cause issues unless you have multiple DHCP servers on your network (not typical).

5.5 How to bypass Gnome keyring for normal users connecting with wireless

It's super simple! First, create a group called networkmanager with the following command (or any other method you prefer):

# groupadd networkmanager

Then add your user to that group using the following command (or any other preferred method):

# gpasswd -a username networkmanager

Replace username in the above command with your actual username.

Now, as root, launch nm-connection-editor and configure the connections:

# nm-connection-editor

Put a check mark next to "Available to all users" and apply the settings.

Note: on gnome3 you can just clik on Wireless settings, it will probably ask for password to run with root privileges.

Now you won't be bothered by Gnome keyring! (citation needed) Also, if you additionally enable "connect automatically", your connection will be available and connected before you even log in to your desktop, making your whole startup process even faster!

5.6 Missing default route

On at least one KDE4 system, no default route was created when establishing wireless connections with NetworkManager. Changing the route settings of the wireless connection to remove the default selection "Use only for resources on this connection" solved the issue.

5.7 3G modem not detected

If NetworkManager (from v0.7.999) does not detect your 3G modem, but you still can connect using wvdial, try installing modemmanager package using pacman -S modemmanager and restart NetworkManager daemon with /etc/rc.d/networkmanager restart. Replug your modem or restart. This utility provides support for hardware not in networkmanager's default database.

5.8 Switching off WLAN on laptops

Sometimes networkmanager won't work when you disable your Wifi-adapter with a switch on your laptop and try to enable it again afterwards. This is often a problem with rfkill. Install rfkill from the repo:

# pacman -S rfkill

and use

$ watch -n1 rfkill list all

to check if the driver notifies rfkill about the wireless adapter's status. If one identifier stays blocked after you switch on the adapter you could try to manually unblock it with (where X is the number of the identifier provided by the above output):

# rfkill event unblock X


5.9 Switching on wifi and bluetooth by default on laptops

Sometimes on suspend it could happen ip link stop network working, this script make the trick on my side for the moment (I will bring back information along that test)

Wifi rules

sudo touch /etc/udev/rules.d/60-wifi.rules
sudo emacs /etc/udev/rules.d/60-wifi.rules
# enable wifi
SUBSYSTEM=="rfkill", ATTR{type}=="wifi", ATTR{state}="1"

Bluetooth rules

sudo touch /etc/udev/rules.d/50-bluetooth.rules
sudo emacs /etc/udev/rules.d/50-bluetooth.rules
# enable bluetooth
SUBSYSTEM=="rfkill", ATTR{type}=="bluetooth", ATTR{state}="1"


5.10 Static IP Settings Revert To DHCP

Due to an unresolved bug, when changing default connections to static IP, nm-applet may not properly store the configuration change, and will revert to automatic DHCP. A workaround for this issue follows.

Edit the default connection (eg "Auto eth0") in nm-applet. Change the connection name (eg "my eth0"), uncheck the "Available to all users" checkbox, change your static IP settings as desired, and click Apply. This will save a new connection with the given name.

Next, you will want to make the default connection not connect automatically. To do so, run

$ sudo nm-connection-editor  # you must use sudo, not su

In the connection editor, edit the default connection (eg "Auto eth0") and uncheck "Connect automatically". Click Apply and close the connection editor.

6 Tips and tricks

6.1 Checking if networking is up inside a cron job or script

Some cron jobs require networking to be up to succeed. You may wish to avoid running these jobs when the network is down. To accomplish this, add an if test for networking that queries NetworkManager's nm-tool and checks the state of networking. The test shown here succeeds if any interface is up, and fails if they are all down. This is convenient for laptops that might be hardwired, might be on wireless, or might be off the network.

if [ `nm-tool|grep State|cut -f2 -d' '` == "connected" ]; then
       #Whatever you want to do if the network is online
else
       #Whatever you want to do if the network is offline - note, this and the else above are optional
fi

This useful for a cron.hourly script that runs fpupdate for the F-Prot virus scanner signature update, as an example. Another way it might be useful, with a little modification, is to differentiate between networks using various parts of the output from nm-tool; for example, since the active wireless network is denoted with an asterisk, you could grep for the network name and then grep for a literal asterisk.

6.2 Automatically unlock keyring after login

6.2.1 Gnome

  1. Right click on the NM icon in your panel and select Edit Connections and open the Wireless tab
  2. Select the connection you want to work with and click the Edit button
  3. Check the boxes “Connect Automatically” and “Available to all users”

Log out and log back in to complete.

Note: The following method is dated and known not to work on at least one machine!

*In /etc/pam.d/gdm (or your corresponding daemon in /etc/pam.d), add these lines at the end of the "auth" and "session" blocks if they do not exist already:

 auth            optional        pam_gnome_keyring.so
 session         optional        pam_gnome_keyring.so  auto_start
  • In /etc/pam.d/passwd, use this line for the 'password' block:
 password    optional    pam_gnome_keyring.so
Next time you log in, you should be asked if you want the password to be unlocked automatically on login.

6.2.2 SLiM login manager

  • In /etc/pam.d/slim, add these lines at the end of the "auth" and "session" blocks if they do not exist already:
 auth            optional        pam_gnome_keyring.so
 session         optional        pam_gnome_keyring.so  auto_start
  • In /etc/pam.d/passwd, use this line for the 'password' block:
 password    optional    pam_gnome_keyring.so
  • In ~/.xinitrc, add this at the very top, before launching your window manager and other applications:
 ## test for an existing bus daemon, just to be safe
 if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
    ## if not found, launch a new one
    eval `dbus-launch --sh-syntax --exit-with-session`
    echo "D-Bus per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
 fi
Next time you log in, you should be asked if you want the password to be unlocked automatically on login.

6.3 Automatically connect on boot

Since version 0.7 the NetworkManager is able to connect on boot, before a user has logged in and unlocked the keyring.

  • First make sure that the keyfile plugin is loaded; /etc/NetworkManager/NetworkManager.conf should look like this:
 [main]
 plugins=keyfile
  • If this was not in the file before, you have to restart nm-system-settings:
 # killall -TERM nm-system-settings
or simply reboot.
  • Now grant your user the right to modify system-connections:

With polkit:

Place the following in /etc/polkit-1/localauthority/50-local.d/10-org-freedesktop-network-manager-settings.pkla

[Allow user YOURUSERNAME to create wireless connections for all users]
Identity=unix-user:YOURUSERNAME
Action=org.freedesktop.NetworkManager.settings.modify.system
ResultAny=no
ResultInactive=no
ResultActive=yes
Finally, in the connection-editor (of the gnome applet), check the Available to all users box.

The connection is now saved in /etc/NetworkManager/system-connections/"CONNECTION NAME". On reboot, NetworkManager will try to connect to it, when in range.

Note: As per this bug report, knetworkmanager does not yet implement this feature. You will need to use the GNOME network applet (nm-applet). Install it as described above in this page, "killall knetworkmanager", then start nm-applet.
Please vote for the bug!

6.4 Ignore specific devices

Sometimes it may be desired that NetworkManager ignores specific devices and doesn't try to configure addresses and routes for them.

You can quickly and easily ignore devices by MAC by using the following in /etc/NetworkManager/NetworkManager.conf :

[keyfile]
unmanaged-devices=mac:00:22:68:1c:59:b1;mac:00:1E:65:30:D1:C4

After you've put this in, restart NetworkManager, and you should be able to configure interfaces without NetworkManager altering what you've set.

If that isn't appropriate, you could ignore by HAL.

  • First you have to find out the Hal UDI (e.g. with lshal):
 ...
 info.product = 'Networking Interface'  (string)
 info.subsystem = 'net'  (string)
 info.udi = '/org/freedesktop/Hal/devices/net_00_1f_11_01_06_55'  (string)
 linux.hotplug_type = 2  (0x2)  (int)
 linux.subsystem = 'net'  (string)
 ...
  • Add the udi to /etc/NetworkManager/nm-system-settings.conf:
 [keyfile]
   unmanaged-devices=/org/freedesktop/Hal/devices/net_00_1f_11_01_06_55
Multiple devices can be specified, delimited by semicolons:
 [keyfile]
   unmanaged-devices=/org/freedesktop/Hal/devices/net_00_1f_11_01_06_55;/org/freedesktop/Hal/devices/net_00_2c_6d_e2_08_af

You do not need to restart NetworkManager for the changes to take effect.

  • Ignoring a type of device at boot time.

this script was used to ignore all ethernet devices at boot time of a archiso build, it can be changed to ignore wifi devices etc. /!\being used on a non-persistant filesystem, the nm-system-settings.conf is default at run time

  #!/bin/sh
  # author: tim noise <darknoise@drkns.net>
  COUNT=0
  TARGET_FILE="/etc/NetworkManager/nm-system-settings.conf"
  for i in `lshal | grep -A6 'Networking Interface' | awk -F "'" '/info.udi = / {print $2}'`; do
      if [ $COUNT = 0 ]; then
          COUNT=$COUNT+1;
          echo "unmanaged-devices=$i" >> $TARGET_FILE
      else
          echo -n ";$i" >> $TARGET_FILE
      fi
  done
  printf "\n" >> $TARGET_FILE

6.5 Connect faster by disabling IPv6

Slow connection or reconnection to the network may be due to superfluous IPv6 queries in NetworkManager. If there is no IPv6 support on the local network, connecting to a network may take longer than normal while Network Manager tries to establish an IPv6 connection that eventually times out. The solution is to disable IPv6 within NetworkManager which will make network connection faster. This has to be done once for every network you connect to.

  • Right-click on the network status icon.
  • Click on "Edit Connections".
  • Go to the "Wired" or "Wireless" tab, as appropriate.
  • Select the name of the network.
  • Click on "Edit".
  • Go to the "IPv6 Settings" tab.
  • In the "Method" dropdown, choose "Ignore".
  • Click on "Save".

7 Acknowledgement

This wiki article is based on ArchWiki. We may have removed non-FSDG bits from it.