An Introduction to systemd

 What is systemd?

Systemd (Stylized ‘systemd’) is process one on Linux distributions that have selected it as their default init system. It is designed specifically with the Linux kernel API in mind.

Being a replacement for SysVinit (the previous init system inherited from UNIX System V and BSD,) and the first process executed in user space, systemd is extremely important.

Currently systemd is not the default in many systems, but the number of adopters is growing, and soon server OSs such as Debian and RHEL will be using it. At present you would find systemd in default installs of the below, to name a few:

  • Fedora
  • Arch
  • CoreOS

Using systemd

systemd is vastly different in terms of the commands that you might use on a day to day basis. As a result of this I am going to list some common changes below.

All of this testing was performed on an Arch system, so your mileage may vary.

Services

On older systems, you might use commands similar to the following when working with services:

# /etc/init.d/ntpd start
# service ntpd start
# chkconfig ntpd on

 

On systemd systems, your commands will be slightly different.

Starting and Stopping

To start a service, you would use the following:

# systemctl start ntpd

 

To stop a service, the opposite:

# systemctl stop ntpd

 

Enabling and Disabling

Obviously starting and stopping services is good, but what if we want to start them at boot?

# systemctl enable ntpd

 

This command actually runs the following:

# ln -s '/usr/lib/systemd/system/ntpd.service' '/etc/systemd/system/multi-user.target.wants/ntpd.service'

 

To disable you would use:

# systemctl disable ntpd

 

Again, this is a link to:

# rm '/etc/systemd/system/multi-user.target.wants/ntpd.service'

 

Working with Logging

While you can, and a lot of people do, simply install syslog to most systemd systems it is recommended that you learn to use the tool ‘journalctl’ to manage and view logs.

Viewing Logs

If you want to view all of your logs, then the following command will work, but might be verbose:

# journatlctl

 

If you wanted to view from only a specific date, you could use:

# journalctl --since="2014-05-07"

 

Specifically kernel logs? That’s easy too:

# journalctl -k

 

Or maybe you want to follow the log, as it logs!

# journalctl -f

 

But hang on a moment, you know the exact PID that you want to look at, how brilliant are you?! (The PID selected here is systemd.)

# journalctl _PID=1

 

What’s that? You don’t know the PID? But you know the unit name? No problem!

# journatlctl -u sshd

 

Still not enough information? Get more verbosity!

# journalctl -x

 

Working with Networking

Let’s say you need to add an IP to a host, because it’s brand new and doesn’t have anything yet. Thankfully, you’re stood in front of it at a prompt

What do you do?

Adding a Static IP Addresses

For this we’re going to use a file, as it’s fairly easy.

# cd /etc/netctl
# touch eth0

 

Once we’ve created the file, we will add the following using our favourite editor (vim,) to configure an IPv4 address:

Description='Our primary connection'
Interface=eth0
Connection=ethernet
IP=static
Address=('192.168.6.10/24')
Gateway='192.168.6.1'
DNS=('8.8.8.8')

 

We can start this interface with the ‘netctl’ command:

# netctl start eth0

 

And again, enable it in a similar way:

# netctl enable eth0

 

Adding a Dynamic IP Address

Maybe you’re at home, and your systemd box is something like XBMC? Maybe you don’t need a static address, and dynamic is fine. Well there’s good news, you would populate your ‘eth0′ file with something like this:

Description='Our DHCP Interface'
Interface=eth0
Connection=ethernet
IP=dhcp

 

That’s much shorter, and it uses the same ‘netctl’ commands to start and enable!

Using New Tools

Managing your Hostname

This sort of management is done by the command ‘hostnamectl’ such as:

# hostnamectl status

 

If you wanted to set your hostname, it couldn’t be simpler:

# hostnamectl set-hostname pi.dmcip.com

 

This will also persist across reboots!

Managing Time

Something we’d all like to be able to do…

# timedatectl status

 

This will give you a slew of facts, similar to the below:

      Local time: Wed 2014-05-07 06:49:36 MDT
  Universal time: Wed 2014-05-07 12:49:36 UTC
        RTC time: n/a
       Time zone: America/Denver (MDT, -0600)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2014-03-09 01:59:59 MST
                  Sun 2014-03-09 03:00:00 MDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2014-11-02 01:59:59 MDT
                  Sun 2014-11-02 01:00:00 MST

 

Obviously this happens to be completely wrong, so let’s fix it!

We first need to find out the timezones available to us:

# timedatectl list-timezones

 

When we have the one we know is right, we use the following:

# timedatectl set-timezone Europe/London

 

Run date again, or timedatectl status, and you will see your changes!

This entry was posted in Linux and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply