Why SystemD will start your next Linux system
For a long time, the default boot and initialization mechanism in Linux was the System V (SysV) init daemon. Along the way, many Linux distributions have attempted to move away from this primitive form of booting. Why you ask? For one thing, it makes the boot process a lot slower, since processes have to be started one at a time. It is also a nightmare to maintain the scripts that Administrators write to manage these start processes, as each package maintainer or administrator, does ‘their own thing.’
So Ubuntu, one of the most popular Linux distributions moved to upstart in late 2006, but this init daemon they adopted, was not widely embraced. Debian, OpenSuse, Fedora, and Red Hat at some point in the last five years supported upstart. Red Hat Enterprise Linux (RHEL) version 6 and derivatives such as CentOS still use upstart. But all of them are ditching upstart for something better, i.e. SystemD. In fact, the Debian technical committee recently voted and they will switch to SystemD in upcoming releases, leaving Ubuntu no choice but to follow.
It now looks like SystemD will be the default initialization and boot method for Linux systems for many years to come. But what does systemD do different?
What is SystemD?
SystemD, is a system and services manager for Linux that was developed by a team from Red Hat, spearheaded by Lennart Poettering, the creator of pulseaudio, and Avahi. SystemD makes Linux systems boot faster since processes are ran in parallel. Once started, processes are monitored, and restarted if they crash. SystemD also reduces the dependency on shell scripts to do much of the work.
Other systemD features, include a cron-like job scheduler, an integrated login manager, which offers ‘multi-seating’ functions. It also has a new an improved logging mechanism called the journal, that will likely replace syslog, the current logging facility on Linux systems.
Below is some technical information to get you as the administrator started with systemD. Please note that there are several manuals and online resources available that will help you understand systemD, I will mention a few at the end of this post.
How to get started with systemD
SystemD is installed by default on recent versions of Fedora, and Arch Linux. For other distributions like Debian and Ubuntu, you might have to wait a bit for systemd to be packaged as the default init system. It is actually recommended that installation is done by the distribution vendors. But if you are the brave type, you can of course install it from source.
Pick up the source tarball and follow the instructions contained in the README file included with the sources.
After installation is completed, you will need to start converting your old startup scripts to systemD. At this time, only SysV init scripts for commonly used services, have been ported to systemD. So you will need to know how to write a service file for rarely used daemons or for that program you are developing. Below is an example of a systemd service file. By the way, these are what those files in “/etc/init.d” used to be.
How to write a systemD service file
Open up your favorite editor such as vim as follows:
vim /tmp/squid.service Am using squid here as an example, so replace it with the name of your service. Enter the following snippet:
[Unit]
Description=Squid caching proxy
After=syslog.target network.target nss-lookup.target
[Service]
Type=forking
EnvironmentFile=/etc/sysconfig/squid
ExecStartPre=/usr/libexec/squid/cache_swap.sh
ExecStart=/usr/sbin/squid $SQUID_OPTS -f $SQUID_CONF
ExecReload=/usr/sbin/squid $SQUID_OPTS -k reconfigure -f $SQUID_CONF
ExecStop=/usr/sbin/squid -k shutdown -f $SQUID_CONF
PIDFile=/var/run/squid.pid
[Install]
WantedBy=multi-user.target
At a minimum, you will need these three sections; unit, service, install.
Unit definitions
Description: A string describing the service and what it does.
After: If service depends on another one, then indicate that it should come after those targets. Notice in my example that one of the pre-requisites for this service is the network service. You will see that often, as most services expect the network to be already running, so always include it.
You can also add ‘Before’ which does the opposite and starts this service before units listed.
Hint: You can read more about unit definitions by calling up the manpage: man systemd.unit
Service definitions
Type: You must always have a type, which can either be forking, simple, oneshot, dbus, or notify. For most daemons, forking should suffice, as it forks the processes by putting them in the background after start.
EnvironmentFile:This is the file which contains the options used by your service daemon. This can be omitted, but can be a life saver occasionally
ExecStartPre: This is the command that is called before the main process – see ExecStart
ExecStart: This is the command that the daemon runs. You can also point to a script on your system e.g. =/bin/sh -c “/usr/sbin/my-cool-script.sh”
ExecStartPost: This is the command that is called after the main process – see ExecStart
ExecReload: This is the command that is called in order to reload the daemon’s configuration file.
ExecStop: This is the command that is used to stop your service.
PIDFile: Enter the file path containing the process ID of your service. SystemD reads this file after startup.
Hint: You can read more about service definitions by calling up the manpage: man systemd.service
Install definitions
This section is optional, but can be used to set the target level
WantedBy: Set a target where this service will be grouped. Like run levels in SysV language.
Hint: You can read more about various special targets by calling up the manpage: man systemd.special
Activating your systemd service
When you are done writing the service file, copy it to: /usr/lib/systemd/system/
cp -v /tmp/squid.service /usr/lib/systemd/system/
You will need reload the systemd daemon:
systemctl --system daemon-reloadIn order for the service to start automatically at boot time, enable your systemd service as follows:
systemctl enable squid.service
Now when you want to start, stop, reload or check the status of this service; do the following:
systemctl COMMAND squid.service ; where COMMAND is either {start|stop|reload|status}
This how-to barely scratches the surface. There are lots of neat tricks you can perform with systemD that your old dog SysV and upstart could not. So take the time to learn it.
Resources:
SystemD website: http://www.freedesktop.org/wiki/Software/systemd
Lennart’s Blog: http://0pointer.de/blog(Look for the SystemD series for Administrators)
Mailing List: http://lists.freedesktop.org/archives/systemd-devel
Man pages: systemd- (Use tab completion to see all available man pages)
How to setup a 3G Wireless Hotspot using Linux
The prevalent and most used type of Internet in Uganda currently is 3G via a USB modem or dongle. Have you found yourself somewhere; may be on travel, or at home, and you need to share your Internet connection with a friend or family members via WiFi? Do you wish you could just plug in your laptop, and just use your WiFi-enabled phone or tablet to surf? Are you a Linux user? Well its very easy to setup a WiFi hotspot for yourself using free and open source software.
There are several ways to do this, however, if you use Linux say on your laptop (or server), what you need is an application called hostapd. It is already installed in most Linux distributions like Redhat/Fedora, CentOS, Ubuntu, Debian, etc. If not simply install it as follows:
yum install hostapd (or use apt get for Debian based systems like Ubuntu)
Of course, you will need to have a Wireless device enabled on your laptop (or server box), and the chipset should support AP (Access Point) mode. To test this:
iw list (Look for modes section to see if AP mode is supported on your Wireless device
Also, you need to setup the Wireless interface device. In this post I am using an IP address of 172.16.0.1 for my Hotspot and ‘wlan0’ as the device name e.g.
ifconfig wlan0 172.16.0.1 Please remember that the above will not survive a reboot, so you need to permanently add it to your network configuration. On Redhat based systems:
vim /etc/sysconfig/network-scripts/ifcfg-wlan0Add these parameters:
TYPE=Wireless
BOOTPROTO=static
IPADDR0=172.16.0.1
NAME=”wlan0″
ONBOOT=yes
Now edit the configuration file for hostapd to suit your needs.
vim /etc/hostapd/hostapd.conf A typical hostapd.conf file looks like this:
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
wpa_passphrase=AVERYSECRETPASSPHRASE
interface=wlan1
ssid=MY-HOTSPOT
hw_mode=g
channel=6
driver=nl80211
The most important changes to make in the above file are:
ssid: The name you want your hotspot to be called
wpa: The encryption type. Use WPA2 and above always
wpa_passphrase: the password users will need to access your hotspot
After you have modified and saved the above file, enable hostapd services, so they also start during boot time.
systemctl enable hostapd.service or chkconfig hostapd on (old way)
systemctl start hostapd.service or service hostapd start (old way)
You will need some facility to dynamically offer IP addresses to clients of your hotspot. One very useful tool is called dnsmasq. Dnsmasq is a lighweight DHCP and DNS server. You can install it using your package manager e.g.
yum install dnsmasq Once installed, edit the configuration file for dnsmasq to suit your environment:
vim /etc/dnsmasq.confThe most important changes to make in the above file are:
interface=wlan0
domain=example.com
dhcp-range=172.16.0.10,172.16.0.199,12h
dhcp-option=3,172.16.0.1
Enable dnsmasq to start now and at boot time:
systemctl enable dnsmasq.service or chkconfig dnsmasq on (old way)
systemctl restart dnsmasq.service or service dnsmasq restart (old way)
The next step is to dial your ISP using either the GUI (NetworkManager) or the CLI tools like the ones I wrote about earlier. If you use the GUI tools, you will need to allow the laptop (server) to act as a gateway. This can be achieved simply:
/bin/echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE (NOTE: This assumes you are connected using a 3G modem with an assigned interface node of ppp0)
Once you are connected, and gateway has been setup, your clients should be able to connect to your Hotspot using the SSID and passphrase you setup above.
That’s it. Happy New year!
DVB-T tuning data for Uganda
I recently submitted initial DVB-T scan data for Kampala to the dvb-apps package upstream. If you are testing DVB-T2 in Uganda using Linux, please go ahead and clone the latest git sources at:
http://git.linuxtv.org/dtv-scan-tables.git
Its still early, but at least with the latest sources, you do not have to tinker around in your app.
Also, I’m looking for testers in the region. If you’re already using DVB-T with Linux, please contact me.
How to create your own Linux install image
I was reading this google+ post by Linux’ very own Linus Torvalds. One of the comments reads “I feel like Mom and Dad are fighting. I love Fedora and I love Linux :)” Clearly, Linux distributions come with their own specific set of packaging rules and guidelines. Sometimes, it does not make sense to everyone. And when that someone else is Linus, people will go ‘bonkers’ to either agree or disagree with him. But why the noise?
To any one who’s not ventured into the murky waters of Linux kernel building and packaging, you may be wondering why a topic like this one solicits such responses even if its not coming from the man who invented Linux. Believe me – it is a big deal.
Due to the rapid development of open-source software, the Linux kernel and packages on the original CD/DVD images tend to become outdated over a short period of time. Sometimes, those packages become insecure and vulnerable. Furthermore, with all the latest and cool hardware devices that vendors are producing every other day, the Linux kernel has to be upgraded so as to be fully usable by those devices.
So in my opinion, Linus Torvalds is half right, since non-techie users need to get Linux working straight out of the box. My feeling though is that the main distros are really not meant for the Linux newbies. That is why re-spins seem to be a lot popular with new Linux users. So if you made it this far and are still reading this, how do you go about creating a re-spin? Here is one way:
Lately, I’ve been spending sometime, creating customized versions of the Fedora Linux images. I use these Fedora re-spins for the embedded products I’ve been working on such as the JambulaPi, TV STB etc. BTW, these re-spins of Fedora are open source and free, so I plan on posting them to a public site in a not too distant future!
A very useful tool for creating custom images for Fedora Linux is called livemedia-creator. Livemedia-creator uses Anaconda, kickstart and Lorax to create bootable media such as live iso’s that use the same install path as a normal system install.
To install it:
sudo yum install lorax
Then, take a look at the readme file located at /usr/share/doc/lorax-*/README.livemedia-creator. There you will find examples on how to set up your very own customized Linux distribution. A kickstart file is highly recommended as you can add other repositories for installing software that is not originally included in the Fedora releases. Also, it lets you do things like adding initial users, etc. Take a look at Kickstarting Fedora Linux installations.
How to install Asterisk 11 – Part 1
Asterisk is the popular open source telephony platform. With just a few hours to spare, you can turn any spare PC in to a world class PBX system. In part 1, I will list the steps involved in installing asterisk using sources. The steps below can be used with RedHat based systems like Fedora. It shouldn’t be that hard to replicate on other Linux distributions like Ubuntu.
Update your system and reboot if there’s a kernel upgrade:
yum update
For Redhat systems, disable selinux:
sed s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
Install required dependencies:
yum -y install make wget openssl-devel ncurses-devel newt-devel libxml2-devel kernel-devel gcc gcc-c++ sqlite-devel
Download current asterisk 11 sources:
cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-11-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/libpri/libpri-1.4-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-current.tar.gz
Note: Dahdi is only needed if you will be using a telephony card to access the PSTN. Timing provided by dahdi is also no longer needed with the new conferencing capabilities in Asterisk 11
Unpack and install the source packages. Please note that the installation order is very important:
a) dahdi
tar zxvf dahdi-linux-complete-current.tar.gz
cd dahdi-linux-complete*
make && make install
make config (Generates sample configs)
b) libpri
tar zxvf libpri-1.4-current.tar.gz
cd libpri-1.4*
make && make install
c) tar zxvf asterisk-11-current.tar.gz
cd asterisk*
./configure --libdir={LIBDIR} e.g /usr/lib64 (use uname -a to confirm architecture"
make menuselect (optional)
make && make install
make samples (on fresh install only)
make config
Start services
service dahdi start
service asterisk start
Check to ensure is running:
asterisk -vr
In part 2, I will go over how to configure your asterisk server.
Setup an instant messaging server for your office
Email is a great tool for communication and that is why even in the age of social networks, it persists. However, if you work in an office environment that requires constant and real-time communication between users, email may not cut it. Sometimes you just want to send a quick message without clogging your users Inboxes. What you need is an instant messaging system.
There are quite a number of open source based IM messaging servers; however, I like and have used jabberd over the years. Jabberd2 is an open source implementation of the jabber/XMPP protocol used by popular IM clients.
Installation of jabberd2 on Linux is fairly easy, although you need to know your way around the command line interface and how to edit configuration files.
To start with, install jabberd2. On Fedora/CentOs/RedHat systems:
yum install jabberd
After installation, you will find all configuration files located under /etc/jabberd
You will need to edit the files sm.xml s2s.xml c2s.xml, router.xml; and templates/roster.xml to suit your environment. Then setup your user accounts in your preferred driver backend. Detailed configuration instructions can be found here.
Now all you need to do is install an IM client on your users PCs. There are several of these for all platforms. On Linux; favorites include pidgin and empathy
One cool thing with jabberd2, is that it is very extensible and you can add chat rooms functionality; a sort of ‘circles’ – old school style
Git – The open source developer’s favorite version control system
Today, my brother sent me a link to Clay Sirky’s talk on the political ramifications of ‘cooperation without coordination’ on communities – How the internet will one day transform government. Knowing how much an advocate for open source software I am, he couldn’t have picked a good time. I’ve been thinking of recommending an open source tool called git on this blog.
OK, I won’t comment on the politics here, but I encourage you to listen to this talk, even if you’re not a software developer.
Interestingly, Clay Sirky mentions the power of collaborative software development using git, which enables one developer in Edinburgh to collaborate with another developer in Entebbe (Uganda) without them knowing each other.
So what is git? Git is an open source based distributed version control system, that allows software developers to collaborate and share code. Unlike traditional systems that require a central server, with git, anyone can clone a repository and make changes locally, that can be pushed upstream. Git was created by the man who invented Linux i.e. Linus Torvalds, so you know it embodies the open source spirit!
I recently migrated all my software development projects to using git, I am really loving it as it makes keeping track of the projects a lot easier than before.
I won’t delve into how to use git here, but a good How-To can be found at:
Getting Started with Git
If you are an open source developer and you’ve not yet started using git, you are really missing out.
Try it and you’ll not look back.
Remind: A Cool CLI based UNIX program
I have started a category on this blog featuring cool open source programs. These are mainly command line based, since that is where I spend most of my time. I will also pick those programs that have real life and practical applications.
Remind: As its name suggests is a reminder application. I use it to remind me of birthdays, anniversaries, holidays, etc
To install it: Use your favorites package manager such as apt or yum:
Using Yum: yum -v install yum
Typical Usage: remind [options] [filename]
e.g. remind ../mybirthdays
Typical contents of the file: mybirthdays:
REM 1 Feb MSG Peter’s birthday
This tells the app to remind you that it is Peter’s Birthday on Feb 1.
You can now script this command and do all sorts of cool things, like sending an email or SMS to your phone, when there’s a reminder.
For Help: Type ‘man remind’ to get a detailed how to


