When you want to turn off your Raspberry Pi, simply pulling the power cord is not a good idea. This is because the Raspberry Pi may still be writing data to the SD card, in which case simply powering down may result in data loss or, even worse, a corrupted SD card.

Before powering it down, you should always safely shut down the Raspberry Pi. You can do so either from the command-line terminal or the desktop GUI menu. Here we explore both methods, including special options for the terminal command.

Shut Down From the Terminal

In a Terminal window on the Raspberry Pi OS desktop, or from the command-line interface, enter the following command followed by the Return key to safely shut down your Raspberry Pi.

        sudo shutdown -h now
    
Shut down Raspberry Pi in terminal

Note that you need sudo user privileges to execute the shutdown command. The -h option tells the Raspberry Pi to halt what it’s doing, while the now parameter instructs it to shut down straight away rather than waiting.

To avoid possible data loss and SD card corruption, before removing the power you should wait a few seconds after shutdown for the green LED to stop blinking on the Raspberry Pi. Once the LED goes out, it’s safe to power down.

While it is possible to use the sudo halt command instead to shut down the system, this can occasionally lead to unexpected results. So it is best practice to use the shutdown command.

Delay Shutdown

If you want to delay shutdown of the Raspberry Pi, you can replace now in the previous command with the number of minutes to wait. For instance, the following command will shut down the Raspberry Pi in 20 minutes’ time:

        sudo shutdown -h 20
    
Shut down Raspberry Pi with delay

You can also instruct the system to shut down at a particular time, using the 24-hour clock. To set it to shut down at 5:30pm, for example, enter:

        sudo shutdown -h 17:30
    

To cancel a scheduled shutdown, enter the following command:

        sudo shutdown -c
    

Reboot the Raspberry Pi

After a shutdown, you simply need to power up the Raspberry Pi to boot it up again.

If you want to reboot the Raspberry Pi automatically after shutdown, enter the following command, with the -r option to reboot:

        sudo shutdown -r now
    

The Raspberry Pi will then shut down before immediately starting to boot up again.

Shut Down Remotely

All of these terminal commands can also be used when accessing your Raspberry Pi remotely from another computer using SSH (Secure Shell). Naturally, the SSH connection will be closed once the Raspberry Pi shuts down.

Rebooting via SSH can be a useful technique if the Raspberry Pi system has frozen. This enables you to restart it without having to physically unplug the power and plug it back in – handy if the Raspberry Pi is in a difficult-to-reach location.

Shut Down From the Desktop GUI

You can also shut down the Raspberry Pi from the Raspberry Pi OS desktop environment GUI. From the main raspberry icon menu, click on the Shutdown option at the bottom. A dialog window will appear, where you can select from three options: Shutdown, Reboot, and Logout.

Shut down Raspberry Pi in GUI

The Logout option is only useful if you have more than one user account on your Raspberry Pi. It closes down all the programs and then logs out the current user.

Add a Power Switch

If you want to shut down the Raspberry Pi with the mere press of a push-button, which triggers a shutdown script, check out our guide to adding a power switch to the Raspberry Pi.

Shutting Down Raspberry Pi Safely

You now know how to shut down your Raspberry Pi safely, without the risk of data loss or SD card corruption, from the desktop GUI or command-line terminal. The latter enables you to get under the hood of the Raspberry Pi and offers a range of powerful commands you will want to try out.