Try New Technology

SSH: Edit, Backup, and Restart service simply

Below is a quick Bash script I wrote to allow me to quickly edit the sshd_config file, while also backing up the old copy and restarting the SSH Daemon for me when the edit was done. I keep this saved in “/usr/bin/sshdmod”. Nothing fancy, but figured some one else might like it:

# Made by Logan Rogers-Follis of
# Last Updated On 2009-11-20
# Known Bugs: NONE
# Script: This script will create a backup of the sshd_config file before editing, and then backup the newest copy , and then restart the sshd service once you are done editing.

# Folder location of sshd_config file.
sshdloc="/etc/ssh" # Change only if this is not where your sshd_config file is located.

#Check/Clear .sshd_config.swp in case some one else is editing it currently.
if [ -e $sshdloc/.sshd_config.swp ]
echo -n "$sshdloc/.sshd_config.swp file found. Delete to continue? (yes/NO) "
read swpcheck
if [ "$swpcheck" == "yes" -o "$swpcheck" == "y" ]
echo -n "Deleting .sshd_config.swp: "
rm -f $sshdloc/.sshd_config.swp
echo "OK"
echo "Exiting"
exit 1

#Pick which editor to use. Default is Vi.
echo -n "Edit sshd_config file with? (nano/VI) "
read editcheck

# Get the current time: YearMonthDayHourMinutesSeconds
timestart=`date +%Y%m%d%H%M%S`

if [ "$editcheck" == "nano" -o "$editcheck" == "n" ]
cp $sshdloc/sshd_config $sshdloc/sshd_config.prev.$timestart
nano $sshdloc/sshd_config
elif [ "$editcheck" == "vi" -o "$editcheck" == "v" ]
cp $sshdloc/sshd_config $sshdloc/sshd_config.prev.$timestart
vi $sshdloc/sshd_config
cp $sshdloc/sshd_config $sshdloc/sshd_config.prev.$timestart
vi $sshdloc/sshd_config

# Get the current time: YearMonthDayHourMinutesSeconds
timedone=`date +%Y%m%d%H%M%S`

#Restarts SSH Daemon.
echo -n "Backing up new copy of sshd_config to $sshdloc/sshd_config.latest.$timedone: "
rm -f $sshdloc/sshd_config.latest.*
cp $sshdloc/sshd_config $sshdloc/sshd_config.latest.$timedone
echo "DONE"
service sshd restart

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...