How to Remotely Reboot your Server.

This is the fifth in a series of posts featuring Protips, tips, tricks, hacks, and secrets provided by Our Team 🙊 — We want to share our top tips for the growing and thriving Linux community out there. Because sometimes you need a little help...

How to Remotely Reboot your Server.

Rebooting your Linux server may be required under some circumstances, e.g., when troubleshooting hardware issues, installing applications, updating the Kernel, configuring your NIC, and so on.
It's kind of cliché, but indeed true – rebooting your Linux server can fix a wide range of issues. Any faulty software is purged from active memory and has it restarts, it loads a fresh, clean copy of the software into active memory.

🎧




Mostly, a reboot is needed so that the changes can be taken into account on the server. If you’re running a headless Linux server, you need to know how to restart the system from the command line.

Yes, of course, we can boot the Linux Server, log in using ssh, and reboot the Server but we will use a shortcut.

SSH reboot command


We will use ssh along with its -t option to log in and run commands.
see below:

$ ssh -p 12344 -t snubmonkey@virtualmonkey 'sudo reboot'

OR

$ ssh -p 12344 -t snubmonkey@virtualmonkey 'sudo shutdown -r now'

OR

$ ssh -p 12344 -t snubmonkey@virtualmonkey 'sudo shutdown -r 2:45'

OR

$ ssh -p 12344 -t snubmonkey@virtualmonkey 'sudo shutdown + 45'

The -toption forces the remote system to enter the command in a terminal. Replace snubmonkey@virtualmonkey with the username @ server name that you want to restart.
Without -t you will see an error: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper.

The p option is used here to specify the port number to which to connect.

-r tells it to restart, hh:mm sets a specific time, +mm sets a countdown.

Bonus: Rebooting a large number of Linux servers


Let's say we have 4 Linux servers (snub1, snub2, snub3, and snub4). We will put those servers' IPs in a servers.txt file which I would use to loop through.

We can use a bash for|loop:

#!/bin/bash
##

file = ../../ip_address.txt

for server in $(cat file)

do

   ssh -t root@${server} 'reboot'

done
##
#

What this for loop does is, it goes through each server in the ip_address.txt file, and then it runs ssh -t root@${server} 'reboot' command for each item in the list.

Say you want to reboot 100 servers. In that case, a bash for|loop will run slow and might not be very useful. As a result, IT automation technology like Ansible is welcome.