Network profiles
5.1
At boot, /etc/init.d/network.sh reads /etc/network.conf and brings
up the network. The script can also read another file, given as its
second argument. Keep one copy of network.conf per place, a
profile, and switch from one to the other with a single command: a
fixed address at the office, Wi-Fi with DHCP at home.
The settings in network.conf are explained in the Handbook chapter on
the network. This
page only covers using several files.
Do you need profiles?
Not to change Wi-Fi network. Every network you joined stays in
/etc/wpa/wpa.conf with its key, so joining a known network again is
just:
# trame wifi connect "HomeNetwork"
Profiles are worth it when more than the Wi-Fi name changes: wired or wireless, DHCP or a fixed address, other DNS servers.
Make the profiles
A profile is a full copy of network.conf. Start from the current
file, so every setting is there, and keep the
NETWORK_CONF_VERSION="2" line: a file without it is treated as an
old one, renamed with .old at the end and rewritten.
# mkdir /etc/network
# cp /etc/network.conf /etc/network/home.conf
# cp /etc/network.conf /etc/network/office.conf
# chmod 600 /etc/network/*.conf
The chmod matters: a profile can hold a Wi-Fi key.
Then edit each file. For example, Wi-Fi with DHCP at home:
INTERFACE="eth0"
DHCP="yes"
STATIC="no"
WIFI="yes"
WIFI_INTERFACE="wlan0"
WIFI_ESSID="HomeNetwork"
WIFI_KEY_TYPE="WPA"
WIFI_KEY="the Wi-Fi key"
and a cable with a fixed address at the office:
INTERFACE="eth0"
DHCP="no"
STATIC="yes"
IP="10.1.2.30"
NETMASK="255.255.255.0"
GATEWAY="10.1.2.1"
DNS_SERVER="10.1.2.1 9.9.9.9"
WIFI="no"
DNS_SERVER takes several addresses, separated by spaces.
Switch profiles
As root:
# /etc/init.d/network.sh restart /etc/network/office.conf
The first line printed tells which file was read:
Loading network settings from /etc/network/office.conf. To stop the
network:
# /etc/init.d/network.sh stop
Back to the default settings:
# /etc/init.d/network.sh restart
The profile used at boot
The boot always reads /etc/network.conf. To start with a profile,
copy it there:
# cat /etc/network/home.conf > /etc/network.conf
cat keeps the mode of network.conf, which the desktop network
applet needs to read.
Good to know:
- trame and the Settings window change
/etc/network.confonly, never your profiles. /etc/wpa/wpa.conf(the known Wi-Fi networks) and/etc/iproute.conf(routes for a fixed address) are shared by all the profiles.
Let a user switch
network.sh reads the file it is given as root, as a shell script. A
user who may run it with any file can run anything as root. So with
sudo, allow only the exact commands. Install sudo, then run
visudo as root:
# spk-add sudo
# visudo
and add a line like this one, for the user tux:
tux ALL = (root) NOPASSWD: /etc/init.d/network.sh restart /etc/network/home.conf, /etc/init.d/network.sh restart /etc/network/office.conf, /etc/init.d/network.sh stop
Don't use a wildcard like /etc/network/*: it also matches
/etc/network/../../home/tux/anything. The profile files must belong
to root and not be writable by the user.
The user then switches with:
$ sudo /etc/init.d/network.sh restart /etc/network/office.conf
A small script saves typing, for example /usr/local/bin/netprofile:
#!/bin/sh
# netprofile NAME: switch to /etc/network/NAME.conf
exec sudo /etc/init.d/network.sh restart /etc/network/$1.conf
Make it executable with chmod +x, then netprofile home. A name
that is not in the sudoers line is refused by sudo.
Troubleshooting
The profile became office.conf.old and a new office.conf
appeared: the file had no NETWORK_CONF_VERSION="2" line. Your
settings were kept, check the new file.
"wlan1 is not a Wi-Fi interface, changing it": the Wi-Fi
interface named in the profile does not exist. The script finds the
right one, but writes it into /etc/network.conf, not into the
profile: fix WIFI_INTERFACE in the profile by hand. iw dev lists
the Wi-Fi interfaces.
Connected, but no name resolves: with a fixed address, the
profile needs DNS_SERVER. Check the result:
$ trame network status
$ cat /etc/resolv.conf
Links
- Handbook: Network, and the network.conf file
- Wi-Fi from the console