Setting the wireless regulatory domain in Linux on your laptop

I travel internationally and want to make sure that my laptop uses the legal wireless networking frequencies in the country I am visiting. In Linux, CRDA (Central Regulatory Domain Agent) is the udev helper used to communicate between userspace and the kernel, and it enables you to view and alter the wireless regulatory domain your kernel uses. For more information see the Regulatory page on the Linux Wireless Wiki site.

CFG80211 is the Linux wireless LAN (802.11) configuration API. The kernel on my main laptop has the following configuration settings relating to CFG80211:

# cat /usr/src/linux/.config | grep CFG80211
CONFIG_CFG80211=m
# CONFIG_CFG80211_DEVELOPER_WARNINGS is not set
# CONFIG_CFG80211_REG_DEBUG is not set
CONFIG_CFG80211_DEFAULT_PS=y
# CONFIG_CFG80211_DEBUGFS is not set
# CONFIG_CFG80211_INTERNAL_REGDB is not set
CONFIG_CFG80211_WEXT=y

and the cfg80211 module is loaded:

# lsmod | grep cfg80211
cfg80211 145747 3 iwlwifi,mac80211,iwldvm

I have the package crda installed, and I have the following udev rule file /etc/udev/rules.d/regulatory.rules to allow the kernel to communicate with userspace:

KERNEL=="regulatory*", ACTION=="change", SUBSYSTEM=="platform", RUN+="/sbin/crda"

So, how do you check which wireless regulatory domain your kernel is currently using, and switch to another domain if necessary? These tasks are performed using the iw command. You’ll need to install the package iw if it is not already installed.

To see the regulatory domain your laptop is using now, enter the following command as root user:

iw reg get

When I use the above command on my laptop after start-up, I normally see the following:

# iw reg get
country 00:
(2402 - 2472 @ 40), (3, 20)
(2457 - 2482 @ 20), (3, 20), PASSIVE-SCAN, NO-IBSS
(2474 - 2494 @ 20), (3, 20), NO-OFDM, PASSIVE-SCAN, NO-IBSS
(5170 - 5250 @ 40), (3, 20), PASSIVE-SCAN, NO-IBSS
(5735 - 5835 @ 40), (3, 20), PASSIVE-SCAN, NO-IBSS

The country code 00 is not the code of the country I am in at present. To tell the kernel which wireless regulatory domain you wish to use, enter the following command as root user:

iw reg set ISO_3166-1_alpha-2

where ISO_3166-1_alpha-2 is the 2-character code for the country you are in. You can find the list of ISO 3166-1 alpha-2 codes on the Wikipedia page ISO 3166-1 alpha-2.

For example, if I were in the UK then I would enter the following command:

# iw reg set GB

and the regulatory domain would then be reported like this:

# iw reg get
country GB:
(2402 - 2482 @ 40), (N/A, 20)
(5170 - 5250 @ 40), (N/A, 20)
(5250 - 5330 @ 40), (N/A, 20), DFS
(5490 - 5710 @ 40), (N/A, 27), DFS

It is not a big deal to use the command line, but I wanted to make it even easier. I’m using KDE on my main laptop, so I created a Desktop Configuration File /home/fitzcarraldo/Desktop/Set_wireless_regulatory_domain containing the following:

[Desktop Entry]
Comment[en_GB]=
Comment=
Exec=/home/fitzcarraldo/iw_reg.sh
GenericName[en_GB]=Set wireless regulatory domain
GenericName=Set wireless regulatory domain
Icon=/home/fitzcarraldo/national-flags-icon.png
MimeType=
Name[en_GB]=Set_wireless_regulatory_domain
Name=Set_wireless_regulatory_domain
Path=
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=

and gave it the following file permissions:

# chmod 744 /home/fitzcarraldo/Desktop/Set_wireless_regulatory_domain
# ls -la /home/fitzcarraldo/Desktop/Set_wireless_regulatory_domain
-rwxr--r-- 1 fitzcarraldo users 496 Jan 15 21:53 /home/fitzcarraldo/Desktop/Set_wireless_regulatory_domain

I used a search engine to find a nice PNG icon consisting of several overlapping national flags, and saved it with the file name name national-flags-icon.png in my home directory.

I created a Bash shell script /home/fitzcarraldo/iw_reg.sh containing the following:

#!/bin/bash
echo "First you need to enter the password of your user account..."
sudo echo ""
echo "The ISO 3166-1 alpha-2 codes are listed on Web page https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2"
echo ""
echo "The current wireless regulatory domain is set as: "
echo ""
sudo iw reg get
echo ""
echo -n "Enter the ISO 3166-1 alpha-2 code (upper case) for the country you are in now, and press ENTER: "
read REGULATORYDOMAIN
sudo iw reg set $REGULATORYDOMAIN
echo ""
echo "The current wireless regulatory domain is now set as: "
echo ""
sudo iw reg get
echo ""
echo "All done. You can close this window."

and gave it the following file permissions:

# chmod 744 /home/fitzcarraldo/iw_reg.sh
# ls -la /home/fitzcarraldo/iw_reg.sh
-rwxr--r-- 1 fitzcarraldo users 632 Jan 15 21:33 /home/fitzcarraldo/iw_reg.sh

Now, if I double-click on the icon for Set_wireless_regulatory_domain on my desktop, a Konsole window pops up with a prompt for me to enter my user account password. When I enter my password the window displays the current wireless regulatory domain the kernel is using and prompts me to enter the 2-character code for the regulatory domain I wish to use instead. When I enter the country code the window displays the new regulatory domain, as shown in the sample below.


First you need to enter the password of your user account...
Password:

The ISO 3166-1 alpha-2 codes are listed on Web page https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

The current wireless regulatory domain is set as:

country SA:
(2402 - 2482 @ 40), (N/A, 20)
(5170 - 5250 @ 20), (3, 23)
(5250 - 5330 @ 20), (3, 23), DFS
(5735 - 5835 @ 20), (3, 30)

Enter the ISO 3166-1 alpha-2 code (upper case) for the country you are in now, and press ENTER: GB

The current wireless regulatory domain is now set as:

country GB:
(2402 - 2482 @ 40), (N/A, 20)
(5170 - 5250 @ 40), (N/A, 20)
(5250 - 5330 @ 40), (N/A, 20), DFS
(5490 - 5710 @ 40), (N/A, 27), DFS

All done. You can close this window.

The task of viewing and changing the regulatory domain after start-up is now very easy for me. The only thing that would be easier than this would be if Linux could detect automatically which country I’m in and set the regulatory domain automatically.

EDIT (July 10, 2015): Note the following restriction when changing the regulatory domain: Helping compliance by allowing to change regulatory domains. See also the Gentoo Forums thread [solved] Atheros and regulatory domain.

Converting ape music files to mp3 in Linux

I had a file in the lossless ape (Monkey’s Audio) file format, and wanted to convert it to a .mp3 file so that I could play it on my portable mp3 player. As is usual in Linux, several alternative solutions exist, and I thought I’d try three of them for fun: shntool, ffmpeg and KDE’s Konvertible (Konvertible is a GUI for ffmpeg).

I already had ffmpeg and Konvertible installed, but not shntool. So first I installed shntool and the Monkey’s Audio codecs it uses:

# emerge media-sound/mac
# emerge media-sound/shntool

Here are the details of these two installed packages:

# eix -I shntool
[I] media-sound/shntool
Available versions: 3.0.10-r1 {alac flac mac shorten sox wavpack}
Installed versions: 3.0.10-r1(08:11:30 19/12/12)(flac -alac -mac -shorten -sox -wavpack)
Homepage: http://www.etree.org/shnutils/shntool/
Description: A multi-purpose WAVE data processing and reporting utility

# eix -I media-sound/mac
[I] media-sound/mac
Available versions: 3.99.4.5.7-r1^m {mmx static-libs}
Installed versions: 3.99.4.5.7-r1^m(07:52:12 19/12/12)(mmx -static-libs)
Homepage: http://etree.org/shnutils/shntool/
Description: Monkey's Audio Codecs

Then I used the following command to convert the file My Band 1971 CoolSounds.ape to mp3:

$ shntool conv -i ape -o 'cust ext=mp3 lame - %f' My\ Band\ 1971\ CoolSounds.ape
Converting [My Band 1971 CoolSounds.ape] (59:15.39) --> [My Band 1971 CoolSounds.mp3] : 100% OK
$

The KDE utility Konvertible was also able to convert it. I double-clicked on the file My Band 1971 CoolSounds.ape in Dolphin to launch Konvertible, selected libmp3lame in the ‘Codec:’ drop-down picklist, 192.00 kbits/s in the ‘Bitrate:’ drop-down picklist, clicked on the folder icon and selected /home/fitzcarraldo as the destination directory, and finally clicked ‘Convert’.

The mp3 files created by shntool and Konvertible were of different sizes:

File created by Konvertible:

$ file My\ Band\ 1971\ CoolSounds.mp3
My Band 1971 CoolSounds.mp3: Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, Stereo
$ ls -la My\ Band\ 1971\ CoolSounds.mp3
-rw-r--r-- 1 fitzcarraldo users 85334024 Dec 19 08:11 My Band 1971 CoolSounds.mp3
$

File created by shntool:

$ file My\ Band\ 1971\ CoolSounds.mp3
My Band 1971 CoolSounds.mp3: MPEG ADTS, layer III, v1, 128 kbps, 44.1 kHz, JntStereo
$ ls -la My\ Band\ 1971\ CoolSounds.mp3
-rw-r--r-- 1 fitzcarraldo users 56889259 Dec 19 08:29 My Band 1971 CoolSounds.mp3
$

So I added the bitrate to the shntool command:

$ shntool conv -i ape -o 'cust ext=mp3 lame -b 192 - %f' My\ Band\ 1971\ CoolSounds.ape
Converting [My Band 1971 CoolSounds.ape] (59:15.39) --> [My Band 1971 CoolSounds.mp3] : 100% OK
$

and this time the mp3 file created by shntool is comparable to the mp3 file created by Konvertible:

$ file My\ Band\ 1971\ CoolSounds.mp3
My Band 1971 CoolSounds.mp3: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo
$ ls -la My\ Band\ 1971\ CoolSounds.mp3
-rw-r--r-- 1 fitzcarraldo users 85333889 Dec 19 08:56 My Band 1971 CoolSounds.mp3
$

The ffmpeg command to do the same thing is:

$ ffmpeg -i My\ Band\ 1971\ CoolSounds.ape -ar 44100 -ab 192000 out.mp3
ffmpeg version 0.10.6 Copyright (c) 2000-2012 the FFmpeg developers
built on Nov 26 2012 07:06:40 with gcc 4.6.3
configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --enable-shared --cc=x86_64-pc-linux-gnu-gcc --cxx=x86_64-pc-linux-gnu-g++ --ar=x86_64-pc-linux-gnu-ar --optflags='-O2 -march=native -pipe' --extra-cflags='-O2 -march=native -pipe' --extra-cxxflags='-O2 -march=native -pipe' --disable-static --enable-gpl --enable-version3 --enable-postproc --enable-avfilter --disable-stripping --disable-debug --disable-doc --disable-vaapi --disable-vdpau --enable-runtime-cpudetect --enable-gnutls --enable-libmp3lame --enable-libvo-aacenc --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-libfaac --enable-nonfree --enable-libdc1394 --enable-openal --disable-indev=v4l --disable-indev=oss --enable-x11grab --enable-libpulse --disable-outdev=oss --enable-libfreetype --enable-pthreads --enable-libgsm --enable-libspeex --disable-amd3dnow --disable-amd3dnowext --disable-altivec --disable-avx --disable-mmx2 --disable-ssse3 --disable-vis --disable-neon --cpu=ho
libavutil 51. 35.100 / 51. 35.100
libavcodec 53. 61.100 / 53. 61.100
libavformat 53. 32.100 / 53. 32.100
libavdevice 53. 4.100 / 53. 4.100
libavfilter 2. 61.100 / 2. 61.100
libswscale 2. 1.100 / 2. 1.100
libswresample 0. 6.100 / 0. 6.100
libpostproc 52. 0.100 / 52. 0.100
Input #0, ape, from 'My Band 1971 CoolSounds.ape':
Metadata:
Album : CoolSounds
Title : C:\1\My Band 1971 CoolSounds
Comment : Exact Audio Copy
Duration: 00:59:15.47, start: 0.000000, bitrate: 829 kb/s
Stream #0:0: Audio: ape (APE / 0x20455041), 44100 Hz, stereo, s16
Output #0, mp3, to 'out.mp3':
Metadata:
TALB : CoolSounds
TIT2 : C:\1\My Band 1971 CoolSounds
Comment : Exact Audio Copy
TSSE : Lavf53.32.100
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16, 192 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (ape -> libmp3lame)
Press [q] to stop, [?] for help
size= 83334kB time=00:59:15.55 bitrate= 192.0kbits/s
video:0kB audio:83333kB global headers:0kB muxing overhead 0.000892%
$

and, as you can see below, the resulting mp3 file is the same size as the mp3 file created using Konvertible (not surprising, since Konvertible is a GUI front-end for ffmpeg) and virtually the same as the mp3 file created by shntool.

$ file out.mp3
out.mp3: Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, Stereo
$ ls -la out.mp3
-rw-r--r-- 1 fitzcarraldo users 85334024 Dec 20 18:14 out.mp3
$

So, there you have it: GUI or command line; take your pick!