SSH restart Linux system using reboot command

I am a new Linux server system administrator. I can log in to my server using ssh keys for the root user. Can you tell me if it is safe to reboot Linux server using ssh. How can I use ssh to reboot the Linux box?

Yes, you can boot the machine or Linux server using ssh. The SSH is protocol. It associated with Secure Shell. In short, ssh is a client-server technology that allows secure remote login from one computer to another. The ssh client is a defacto standard in Linux and Unix world for managing servers. This page shows how to restart Linux system using ssh command.

How to login using ssh

The syntax is:
ssh [email protected]
ssh [email protected]
ssh [email protected]

For security reasons root login is always disabled on Linux servers. In that case login as normal user and use the su command or sudo command to gain root shell:
ssh [email protected]
sudo -i

Verify with id command:

SSH restart Linux system

Now, you know how to log in as root user using ssh command:
ssh [email protected]
We will now reboot the Linux server using ssh. The syntax is as follows (use any one of the following command):
# reboot
# shutdown -r now
# shutdown -r 0

One can use the sudo command as follows:
$ sudo shutdown -r now
All of the above commands would reboot the machine. See “SSH Public Key Based Authentication on a Linux/Unix server” for more information.

SSH reboot command example

There is a shortcut too. You can use ssh to log in and run commands. The syntax is as follows when you log in as non-root user:
ssh -t [email protected] "sudo reboot"
ssh -t [email protected] "sudo shutdown -r now"

Of course, if you log in to Linux server as root user, avoid typing sudo:
ssh -t [email protected] "reboot"
ssh -t [email protected] "shutdown -r now"

Restarting Linux computer using ssh command line option

sudo: no tty present and no askpass program specified

If you skip the -t option to the ssh command, you will see an error that read as follows on screen:

sudo: no tty present and no askpass program specified

A note about rebooting a large number of Linux servers

You can use bash for loop as follows to reboot 5 servers named www1, www2, db1, db2, and cache1:

for s in www1 www2 db1 db2 cache1:
   ssh -t root@${s} "shutdown -r now"

And, there is an Ansible tool to reboot Linux machine or server with playbooks too. Say you want to reboot 100 AWS vms or servers. A bash for loop is slow and not very useful. Hence, you use an IT automation tool such as Ansible.


In this tutorial, you learned how to reboot the Linux machine using ssh. See shutdown(8) man page here for more info.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.