Difference between revisions of "Systemd"

From Christoph's Personal Wiki
Jump to: navigation, search
(journalctl)
(External links)
Line 352: Line 352:
  
 
==External links==
 
==External links==
*[http://freedesktop.org/wiki/Software/systemd/ Official website]
+
* [http://freedesktop.org/wiki/Software/systemd/ Official website]
*[https://www.digitalocean.com/community/tutorials/how-to-use-journalctl-to-view-and-manipulate-systemd-logs How To Use Journalctl to View and Manipulate Systemd Logs]
+
* Full systemd documentation can be found by running <code>man 5 systemd.unit</code>
*[https://tlhp.cf/lennart-poettering-su/ Explanation of `machinectl`] &mdash; the `su` replacement on systemd
+
* [https://www.digitalocean.com/community/tutorials/how-to-use-journalctl-to-view-and-manipulate-systemd-logs How To Use Journalctl to View and Manipulate Systemd Logs]
 +
* [https://tlhp.cf/lennart-poettering-su/ Explanation of `machinectl`] &mdash; the `su` replacement on systemd
  
 
[[Category:Linux Command Line Tools]]
 
[[Category:Linux Command Line Tools]]

Revision as of 23:28, 10 April 2023

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 is-active docker   
active
$ systemctl [reboot|poweroff|suspend]
  • List all loaded units:
$ systemctl list-units -all | grep loaded | awk '{print $1;}'
  • List all enabled units:
$ systemctl list-unit-files | grep enabled | awk '{print $1;}'
  • List all loaded services:
$ systemctl list-units -all | grep service | grep loaded | awk '{print $1;}'
  • List all enabled services:
$ systemctl list-unit-files | grep service | grep enabled | awk '{print $1;}' > enabled.txt
  • Find a list of services that are loaded but not enabled:
$ systemctl list-units -all | grep service | grep loaded | awk '{print $1;}' > loaded.txt
$ systemctl list-unit-files | grep service | grep enabled | awk '{print $1;}' > enabled.txt
$ diff -y loaded.txt enabled.txt
# Check for missing ones:
$ diff -y loaded.txt enabled.txt | grep '<'
  • List failed services:
$ 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'
Edit or create a service
$ systemctl edit --force --full my-new.service
[Unit]
Description=Description of my service
After=network-online.target

[Service]
Type=idle
User=bob
WorkingDirectory=/path/to
ExecStart=/path/to/version1
# Otherwise
ExecStart=/path/to/version1

[Install]
WantedBy=multi-user.target

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"
  • Show BIOS boot up sequence:
$ journalctl --no-hostname -o short-monotonic --boot -0
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

You can also use the --since argument to filter entries:

--since "2017-10-14 17:00:00"
--since today

Finally, you can set the following in /etc/systemd/journald.conf:

SystemMaxUse=100M
  • Misc:
$ journalctl --utc -ke

Where:

--utc: show time in Coordinated Universal Time (UTC).
-ke: show only kernel messages and jumps to end of the log.

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 set-hostname <new-hostname>
$ vi /etc/hosts
127.0.0.1 localhost
127.0.1.1 <new-hostname>
  • Other ways to change the hostname:
$ sudo hostnamectl --transient set-hostname $hostname
$ sudo hostnamectl --static set-hostname $hostname
$ sudo hostnamectl --pretty set-hostname $hostname

Systemd timers

  • List all timers on current host:
$ systemctl status *timer
● apt-daily.timer - Daily apt download activities
   Loaded: loaded (/lib/systemd/system/apt-daily.timer; enabled; vendor preset: enabled)
   Active: active (waiting) since Sun 2021-08-08 04:57:00 PDT; 1 weeks 2 days ago
  Trigger: Wed 2021-08-18 04:19:59 PDT; 14h left
...
  • Check status of a given timer:
$ systemctl status motd-news.timer
● motd-news.timer - Message of the Day
   Loaded: loaded (/lib/systemd/system/motd-news.timer; enabled; vendor preset: enabled)
   Active: active (waiting) since Sun 2021-08-08 04:57:00 PDT; 1 weeks 2 days ago
  Trigger: Wed 2021-08-18 04:44:11 PDT; 14h left
  • Check the journal for a given timer:
$ journalctl -S today -u apt-daily-upgrade.timer
-- Logs begin at Fri 2018-07-13 11:54:18 PDT, end at Tue 2021-08-17 14:20:37 PDT. --
-- No entries --
  • Useful for setting timers:
$ systemd-analyze calendar 2030-08-17
  Original form: 2030-08-17
Normalized form: 2030-08-17 00:00:00
    Next elapse: Sat 2030-08-17 00:00:00 PDT
       (in UTC): Sat 2030-08-17 07:00:00 UTC
       From now: 8 years 11 months left

$ systemd-analyze calendar 2030-08-17 20:10:12
  Original form: 2030-08-17
Normalized form: 2030-08-17 00:00:00
    Next elapse: Sat 2030-08-17 00:00:00 PDT
       (in UTC): Sat 2030-08-17 07:00:00 UTC
       From now: 8 years 11 months left

  Original form: 20:10:12
Normalized form: *-*-* 20:10:12
    Next elapse: Tue 2021-08-17 20:10:12 PDT
       (in UTC): Wed 2021-08-18 03:10:12 UTC
       From now: 5h 46min left


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
  • List content of an initramfs image:
$ lsinitramfs /boot/initrd.img-$(uname -r) | less

External links