Restarting the Linux Machine from the Command Line
Using the Reboot Command
The most straightforward way to restart a Linux machine from the command line is to use the reboot
command. This command can be executed by any user with sudo privileges.
$ sudo reboot
When you run this command, the system will begin the reboot process, which includes shutting down running processes, flushing file system buffers, unmounting file systems, and finally restarting the system.
Restarting with Specific Options
You can also use various options with the reboot
command to customize the restart process. For example, to perform a forced reboot without properly shutting down running processes, you can use the -f
or --force
option:
$ sudo reboot -f
To reboot the system and then power it off, you can use the -p
or --poweroff
option:
$ sudo reboot -p
Verifying the Reboot Process
After issuing the reboot
command, you can monitor the system's progress by checking the system logs. On Ubuntu 22.04, you can use the journalctl
command to view the system log:
$ sudo journalctl -b -1
This will display the log entries from the previous boot, which should include information about the reboot process.
Restarting from a Script
If you need to automate the reboot process, you can create a shell script that executes the reboot
command. For example, you could create a script named restart.sh
with the following contents:
#!/bin/bash
echo "Restarting the system..."
sudo reboot
Then, make the script executable and run it:
$ chmod +x restart.sh
$ ./restart.sh
This approach can be useful for automating system maintenance tasks or for including the reboot command as part of a larger automation workflow.