Tag: systemctl

What is the difference between “systemctl reboot” and “reboot” and “systemctl poweroff” and “poweroff”

What's the difference between

sudo systemctl reboot

And

sudo reboot

Is it true that the use of commands depends on the operating system, and that one will execute a shorthand version, the other will use systemctl?

Answer:

The halt, poweroff, reboot commands are implemented to maintain basic compatibility with the original SysV commands. Verbs

  • systemctl halt
  • systemctl poweroff
  • systemctl reboot

provide the same functionality with some additional features.

That is, reboot is now also systemctl. You can verify this:

which reboot
/usr/sbin/reboot

file /usr/sbin/reboot
/usr/sbin/reboot: symbolic link to /bin/systemctl

That is, the reboot command is actually a symbolic link to systemctl.

In turn, the command

systemctl reboot

is an abbreviation for

systemctl start reboot.target --job-mode=replace-irreversibly --no-block

That is

reboot

this is exactly the same as

systemctl reboot

as well as

systemctl start reboot.target --job-mode=replace-irreversibly --no-block

This is true for distributions that have switched to systemd (for example, Arch Linux, the entire Debian family, including Ubuntu). That is, for most modern distributions, except for those on which SysV remained.

In some cases, the reboot command does not work – see Error “Failed to talk to init daemon” for details. In this case, to restart the computer, you must add the -f option:

reboot -f

The shutdown command is:

poweroff -f

Even if these commands did not help, then use the options with the double option -f.

To turn off your computer do:

poweroff -f -f

Or restart your computer with the command:

reboot -f -f

The -f option means forced immediate stop, shutdown, or reboot. When specified once, this results in an immediate but clean shutdown by the system manager. If specified twice, it results in an immediate shutdown without contacting the system manager.

When using the -f option with systemctl halt, systemctl poweroff, systemctl reboot, or systemctl kexec, the selected operation is performed without shutting down all units. However, all processes will be forcibly terminated, and all file systems will be unmounted or remounted read-only. Therefore, it is a radical, but relatively safe option to request an immediate restart. If you specify --force twice for these operations (except for kexec), they will be executed immediately, without killing any processes or unmounting any filesystems. Warning: specifying --force twice for any of these operations can result in data loss. Note that if you specify --force twice, the selected operation is performed by systemctl itself and is not associated with the system manager. This means that the command must be executed even if the system manager fails.

Do services need to be restarted when updating packages

Package configuration: whether to restart the service

During the installation of package updates and their configuration, the apt program may ask you to restart the service:

There are services installed on your system which need to be restarted when certain libraries, such as libpam, libc, and libssl, are upgraded. Since these restarts may cause interruptions of service for the system, you will normally be prompted on each upgrade for the list of services you wish to restart. You can choose this option to avoid being prompted; instead, all necessary restarts will be done for you automatically so you can avoid being asked questions on each library upgrade. Restart services during package upgrades without asking?

This message can be confusing, especially the phrase “cause interruptions of service for the system”. In fact, the essence is quite simple – the binaries have been updated and you need to restart the services that use them so that they start using the updated versions of the files.

The name of the package that requires the service to be restarted is in the upper left corner, in the screenshot it is libc6, i.e. “GNU C Library: Shared libraries”. It contains the standard libraries that are used by nearly all programs on the system. This package includes shared versions of the standard C library and the standard math library, as well as many others.

What kind of interruptions can a service restart cause?

Examples of the consequences of restarting services:

  • at the time of restarting the web server service, sites will be unavailable to users
  • when restarting the caching proxy server, the cache stored in RAM will be deleted
  • restarting network services can lead to connection drops (but in practice this does not always happen)

That is, the possible consequences of restarting services on the home computer are insignificant – you can safely restart.

As far as restarting services on a server, for example, restarting the SSH server usually doesn't break the connection. You need to evaluate the consequences of restarting other services based on your situation.

See also:

Error “Failed to talk to init daemon” (SOLVED)

You can use command line to shutdown Linux computer, following command will shutdown computer:

shutdown -h now
systemctl halt

To reboot, you can use the following command:

systemctl restart

They usually work fine, but on some distributions they require elevated privileges, which means they need to be run with sudo.

But in single user mode, these commands result in the following error:

System has not been booted with systemd as init system (PID 1). Can’t operate.
Failed to connect to bus: Host is down
Failed to talk to init daemon.

Linux single user mode is used, for example, to reset a forgotten password for the root user or any other user. The operation of the computer in this mode is different from normal and, as you can see from the error message, the system was not booted with systemd as the init of the system, so it cannot connect to the bus and cannot send commands to the init daemon.

Related: How to reset a forgotten login password in Linux

However, there is still a way to turn off the computer.

To log out safely, type:

sync
umount /

These commands instruct the OS to write the changes made to the file system (for optimization purposes, they can be stored in the cache), and then unmount the root file system.

After that, to turn off the computer, run:

poweroff -f

Or restart your computer with the command:

reboot -f

Even if these commands did not help, then use the double -f options.

To turn off your computer run:

poweroff -f -f

Or restart your computer with the command:

reboot -f -f

The -f option means forced immediate stop, shutdown, or reboot. When specified once, this results in an immediate but clean shutdown by the system manager. If specified twice, it results in an immediate shutdown without contacting the system manager.

When using the -f option with systemctl halt, systemctl poweroff, systemctl reboot, or systemctl kexec, the selected operation is performed without shutting down all units. However, all processes will be forcibly terminated, and all file systems will be unmounted or remounted read-only. Therefore, it is a radical, but relatively safe option to request an immediate restart. If you specify --force twice for these operations (except for kexec), they will be executed immediately, without killing any processes or unmounting any filesystems. Warning: specifying --force twice for any of these operations can result in data loss. Note that if you specify --force twice, the selected operation is performed by systemctl itself and is not associated with the system manager. This means that the command must be executed even if the system manager fails.

How to Run a Program Automatically on Startup in Linux

If you want a program or script to run when the system starts up, then this can be done using systemctl. This method is universal: autorun also works on headless servers, not just when you enter a graphical desktop environment. This method is universal and will work on all systems where systemctl is present (e.g. Debian, Linux Mint, Ubuntu, Kali Linux, Arch Linux, etc.).

To add a script to autoload in Linux, you need to create a specific file in the /etc/systemd/system/ directory. Choose your own file name. For example, I want to run a script on startup that contains several rules to hinder DOS attacks. This file is located at /root/firewall.sh

We start by assigning the correct permissions to the file:

chmod 755 /root/firewall.sh

Make sure the file has shebang #!/bin/bash (or whatever matches the contents of the file).

I create a file anti-dos.service (you can choose your own name):

vim /etc/systemd/system/anti-dos.service

Content of my file

[Unit]
Description=Initial anti-DOS protection.

[Service]
ExecStart=/root/firewall.sh

[Install]
WantedBy=multi-user.target

Here

  • Description=Initial anti-DOS protection. - this is a description, replace the description with your own.
  • ExecStart=/root/firewall.sh - full path to the file that I want to add to startup is specified.

Don't write something like “ExecStart=/bin/sh /path/to/script.sh” as that won't work. If you really want to specify an interpreter and a script, then use a construction like this:

/usr/bin/bash -c 'INTERPRETER /PATH/TO/SCRIPT'

For instance:

/usr/bin/bash -c 'php /root/bin/translator.php'

Example file using this construction

[Unit]
Description=Launch translator.
 
[Service]
ExecStart=/usr/bin/bash -c 'php /root/bin/translator.php'
 
[Install]
WantedBy=multi-user.target
  • WantedBy=multi-user.target - means that autorun is made for all users.

To start the services in the current session (change the name anti-dos.service to the name of your file):

systemctl start anti-dos.service

To check the status of a service:

systemctl status anti-dos.service

To enable autostart, you need to do this (change the name anti-dos.service to the name of your file):

systemctl enable anti-dos.service

Creating a link says that adding to autostart worked:

Let's restart the server to check))

The screenshot above shows that everything worked properly. The fact that the process is shown as deceased, in my case, is normal. Since I added not a service to autostart, but a one-time script.

To make sure that the firewall settings are in effect, I type:

iptables --list

Yes, my firewall rules are there.

You can also find your autorun file in the list of everything added to startup:

systemctl list-unit-files

After editing files with the extension .service, for the changes to take effect, you need to run the command:

systemctl daemon-reload

How to change the default operating system in Arch Linux (for UEFI and systemd-boot)

This instruction applies to all boot loaders that implement the Boot Loader Specification and/or Boot Loader Interface, such as systemd-boot.

If you have a modern computer (no older than seven years at the time of writing), then it most likely uses UEFI instead of BIOS and, therefore, uses systemd-boot to boot, which is usually mentioned in the Arch Linux installation instructions (although there may be grub).

The bootctl program can check EFI firmware and bootloader status, list and manage available bootloaders and bootloader entries, and install, update, or remove systemd-boot on the current system.

To check if your system uses systemd-boot, run the command:

bootctl is-installed

The following command will show all available loader entries that implement the Boot Loader Specification, as well as any other entries found or automatically generated by a loader that implements the Boot Loader Interface.

bootctl list

  • title is an entry name. Note that the entry with the string “(default)” is selected as the default loaded
  • id is an identifier, it is needed in the commands described below to change the OS loaded by default
  • source is a path in the OS
  • linux is the Linux kernel
  • initrd is initial RAM filesystem. The purpose of initramfs is to boot the system to the point where it can access the root filesystem.
  • options are boot options

The following command sets the default bootloader entry. It takes one string of the bootloader entry ID as an argument.

sudo bootctl set-default ID

The set-oneshot command will set the default entry for the next boot only, set-default will set it permanently for all future bootings.

sudo bootctl set-oneshot ID

Optionally, the bootloader entry ID can be one of: @default, @oneshot, or @current, which corresponds to the current default bootloader entry for all future downloads, the current default bootloader entry for the next boot, and the currently loaded bootloader entry. These special identifiers are converted to the current values of the EFI variables LoaderEntryDefault, LoaderEntryOneShot, and LoaderEntrySelected. These special identifiers are primarily useful as a quick way to permanently make the currently loaded bootloader entry the default choice, or update the default bootloader entry for the next boot to the default bootloader entry for all future bootings, but they can be used for others operations as well. If an empty string ("") is specified as the identifier, the corresponding EFI variable will not be set.

The boot manager integrates with the systemctl command, so the following commands can be used:

systemctl reboot --boot-loader-entry=ID

This command will reboot the computer to the specified bootloader entry.

By the way, to get a very compact list containing only bootloader entries, run the command:

systemctl reboot --boot-loader-entry=help

The following command will immediately reload you into the EFI boot menu. This menu will have a timeout for the specified number of SECONDS:

systemctl reboot --boot-loader-menu=SECONDS
Loading...
X