Systemd

From Christoph's Personal Wiki
Revision as of 22:48, 15 July 2020 by Christoph (Talk | contribs) (journalctl)

Jump to: navigation, search

systemd is a suite of system management daemons, libraries, and utilities designed as a central management and configuration platform for the Linux computer operating system.

Example usage

  • Check if you are running "init" or "systemd":
$ cat /proc/1/comm
systemd
  • Restart network:
$ systemctl restart network.target
$ systemctl stop firewalld
$ systemctl mask firewalld
  • Miscellaneous:
$ systemctl list-units
$ systemctl list-units -t service
$ systemctl list-units | grep .service
$ systemctl list-units -t target
$ systemctl list-unit-files
$ systemctl list-unit-files -t target
$ systemctl list-dependencies multi-user.target
$ systemctl [status|stop|enable|disable|restart] ssh.service
$ systemctl is-enabled ssh.service
$ systemctl edit --full docker.service
$ systemctl cat ssh.service
$ systemctl show --property=ExecStart docker.service
$ systemctl [reboot|poweroff|suspend]
$ systemctl --failed
  UNIT              LOAD   ACTIVE SUB    DESCRIPTION
● pollinate.service loaded failed failed Seed the pseudo random number generator on first boot
● vboxadd.service   loaded failed failed LSB: VirtualBox Linux Additions kernel modules
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
  • cgroup tree
$ systemd-cgls
├─1 /sbin/init
├─system.slice
│ ├─dbus.service
│ │ └─776 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
│ ├─cron.service
│ │ └─692 /usr/sbin/cron -f
...
  • ps with cgroups:
$ alias psc='ps xawf -eo pid,user,cgroup,args'

Related commands

systemd-analyze

  • Analyze system boot-up performance
$ systemd-analyze
Startup finished in 5.223s (kernel) + 7.781s (userspace) = 13.004s

# ~OR~

$ systemd-analyze  # AWS EC2 instance (t2-micro):
Startup finished in 1.588s (kernel) + 3.100s (initrd) + 27.516s (userspace) = 32.206s
multi-user.target reached after 11.735s in userspace

# ~OR~

$ systemd-analyze  # System76 laptop running Ubuntu 18.04:
Startup finished in 3.993s (firmware) + 29.599s (loader) + 5.337s (kernel) + 12.725s (userspace) = 51.655s
graphical.target reached after 12.717s in userspace
  • Plot all dependencies of any unit whose name starts with "avahi-daemon":
$ systemd-analyze dot 'avahi-daemon.*' | dot -Tsvg > avahi.svg
$ eog avahi.svg
  • Plot the dependencies between all known target units:
$ systemd-analyze dot --to-pattern='*.target' --from-pattern='*.target' | dot -Tsvg > targets.svg
$ eog targets.svg

journalctl

Note: combine with syslog-ng for backward compatibility.

$ journalctl
$ journalctl | grep -Ei 'error|fail'
$ journalctl -b  # show only logs from this boot
$ journalctl -b -1 # show only logs from previous boot
$ journalctl -u ssh  # show only logs for a given unit
$ journalctl -f  # follow (somewhat analogous to `tail -f /var/log/messages`)
$ journalctl -f -u ssh.service  # show only logs for ssh unit and follow
  • Show logs for a given date/time period:
$ journalctl -u ssh --since="2014-12-06 23:35:00"
$ journalctl --since "2014-12-06" --until "2014-12-07 03:00"
$ journalctl --since yesterday
$ journalctl --since 03:00 --until "1 hour ago"
$ journalctl -u ssh.service --since="5 minutes ago"
Cleanup journal logs (i.e., the self-maintenance method is to vacuum the logs by size or time)
  • Retain only the past two days:
$ journalctl --vacuum-time=2d
  • Retain only the past 500 MB:
$ journalctl --vacuum-size=500M

For an even more robust cleanup:

$ journalctl --flush --rotate
$ journalctl --vacuum-time=1s

See: man journalctl for more information.

timedatectl

Note: Most of these commands will need to be run as either root or sudo and is only valid for systems using systemd.

  • List all available timezones on your computer/server:
$ timedatectl list-timezones
  • Set your computer's/server's timezone:
$ timedatectl set-timezone region/timezone
  • For instance, to set your timezone to United States Pacific Time (PST; -8GMT):
$ timedatectl set-timezone America/Vancouver

Your system will be updated to use the selected timezone. You can verify with:

$ timedatectl
      Local time: Fri, 2012-11-02 09:26:46 CET
  Universal time: Fri, 2012-11-02 08:26:46 UTC
        RTC time: Fri, 2012-11-02 08:26:45
        Timezone: Europe/Warsaw
      UTC offset: +0100
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: no
 Last DST change: CEST → CET, DST became inactive
      Sun, 2012-10-28 02:59:59 CEST
      Sun, 2012-10-28 02:00:00 CET
 Next DST change: CET → CEST, DST will become active
      the clock will jump one hour forward
      Sun, 2013-03-31 01:59:59 CET
      Sun, 2013-03-31 03:00:00 CEST
  • Enable an NTP daemon (chronyd):
$ timedatectl set-ntp true
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-ntp ===
Authentication is required to control whether network time synchronization shall be enabled.
Authenticating as: user
Password: ********
==== AUTHENTICATION COMPLETE ===

$ systemctl status chronyd.service
chronyd.service - NTP client/server
Loaded: loaded (/lib/systemd/system/chronyd.service; enabled)
Active: active (running) since Fri, 2012-11-02 09:36:25 CET; 5s ago
...

hostnamectl

`hostnamectl` allows you to control the system hostname.

  • Example response from a laptop running Ubuntu:
$ hostnamectl
   Static hostname: my_hostname
         Icon name: computer-laptop
           Chassis: laptop
           Boot ID: ffffffffffffffffffffffffffffffff
  Operating System: Ubuntu 14.04.2 LTS
            Kernel: Linux 3.13.0-52-generic
      Architecture: x86_64
  • Example response from a vagrant box running Fedora:
$ hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ffffffffffffffffffffffffffffffff
           Boot ID: ffffffffffffffffffffffffffffffff
    Virtualization: oracle
  Operating System: Fedora 22 (Twenty Two)
       CPE OS Name: cpe:/o:fedoraproject:fedora:22
            Kernel: Linux 4.0.4-303.fc22.x86_64
      Architecture: x86-64
  • Example response from an AWS EC2 instance running CentOS 7:
$ hostnamectl
   Static hostname: ip-172-22-1-210.us-west-2.compute.internal
Transient hostname: ip-172-22-1-210
         Icon name: computer-vm
           Chassis: vm
        Machine ID: f32e0af35337b5dfcbedcb0d1de8dca1
           Boot ID: ea5461881a264a88abe239b2337169bf
    Virtualization: xen
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.10.1.el7.x86_64
      Architecture: x86-64
  • Change the hostname:
$ sudo hostnamectl --transient set-hostname $hostname
$ sudo hostnamectl --static set-hostname $hostname
$ sudo hostnamectl --pretty set-hostname $hostname

Other

See: for a complete list

  • Control the system locale and keyboard layout settings:
$ localectl 
  System Locale: LANG=en_US.UTF-8
      VC Keymap: n/a
     X11 Layout: us
      X11 Model: pc105
$ loginctl  # Control the systemd login manager
$ busctl  # Introspect the bus
$ machinectl # Control the systemd machine manager
$ networkctl # Query the status of network links
$ systemd-cgls  # Recursively show control group contents
$ systemd-cgtop  # Show top control groups by their resource usage
$ systemd-path  # List and query system and user paths

External links