Chapter 1. Starting and Stopping Linux

There multiple ways to shutdown or reboot a Linux system. The shutdown/reboot/lock screen/logout buttons in Linux graphical desktops are sufficient for most users, and do not require root permissions.

There are also a number of commands that have accumulated over the decades, and these require root permissions: shutdown, halt, poweroff, reboot, and the “three-key salute” Ctrl+Alt+Del. The shutdown command provides the useful options of timed shutdowns, with warnings to all logged-in users.

But that’s not all, because systemd introduced a new batch of shutdown and reboot commands. On Linux distributions with systemd, the classic old commands are controlled by the systemctl command. You can see this with the stat command, like this example for shutdown:

$ stat /sbin/shutdown
  File: /sbin/shutdown -> /bin/systemctl
  Size: 14              Blocks: 0          IO Block: 4096   symbolic link
Device: 802h/2050d      Inode: 1177556     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)

The File: line displays a symlink to /bin/systemctl. On Linux systems without systemd there is not a symlink. The commands behave the same way whether or not your system uses systemd.

Linux supports automated startups and shutdowns. An automated shutdown is nice to remind you to stop working, and you don’t have to remember to shut off your computer for the night. Automated wakeups are a convenience, and practical. For example, if you work in an office and at home, you can set up automated wakeups and shutdowns so that you can log in from either location during work hours without having to leave them running all the time. There are multiple tools for automated wakeups, with varying degrees of reliability. In this chapter you will learn about all of them.

1.1 Shutting Down, Timed Shutdowns, and Rebooting with the shutdown Command


You want to schedule timed shutdowns, for example ten minutes from now, or at a specific time, and warn all logged-in users. Or you want to just shutdown now with no frills. Or you want to learn all the shutdown command options such as reboot and sending custom messages to warn users that the system is going down, and not fool around with all the other Linux shutdown commands like halt, poweroff, and reboot.


The following examples show how to shutdown immediately, shutdown a certain number of minutes from now, cancel a shutdown, create a custom shutdown message, shutdown at a specific time, halt, and reboot.

Shut down immediately, with no notification to other logged-in users:

$ sudo shutdown -h now

Shut down in ten minutes with notifications:

$ sudo shutdown -h +10
Shutdown scheduled for Sun 2020-05-24 11:04:43 PDT, use 'shutdown -c' to cancel.

Other users on the system will see this message:

Broadcast message from root@dreamer on pts/4 (Sun 2020-05-24 10:54:43 PDT):

The system is going down for poweroff at Sun 2020-05-24 11:04:43 PDT!

You can see this message by having a second terminal open. The shutdown message does not return your terminal to the command prompt, so press the Return key to restore your command prompt.

Cancel the shutdown:

$ sudo shutdown -c

Logged-in users will see this message:

Broadcast message from root@dreamer on pts/4 (Sun 2020-05-24 10:56:00 PDT):

The system shutdown has been cancelled

Create your own message:

$ shutdown -h +6 "Time to stop working and go outside to play!"

Rather than specifying minutes to shutdown, you may set a shutdown time in 24-hour hh:mm format. The following example shuts down the system at 10:15 PM:

$ shutdown -h 22:15

Running shutdown with no options is equivalent to shutdown -h +1.


shutdown sends messages only when you use the -h option, except for the -h now option, and when you use the -k option. These are called wall messages, which is short for “write to all logged-in users”. Only root can send wall messages.

shutdown has a few other options:

  • --help displays a summary of options.

  • -h, --halt performs a clean shutdown but does not power off the machine, you must press and hold the power button to power off your machine.

  • -P, --poweroff performs a clean shutdown and powers off the machine.

  • -r, --reboot performs a clean shutdown and reboots the machines.

  • -k sends a wall message without shutting down the machine.

  • --no-wall disables wall messages.

See Also

  • shutdown (8)

  • wall (1)

  • systemd-halt.service (8)

1.2 Shutting Down and Rebooting with halt and poweroff


You understand the shutdown command, and now you want to know what halt, reboot, and poweroff are for, and how to use them.


halt performs a clean shutdown, stopping all services, processes, and unmounting filesystems, but it does not power off the machine. After the halt command is finished you must press and hold the power button to complete the shutdown.

reboot performs a clean shutdown and restarts the system.

poweroff performs a clean shutdown and powers off the machine. All require root permissions:

$ sudo halt
$ sudo reboot
$ sudo poweroff

The halt and poweroff commands can reboot the system after a clean shutdown:

$ sudo halt --reboot
$ sudo poweroff --reboot


If you’re thinking this all looks a little weird, you’re right. Sometimes cruft accumulates, and never goes away.

During a system shutdown from a graphical environment, press the Esc key to see all the shutdown messages.

halt and poweroff are the same commands, and support the same options:

  • --help displays a summary of options.

  • --halt performs a clean shutdown but does not power off the machine (yes, halt and halt --halt do the same thing).

  • -p, --poweroff performs a clean shutdown and powers off the machine (yes, poweroff and poweroff --poweroff do the same thing) .

  • --reboot performs a clean shutdown and restarts the machine.

  • -f, --force forces an immediate halt or poweroff. Shutdown of all running services is skipped, all processes are killed, and all file systems are unmounted or mounted read-only. Run it twice to force an unclean shutdown, for example sudo poweroff -f -f, which (obviously) you should use only when normal shutdown commands fail.

  • -w, --wtmp-only does not perform a shutdown, but only writes an entry in /var/log/wtmp.

  • -d, --no-wtmp prevents writing a wtmp entry.

  • -n, --no-sync disables syncing storage media before shutting down. This prevents the completion of writing data in memory to disk, so it may be useful for creating scenarios for practicing data recovery.

See Also

  • halt (8)

  • poweroff (8)

  • systemd-halt.service (8)

1.3 Sending your System into Sleep Modes with systemctl


Your Linux system has systemd, and you want to know how to use systemctl to manage system sleep modes.


systemctl provides these power saving modes: suspend, hibernate, hybrid-sleep, and suspend-then-hibernate. Put your system into suspend mode:

$ sudo systemctl suspend

This stores your current session in RAM, and puts all hardware into a suspended state. Wake up your system by pressing a key on your keyboard.

Put your system into hibernate mode:

$ sudo systemctl hibernate

This stores your session on disk, and powers off the machine. Wake it up by pressing the power button, and when it resumes, which can take a minute or two, your session will be restored where you left off. You should not have to log in or re-open your programs.

Put your system into hybrid-sleep mode:

$ sudo systemctl hybrid-sleep

This suspends your system to both memory and disk, and shuts off all devices except RAM. If your system RAM loses power then the system resumes from disk. Wake it up by pressing the power button.

Put your system into suspend-then-hibernate mode:

$ sudo systemctl suspend-then-hibernate

suspend-then-hibernate first goes into suspend mode, then enters hibernation after the period of time specified by the HibernateDelaySec= setting in /etc/systemd/sleep.conf. Wake it up with a key press. >>>>>>> systemd-corrections


See the Discussion in Recipe 1.8 for details on the different sleep states.

Your graphical desktop should have buttons for entering power saving modes, and a graphical configuration tool for controlling events such as screen blanking, screen locking, and power button actions such as entering a sleep state or powering off.

Some of these commands may not behave reliably on your Linux distribution. Power management is affected by your UEFI, CPU capabilities, udev, ACPI, kernel compilation options, and possibly other devices and programs, so much depends on how your particular Linux has implemented power management. Check your distribution documentation.

See Also

  • systemctl (1)

  • systemd-halt.service (8)

1.4 Rebooting with Ctrl+Alt+Del


You want a reliable method of rebooting that always works, even when you are having problems such as crashes and runaway processes.


The good old “three finger salute”, Ctrl+Alt+Del, was made for this. Any user can run Ctrl+Alt+Del. Press and hold these three keys in sequence, and they will override most problems and cleanly reboot your system.

However, there is a catch: Ctrl+Alt+Del behaves differently on the various Linux distributions. Some disable it by default. Some disable it only in their graphical environments, and leave it enabled in the console. Depending on which Linux you are using, you may need to find your keyboard shortcut settings and configure Ctrl+Alt+Del to reboot. Figure 1-1 shows what this looks like in Xubuntu.

Configuring Ctrl+Alt+Del in Xubuntu
Figure 1-1. Settings > Keyboard > Applications, configuring keyboard shortcuts in Xubuntu.


On systems with systemd, Ctrl+Alt+Del is linked to the, which you can see with stat:

$ stat /usr/lib/systemd/system/
  File: /usr/lib/systemd/system/ ->
  Size: 13              Blocks: 0          IO Block: 4096   symbolic link
Device: 802h/2050d      Inode: 136890      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)

Non-systemd Linux systems manage Ctrl+Alt+Del in a number of ways, and you must rely on their documentation to learn how.

See Also

  • systemd.special (7)

1.5 Disabling and Re-enabling Ctrl+Alt+Delete in systemd


You want to disable Ctrl+Alt+Del, or re-enable it.


Disable and mask the to completely disable it:

$ sudo systemctl disable
Removed /etc/systemd/system/
$ sudo systemctl mask
Created symlink /etc/systemd/system/ → /dev/null.

Depending on which Linux you are using, this may disable it only in the console, and you will also have to remove it from your keyboard shortcut settings in your graphical desktop.

Re-enable and un-mask Ctrl+Alt+Del:

$ sudo systemctl unmask
Removed /etc/systemd/system/
$ sudo systemctl enable
Created symlink /etc/systemd/system/ →


In the non-systemd Linuxes there are a lot of variations in how they manage Ctrl+Alt+Del, so you will have to find out how to disable and enable Ctrl+Alt+Del on your particular Linux. Linux systems with systemd (which is nearly all of the major distributions) should all control it with systemd.

Ctrl+Alt+Del is so useful it does not make sense to me to disable it. I’ve heard reasons like preventing users from rebooting accidentally, or it is a security measure. The first reason is implausible, though I suppose it happens. For the second reason, remember the old Unix dictum “She who has physical access to the machine owns it.” But of course you must do what works for you.

See Also

  • systemd.special (7)

1.6 Creating Scheduled Shutdowns


You want your machine turn itself off at night, so you can walk away and not worry about it. Or, your users are careless watt-wasters who refuse to develop the habit of shutting their PCs down at night.


Use cron to create scheduled shutdowns. For example, add this line to /etc/crontab to shutdown every night at 10:30 pm, with a 20-minute warning:

#  m   h   dom mon dow   user    command
  10  22    *   *   *    root    /sbin/shutdown -h +20

This example runs only on 11 pm weekdays, and shuts down immediately with no warning:

#  m   h   dom mon dow   user    command
  00  23    *   *  1-5   root    /sbin/shutdown -h now

Another way is to use the crontab command:

# crontab -e

This opens the root user’s crontab in edit mode. Edit and save, and you’re done. Don’t try to name the file yourself. During editing it is a temporary file, which is automatically renamed by crontab when you save it. It will be saved in /var/spool/cron/crontabs.


/etc/crontab has a name field, so any user can have entries in this file, but only root can edit /etc/crontab. Users who want to control their own personal crontabs should use the crontab command.

The fields in /etc/crontab can take a little getting used to, so here are more examples and explanations.

Table 1-1. Table of crontab values
Field Allowed values





day of month




day of week


The asterisk * is a wildcard for “all.”

Shut down only on weekends:

# shutdown at 1:05 am Saturdays and Sundays
00 01   * * 7,0   root /sbin/shutdown -h +5

Sometimes Sunday is 7, you’ll have to test it to see what works, so you may need to use 6,7 for Saturday, Sunday:

00 01   * * 6,7   root /sbin/shutdown -h +5

It would be nice to use sat,sun, but you can only enter one day by name, and cannot use names in lists. Day-of-the-week and months are named with the first three letters: sat, sun, jan, feb. Case does not matter.

You can use ranges: 1-4 means 1,2,3, and 4.

Ranges and lists can be mixed: 1,3,5,6-10

Step values follow ranges:

10-23/2 is every second hour in the range

*/2 in the dow field means every other day

2-6/2 equals 2,4,6

The following strings are nice shortcuts that replace the first five fields:


See Also

  • cron (8)

  • crontab (1)

  • crontab (5)

1.7 Scheduling Automated Startups with UEFI Wakeups


Scheduled shutdowns are so wonderful you want scheduled startups as well.


You’re in luck, because Linux supports scheduled wakeups. There are three methods to try: Wake-on-LAN, RTC wakeups, or your computer’s UEFI setup, if it has a scheduled wakeup feature.

UEFI wakeups are the most reliable. Figure 1-2 shows the scheduled wakeup screen on a Dell system.

Dell UEFI wakeup scheduler
Figure 1-2. Scheduling wakeups in a Dell UEFI.

Enter your UEFI setup by pressing the appropriate Fn key at startup. On Dell, ASUS, and Acer systems this is usually F2, and Lenovo uses F1. Though this may vary, for example some systems use the Delete key, so check your machine’s documentation. Some systems tell which key to press on their startup screens. Sometimes it is a bit tricky to press the key at the right time, so start pressing it right after you press the power button, just like banging on an elevator button to make it arrive faster.

If your system does not have a UEFI wakeup feature try either Wake-on-LAN (see Recipe 1.9, which requires a second device to send a wakeup signal), or RTC wakeups (see Recipe 1.8).


Let us discuss briefly what BIOS and UEFI mean. When you boot up your computer, the first startup instructions come from the Basic Input Output System (BIOS), or the Unified Extensible Firmware Interface (UEFI) firmware stored on the computer’s motherboard. BIOS is the old legacy system that has been with us since 1980. UEFI is its modern replacement. UEFI includes legacy BIOS support, though someday this will be removed. Nearly all computers made after the mid-2000s have UEFI.

UEFI has considerably more features than the old BIOS, and is like a little operating system. The UEFI setup screens control boot order, boot devices, security options, the infamous Secure Boot, and many more functions.

1.8 Scheduling Automated Startups with RTC Wakeups


You want to set up scheduled wakeups with RTC because your UEFI setup does not have a scheduled wakeup feature, or because you just want to.


Use the rtcwake command, which should already be present on your system from the util-linux package. rtcwake stops and wakes your system. You may set it to wake up your system after a specified interval, such as 1800 seconds from now, or at a scheduled time and date.

Your system’s hardware, or real-time clock (RTC) should be set to Coordinated Universal Time (UTC). (See TODO)

When rtcwake stops your system it sends it into an ACPI sleep state. Look in /sys/power/state to see which sleep states your system supports. In the following example only three of the six ACPI sleep states are supported:

$ cat /sys/power/state
freeze mem disk

On non-systemd Linuxes query /proc/acpi/info.

In the above example we have three sleep states to try. Test each one according to the following example:

$ sudo rtcwake -m freeze -s 60

-m specifies the standby mode, and -s is the number of seconds until the system starts up again. When it is successful you will see your system go into a sleep state, and then wake up, and you will see either a success message or an error message.

This example puts the system into ACPI sleep state disk, and schedules a wakeup tomorrow at 8am. rtcwake uses UNIX epoch time, and the date command converts this to a more human-readable form:

$ sudo rtcwake -m disk no -u -t $(date +%s -d "tomorrow 08:00")

The following is a nice simple /etc/crontab example. The rtcwake command suspends to disk at 11pm on weeknights and wakes up 8 hours later. Using cron to control the time and date is easier than figuring out the way rtcwake manages time and date:

#  m   h   dom mon dow   user    command
  00  23    *   *  1-5   root    /usr/sbin/rtcwake -m disk -s 28800


Real-time Clock (RTC) wakeups are the least reliable. Your system has to be put into an Advanced Configuration and Power Interface (ACPI) sleep state supported by your chosen Linux. ACPI is the modern power management standard for managing sleep states. It is supposed to be vendor-neutral and hardware-independent, but the standard is complex and hardware vendors often support only a subset of its capabilities. Adding to the fun, the various Linuxes implement it in different ways.

There are six ACPI sleep states, S0-S5. The Linux kernel is capable of supporting up to four, and distributions vary in which ones and how many:

  • S0: System is running, monitor may be off, most peripherals on.

  • S1: Power-on suspend, CPU stops, power to CPU and RAM is on.

  • S2: CPU powered off, dirty cache flushed to RAM.

  • S3: Also called standby, sleep, and suspend-to-RAM. Data may not be written to disk.

  • S4: Hibernation, suspend-to-disk. Everything in RAM is written to disk and the system is powered down.

  • S5: Similar to powering the system off, except there is still power to the power button and peripherals such as the keyboard, network interface, and USB devices.

Unix epoch time is the number of seconds elapsed since midnght January 1, 1970 UTC. date +"%s” reports the curent Unix epoch time. The examples in this recipe use date to configure dates and times, rather than working directly with Unix epoch values. The -t option supports working in Unix time, without using date to convert.

See Also

  • man cron (8)

  • man rtcwake (8)

1.9 Setting up Remote Wakeups with Wake-on-LAN over Wired Ethernet


You want to set up remotely-triggered wakeups with Wake-on-LAN because your UEFI setup does not have a scheduled wakeup feature, or it is too simple for your needs, or you want the flexibility to send a wakeup signal at random times. This recipe is for waking up a machine on the same network as the device you use to send the wakeup signal, and your target machine must use a wired Ethernet interface.


Configure your PC to listen for wakeup requests, then use a second device, such as another computer, a smartphone, or a Raspberry Pi to send the wakeup signal, which is called the magic packet. Which isn’t really magic, but merely a specialized packet designed specifically for remote wakeups. (Yes, I too am sad it is not real magic.)

Start by booting into your system’s UEFI setup and look for settings for enabling Wake-on-LAN. An important security precaution is to also disable all settings that enable PXE booting. If PXE boot is enabled, and you have a PXE server on your network, it is possible that your machine will wake up to PXE boot and install a new image, overwriting the existing installation.

Then exit and finish starting up. Install the wakeonlan and ethtool packages.

Fetch the name of your Ethernet interface, and use ethtool to verify that it supports Wake-on-LAN:

$ ifconfig
enp0s25: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet  netmask  broadcast
        inet6 fe80::8669:ec94:861d:c9bb  prefixlen 64  scopeid 0x20<link>
        ether d1:50:f2:82:e7:fb txqueuelen 1000  (Ethernet)

$ sudo ethtool enp0s25 | grep -i wake-on
[sudo] password for carla:
        Supports Wake-on: pumbg
        Wake-on: g

Record the MAC address of your interface. In the above ifconfig example the “ether” line lists the MAC address, 9c:ef:d5:fe:01:7c. Yours will be different as MAC addresses are unique.

“Supports Wake-on: pumbg” is the magic phrase that verifies your interface has the necessary support, indicated by the g switch. The second line, “Wake-on: g” tells you that it is already enabled. If it is not, enable it:

$ sudo ethtool -s enp0s25 wol g

If it does not stay enabled after a restart, create an entry in /etc/crontab to run the command after every restart:

@reboot root /usr/bin/ethtool -s enp0s25 wol g

Shutdown the machine, and from a second device on the same network send the command to wake it up, using the MAC address of your target machine’s Ethernet interface:

$ /usr/bin/wakeonlan d1:50:f2:82:e7:fb

If your target machine and second device are on the same network but in different subnets, specify the broadcast address for your target machine:

$ /usr/bin/wakeonlan -i d1:50:f2:82:e7:fb


Wake-on-LAN is an Ethernet standard for remotely waking up a computer by sending it a wakeup signal over a network. wakeonlan is the name of the command, and the name of the package on most Linuxes.

When your computer is shutdown it isn’t really turned off, but is in a low power mode, and can receive and act on the magic packet wakeup signal.

wakeonlan sends the magic packet over UDP port 9. The magic packet goes to the network’s broadcast address, and every host on the network receives this packet. The MAC address ensures that only the host with that address will wake up.

The target machine wakes up just as if you had pressed the power button.

You may send the magic packet over the Internet to wake up a machine outside your local network. See TODO ssh chapter/recipe.

See Also

1.10 Setting up Remote Wakeups over Wifi (WoWLAN)


Your want to wake up remote computer via its wireless interface (Wakeon-Wireless_LAN, or WoWLAN).

This recipe is for waking up a machine on the same network as the device you use to send the wakeup signal.


Your machine must have an onboard wireless interface, either integrated on the motherboard or PCI. It will not work with a USB interface because there is no power to the USB bus when the machine is shut down.

First enter the UEFI setup of the machine you want to wake up remotely, and enable any Wake-on-LAN settings. An important security precaution is to also disable all settings that enable PXE booting. If PXE boot is enabled and you have a PXE server on your network, it is possible that your machine will wake up to PXE boot and install a new image, overwriting the existing installation.

Then exit and finish starting up. Install the iw command, and use it to list all of your wireless devices:

$ iw dev
        Interface wlxcc3fd5fe014c
                ifindex 3
                wdev 0x1
                addr 9c:bf:25:fe:0e:7c
                ssid accesspointe
                type managed
                channel 11 (2462 MHz), width: 20 MHz, center1: 2462 MHz
                txpower 20.00 dBm

If there is more than one, query the one you want to use. The following example shows a wireless interface does not support WoWLAN:

$ iw phy0 wowlan show
command failed: Operation not supported (-95)

The following example is for an interface that supports WoWLAN, and it is not enabled:

$ iw phy0 wowlan show
WoWLAN is disabled

Enable WoWLAN:

$ sudo iw phy0 wowlan enable magic-packet
WoWLAN is enabled:
  * wake up on magic packet

iw dev provides the MAC address, which your second device needs to send the magic packet:

$ /usr/bin/wakeonlan 9c:bf:25:fe:0e:7c

Send the magic packet to a machine on the same network, but in a different subnet, using the broadcast address of the subnet:

$ /usr/bin/wakeonlan -i 9c:bf:25:fe:0e:7c


This works well when the remote computer is a laptop, as laptops have integrated wireless network interfaces that typically support more features. Desktop machines usually don’t ship with integrated wireless interfaces, so you must shop carefully for a PCI/PCIe wireless adapter that support WoWLAN and Linux.

You may send the magic packet over the Internet to wake up a machine outside your local network. See TODO ssh chapter/recipe.

See Also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.