Using
json.tool
from the shell to validate and pretty-print:json.tool
from the shell to validate and pretty-print:$ echo '{"json":"obj"}' | python -m json.tool { "json": "obj" } $ echo '{1.2:3.4}' | python -mjson.tool Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Systemd
is an init system and system manager that is widely becoming the new standard for Linux machines. While there are considerable opinions about whether systemd
is an improvement over the traditional SysV
init systems it is replacing, the majority of distributions plan to adopt it or have already done so.systemd
is well worth the trouble, as it will make administrating these servers considerably easier. Learning about and utilizing the tools and daemons that comprise systemd
will help you better appreciate the power, flexibility, and capabilities it provides, or at least help you to do your job with minimal hassle.systemctl
command, which is the central management tool for controlling the init system. We will cover how to manage services, check statuses, change system states, and work with the configuration files.systemd
, the target of most actions are "units", which are resources that systemd
knows how to manage. Units are categorized by the type of resource they represent and they are defined with files known as unit files. The type of each unit can be inferred from the suffix on the end of the file..service
. However, for most service management commands, you can actually leave off the .service
suffix, as systemd
is smart enough to know that you probably want to operate on a service when using service management commands.systemd
service, executing instructions in the service's unit file, use the start
command. If you are running as a non-root user, you will have to use sudo
since this will affect the state of the operating system:sudo systemctl start application.service
systemd
knows to look for *.service
files for service management commands, so the command could just as easily be typed like this:sudo systemctl start application
.service
suffix for the remainder of the commands to be explicit about the target we are operating on.stop
command instead:sudo systemctl stop application.service
restart
command:sudo systemctl restart application.service
reload
command to initiate that process:sudo systemctl reload application.service
reload-or-restart
command. This will reload the configuration in-place if available. Otherwise, it will restart the service so the new configuration is picked up:sudo systemctl reload-or-restart application.service
systemd
to start services automatically at boot, you must enable them.enable
command:sudo systemctl enable application.service
/lib/systemd/system
or /etc/systemd/system
) into the location on disk where systemd
looks for autostart files (usually /etc/systemd/system/some_target.target.wants
. We will go over what a target is later in this guide).sudo systemctl disable application.service
start
and enable
commands.status
command:systemctl status application.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2015-01-27 19:41:23 EST; 22h ago
Main PID: 495 (nginx)
CGroup: /system.slice/nginx.service
├─495 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr;
└─496 nginx: worker process
Jan 27 19:41:23 desktop systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 27 19:41:23 desktop systemd[1]: Started A high performance web server and a reverse proxy server.
is-active
command:systemctl is-active application.service
active
or inactive
. The exit code will be "0" if it is active, making the result simpler to parse programatically.is-enabled
command:systemctl is-enabled application.service
enabled
or disabled
and will again set the exit code to "0" or "1" depending on the answer to the command question.systemctl is-failed application.service
active
if it is running properly or failed
if an error occurred. If the unit was intentionally stopped, it may return unknown
or inactive
. An exit status of "0" indicates that a failure occurred and an exit status of "1" indicates any other status.systemctl
commands that provide this information.systemd
knows about, we can use the list-units
command:systemctl list-units
systemd
currently has active on the system. The output will look something like this:UNIT LOAD ACTIVE SUB DESCRIPTION
atd.service loaded active running ATD daemon
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
dbus.service loaded active running D-Bus System Message Bus
dcron.service loaded active running Periodic Command Scheduler
dkms.service loaded active exited Dynamic Kernel Modules System
getty@tty1.service loaded active running Getty on tty1
. . .
systemd
unit namesystemd
. The configuration of loaded units is kept in memory.list-units
command shows only active units by default, all of the entries above will show "loaded" in the LOAD column and "active" in the ACTIVE column. This display is actually the default behavior of systemctl
when called without additional commands, so you will see the same thing if you call systemctl
with no arguments:systemctl
systemctl
to output different information by adding additional flags. For instance, to see all of the units that systemd
has loaded (or attempted to load), regardless of whether they are currently active, you can use the --all
flag, like this:systemctl list-units --all
systemd
loaded or attempted to load, regardless of its current state on the system. Some units become inactive after running, and some units that systemd
attempted to load may have not been found on disk.--state=
flag to indicate the LOAD, ACTIVE, or SUB states that we wish to see. You will have to keep the --all
flag so that systemctl
allows non-active units to be displayed:systemctl list-units --all --state=inactive
--type=
filter. We can tell systemctl
to only display units of the type we are interested in. For example, to see only active service units, we can use:systemctl list-units --type=service
list-units
command only displays units that systemd
has attempted to parse and load into memory. Since systemd
will only read units that it thinks it needs, this will not necessarily include all of the available units on the system. To see every available unit file within the systemd
paths, including those that systemd
has not attempted to load, you can use the list-unit-files
command instead:systemctl list-unit-files
systemd
knows about. Since systemd
has not necessarily read all of the unit definitions in this view, it only presents information about the files themselves. The output has two columns: the unit file and the state.UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount static
var-lib-nfs-rpc_pipefs.mount static
org.cups.cupsd.path enabled
. . .
systemd
knows about. However, we can find out more specific information about units using some additional commands.systemd
has loaded into its system, you can use the cat
command (this was added in systemd
version 209). For instance, to see the unit file of the atd
scheduling daemon, we could type:systemctl cat atd.service
[Unit]
Description=ATD daemon
[Service]
Type=forking
ExecStart=/usr/bin/atd
[Install]
WantedBy=multi-user.target
systemd
process. This can be important if you have modified unit files recently or if you are overriding certain options in a unit file fragment (we will cover this later).list-dependencies
command:systemctl list-dependencies sshd.service
sshd.service
├─system.slice
└─basic.target
├─microcode.service
├─rhel-autorelabel-mark.service
├─rhel-autorelabel.service
├─rhel-configure.service
├─rhel-dmesg.service
├─rhel-loadmodules.service
├─paths.target
├─slices.target
. . .
.target
units, which indicate system states. To recursively list all dependencies, include the --all
flag. --reverse
flag to the command. Other flags that are useful are the --before
and --after
flags, which can be used to show units that depend on the specified unit starting before and after themselves, respectively.show
command. This will display a list of properties that are set for the specified unit using a key=value
format:systemctl show sshd.service
Id=sshd.service
Names=sshd.service
Requires=basic.target
Wants=system.slice
WantedBy=multi-user.target
Conflicts=shutdown.target
Before=shutdown.target multi-user.target
After=syslog.target network.target auditd.service systemd-journald.socket basic.target system.slice
Description=OpenSSH server daemon
. . .
-p
flag with the property name. For instance, to see the conflicts that the sshd.service
unit has, you can type:systemctl show sshd.service -p Conflicts
Conflicts=shutdown.target
systemd
also has the ability to mark a unit as completely unstartable, automatically or manually, by linking it to /dev/null
. This is called masking the unit, and is possible with the mask
command:sudo systemctl mask nginx.service
list-unit-files
, you will see the service is now listed as masked:systemctl list-unit-files
. . .
kmod-static-nodes.service static
ldconfig.service static
mandb.service static
messagebus.service static
nginx.service masked
quotaon.service static
rc-local.service static
rdisc.service disabled
rescue.service static
. . .
sudo systemctl start nginx.service
Failed to start nginx.service: Unit nginx.service is masked.
unmask
command:sudo systemctl unmask nginx.service
systemctl
provides builtin mechanisms for editing and modifying unit files if you need to make adjustments. This functionality was added in systemd
version 218.edit
command, by default, will open a unit file snippet for the unit in question:sudo systemctl edit nginx.service
/etc/systemd/system
directory which contains the name of the unit with .d
appended. For instance, for the nginx.service
, a directory called nginx.service.d
will be created.override.conf
. When the unit is loaded, systemd
will, in memory, merge the override snippet with the full unit file. The snippet's directives will take precedence over those found in the original unit file. --full
flag:sudo systemctl edit --full nginx.service
/etc/systemd/system
, which will take precedence over the system's unit definition (usually found somewhere in /lib/systemd/system
)..d
configuration directory or the modified service file from /etc/systemd/system
. For instance, to remove a snippet, we could type:sudo rm -r /etc/systemd/system/nginx.service.d
sudo rm /etc/systemd/system/nginx.service
systemd
process so that it no longer attempts to reference these files and reverts back to using the system copies. You can do this by typing:sudo systemctl daemon-reload
.target
. Targets do not do much themselves, but are instead used to group other units together.swap.target
that is used to indicate that swap is ready for use. Units that are part of this process can sync with this target by indicating in their configuration that they are WantedBy=
or RequiredBy=
the swap.target
. Units that require swap to be available can specify this condition using the Wants=
, Requires=
, and After=
specifications to indicate the nature of their relationship.systemd
process has a default target that it uses when booting the system. Satisfying the cascade of dependencies from that single target will bring the system into the desired state. To find the default target for your system, type:systemctl get-default
multi-user.target
set-default
. For instance, if you have a graphical desktop installed and you wish for the system to boot into that by default, you can change your default target accordingly:sudo systemctl set-default graphical.target
systemctl list-unit-files --type=target
systemd
has attempted to start all of the units tied to the target and has not tried to tear them down again. To see all of the active targets, type:systemctl list-units --type=target
isolate
. This is similar to changing the runlevel in other init systems.graphical.target
active, you can shut down the graphical system and put the system into a multi-user command line state by isolating the multi-user.target
. Since graphical.target
depends on multi-user.target
but not the other way around, all of the graphical units will be stopped.systemctl list-dependencies multi-user.target
sudo systemctl isolate multi-user.target
systemctl
also has some shortcuts that add a bit of additional functionality.rescue
command instead of isolate rescue.target
:sudo systemctl rescue
halt
command:sudo systemctl halt
poweroff
command:sudo systemctl poweroff
reboot
command:sudo systemctl reboot
systemd
.sudo reboot
systemctl
command that allow you to interact with and control your systemd
instance. The systemctl
utility will be your main point of interaction for service and system state management.systemctl
operates mainly with the core systemd
process, there are other components to the systemd
ecosystem that are controlled by other utilities. Other capabilities, like log management and user sessions are handled by separate daemons and management utilities (journald
/journalctl
and logind
/loginctl
respectively). Taking time to become familiar with these other tools and daemons will make management an easier task.