Jul
28

Automatically Add SSH Key to Your Servers

By bradj  //  Bash, Linux  //  No Comments

I am constantly working with multiple linux servers (mostly Debian). Whenever I create a new server I immediately execute this simple script I created which adds my box’s public key to the server I am trying to communicate with. If you know about SSH and SSH Keys then you know what I am talking about… if not… read this. Basically… an SSH Key circumvents the process of sending a password over the wire which is susceptible for interception and decryption. When connecting to a server under the SSH protocol the server sends the client a key/file that is encrypted with super awesomeness. The client reads the file and is like… dude… you need a password… and you’re all like… ok try this password… and then it’s like ok that works I am going to open the connection now…. and you’re like sweet and are able to start h4xing the crap out of life. The difference between this and normal auth: the SSH client does that communication on the client system instead of doing it over the wire! Yay… not rocket science but whatever.

Anyway… this script I wrote sets this junk up for ya. Is there another way to do it that is probably better? Absolutely. If you know it… then please share! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
 
usage()
{
cat << EOF
usage: $0 options
 
This script appends your public SSH Key on the server you specify which allows you to ssh into that server without typing a password 
 
OPTIONS:
   -h      Show this message
   -u      Username associated with the server you are accessing
   -s      Server address
   -f      SSH Public key file
 
EXAMPLES:
   ./distributeSSH.sh -u username -s 172.1.1.1 -f ~/.ssh/id_dsa.pub
EOF
}
 
user=
server=
file=
while getopts “hu:s:f:” OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         u)
             user=$OPTARG
             ;;
         s)
             server=$OPTARG
             ;;
         f)
             file=$OPTARG
             ;;
         ?)
             usage
             exit
             ;;
     esac
done
 
if [[ -z $user ]] || [[ -z $server ]] || [[ -z $file ]]
then
     usage
     exit 1
fi
 
echo "User = $user"
echo "IP = $server"
echo "SSH Key file = $file"
 
echo "Uploading file on $server in the home directory of $user"
scp $file $user@$server:~/
echo "Executing necessary commands on $server to configure ssh key"
 
ssh -t $user@$server "if [ ! -d ~/.ssh/ ]; then echo Making ssh directory; mkdir ~/.ssh; else echo Found ssh directory; fi; chmod 700 ~/.ssh; cat ~/id_dsa.pub >> ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys; rm -f ~/id_dsa.pub"
echo "Configuring client to connect without using password..."
ssh -o PreferredAuthentications=publickey $user@$server echo "add this line to your .bashrc: alias server='ssh -X $user@$server'"

Leave a comment

You must be logged in to post a comment.