SSH restart Linux system using reboot command

last updated in Categories , , , , ,

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 user@server
ssh user@server-ip-address
ssh root@server1.cyberciti.biz

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 vivek@server1.cyberciti.biz
sudo -i

Verify with id command:
id
ssh restart system using reboot

SSH restart Linux system

Now, you know how to log in as root user using ssh command:
ssh user@linux-box
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 vivek@server1.cyberciti.biz "sudo reboot"
ssh -t vivek@server1.cyberciti.biz "sudo shutdown -r now"

Of course, if you log in to Linux server as root user, avoid typing sudo:
ssh -t root@server1.cyberciti.biz "reboot"
ssh -t root@server1.cyberciti.biz "shutdown -r now"

SSH restart Linux system command
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:
do
   ssh -t root@${s} "shutdown -r now"
done

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.

Conclusion

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

Posted by: Vivek Gite

The author is the creator of nixCraft 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.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!