You are not logged in.
Hi all,
I ran into a great ssh tip in Linux Server Hacks (by O'Reilly), it goes something like this:
1) create an ssh key with
$ ssh-keygen -t rsa
2) create an .ssh directory in the server with the right permissions
$ ssh nameofserver "mkdir .ssh ; chmod 0700 .ssh"
3) place key in server folder
$ scp .ssh/id_rsa.pub nameofserver:.ssh/authorized_keys2
Now you can log in to your server without typing your users password, but here comes the interesting bit,
create an executable file called ssh-to (or whatever) in ~/bin like this:
1) make the file $HOME/bin/ssh-to with whatever editor suits you, and ad the following lines
#!/bin/sh
ssh `basename $0` $*
2) make it executable
$ chmod +x $HOME/bin/ssh-to
3) symlink your servername to the executable
$ ln -s nameofserver $HOME/bin/ssh-to
Now you only need to type in nameofserver to log in with ssh seamlessly without password. It even works with nameofserver uptime, nameofserver ls, exc...
What i am wondering ablout though, is if there is a way one could expand this simple script to detect if the server has the necessary key file, and if not add it with scp. In that way you only need to type in your server password once, and the ssh key will be added automaticly. Any suggestions?
Last edited by dansimon (2011-08-18 09:24:21)
Offline
I think I'd go at the problem a bit differently. Why not write a dedicated "ssh-to-add" that does the scp and then creates the ssh-to symlink?
Offline
Sounds like a good ide, what i was looking for though was some way of making the process automatic. What i mean is that the script must in some way be able to cheak if the server has the ssh key, if so log in with ssh, if not scp it over then log in with ssh...
I gues i need some if then loop here but am not terably good at bash scripting, and some good suggestions would be capital ![]()
Offline
I'm not even sure that what you're asking for is even possible, which is why I suggested an alternative approach. I hesitate to say "not doable" because ssh has an awesomely huge man page and I could definitely be wrong. Maybe you could use something like Expect to parse the strings but I still don't see a (easy) way to store the password for reuse without doing something quite insecure.
By the way, this is really more of a "Programming" problem than a "Protection" problem.
Offline
Yeah i suppose your right, i'll mark this post as [SOLVED]. If i find some whizzardry way of doing this though, i'll post my findings in this thread.
Thanks for the quick repply ![]()
Offline
FWIW, a simpler way to set up an auto ssh login is to add an entry like this to ~/.ssh/config
Host svr
User whatever
Port 2222
Hostname 12.34.56.78
IdentityFile ~/.ssh/svrproduced with a ssh-keygen -f ~/.ssh/svr then use an alias like alias svr='ssh svr' to simply login to that remote host with just svr, which also works with scp and rsync. The public key can be pushed to the server in one go with something like this...
cat ~/.ssh/svr.pub | ssh whatever@12.34.56.78 -p 2222 'cat >> .ssh/authorized_keys;chmod 600 .ssh/authorized_keys'I've got a script that automates this if anyone is interested.
Last edited by markc (2011-08-19 16:40:23)
Offline
Could you post that script in this thread? Very cool solution btw!
Offline
% newkey
Usage: newkey hostlabel user@remotehost [port] [identity@email]
It produces long ugly key names but they are unique so that associated shkey and rmkey scripts can manage them.
#!/bin/bash
# newkey v0.1.1 20090818 markc@renta.net (AGPLv3)
[ -z "$2" ] && echo "Usage: newkey hostlabel user@remotehost [port] [identity@email]" && exit 1
[ -n "$3" ] && PORT=$3 || PORT=22
[ -n "$4" ] && AUTH=$4 || AUTH=$2
IDFILE=$1'_'$2'_'$PORT'_'$AUTH
[ -f ~/.ssh/$IDFILE ] && echo "Error: ~/.ssh/$IDFILE already exists" && exit 2
LHS=$(echo $2|cut -d@ -f1)
RHS=$(echo $2|cut -d@ -f2)
ssh-keygen -q -f ~/.ssh/$IDFILE -C $AUTH
echo "
Host $1
User $LHS
Port $PORT
Hostname $RHS
IdentityFile ~/.ssh/$IDFILE" >> ~/.ssh/config
chmod 600 ~/.ssh/config ~/.ssh/$IDFILE*
cat ~/.ssh/$IDFILE.pub | ssh $2 -p $PORT 'cat >> .ssh/authorized_keys;chmod 600 .ssh/authorized_keys'Offline