Difference between revisions of "Systemd"

From Christoph's Personal Wiki
Jump to: navigation, search
(Example usage)
(Example usage)
Line 25: Line 25:
 
  $ systemctl [status|stop|enable|disable|restart] ssh.service
 
  $ systemctl [status|stop|enable|disable|restart] ssh.service
 
  $ systemctl is-enabled ssh.service
 
  $ systemctl is-enabled ssh.service
 +
$ systemctl edit --full docker.service
 
  $ systemctl cat ssh.service
 
  $ systemctl cat ssh.service
 
  $ systemctl show --property=ExecStart docker.service
 
  $ systemctl show --property=ExecStart docker.service

Revision as of 23:20, 5 October 2016

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
  • 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"

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

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