Ya muchos sabemos usar esto de las llaves de SSH pero como sea siempre es conveniente poner este tipo de notas por si algón dóa lo olvidamos ya que me ha pasado que cosas tan sencillas se me olvidan :S
bueno veamos que hay que hacer:
En dónde vas a realizar la conexión hay que poner esto:
[terminal]
ssh-keygen -t dsa
[/terminal]
Esto genera 2 archivos en $home/.ssh/ (uno póblico y otro privado). Ahora agarramos el archivo póblico (id_dsa.pub) y lo copiamos al host a donde haremos la conexión. Ya saben usen:
[terminal]
scp id_dsa.pub usuario@host:~
[/terminal]
Bien ahora nos conectamos al host remoto y hacemos lo siguiente:
[terminal]
cat $home/id_dsa.pub >> $home/.ssh/authorized_keys
chmod 644 $home/.ssh/authorized_keys
[/terminal]
y listo ya podremos conectarnos a este host sin necesidad de usar contraseóa puesto que la llave ya esta inyectada
Pero bueno hacer todo esto es mucho rollo verdad, yo para evitar andar haciendo tanto movedero de archivos uso el siguiente script que un compaóero me paso. Con este script solo pongo un comando, coloco la contraseóa del host remoto 2 veces y listo, todo lo demas se hace solo (h)
[terminal]
[12:24 ] [ivan@lily][~/script]
#:./ssh-send-key rico.org.mx
user (ivan): usuario
Setting up DSA authentication for usuario@rico.org.mx…
DSA public key no found. Creating key…Generating public/private dsa key pair.
Created directory {{/home/ivan/.ssh{{.
Your identification has been saved in /home/ivan/.ssh/id_dsa.
Your public key has been saved in /home/ivan/.ssh/id_dsa.pub.
The key fingerprint is:
0c:54:46:fb:ac:59:b2:cd:c4:c2:78:55:50:de:08:23 ivan@lily
done.
The authenticity of host {{rico.org.mx (207.44.162.68){{ can{{t be established.
RSA key fingerprint is 42:68:cf:35:92:4e:cb:a0:6e:95:4d:84:21:ab:88:f0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added {{rico.org.mx,207.44.162.68{{ (RSA) to the list of known hosts.
ricoorg@rico.org.mx{{s password:
stdin: is not a tty
id_dsa.pub 100% 599 0.6KB/s 00:00
ricoorg@rico.org.mx{{s password:
stdin: is not a tty
SUCCEDED — public key authentication as usuario@rico.org.mx.
stdin: is not a tty
[12:24 ] [ivan@lily][~/script]
#:
[/terminal]
Aquó les dejo el código para que lo usen en sus servidores, a mi me ha sido muy ótil.
[code='Bash']
#!/bin/sh
#
# This script will upload the ssh public key to the server specified.
#
# Copyright (C) 2002, Rafael Seplveda
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
### Code:
version="1.0.0"
ask_user()
{
# Ask for a username
echo -n "user ($USER): "
read usr
[ $usr ] || usr=$USER
}
show_help()
{
# Text to be displayed as {{help{{
echo "Usage: ssh-send-key [OPTION] [USER@[HOST]]"
echo "Upload public key to a remote server using ssh."
echo
echo " -i IDENTITY use this identity for key authentication"
echo " like `dsa{{ (default) or `rsa{{."
echo " -h display this help and exit"
echo " -v output version information and exit"
echo
echo "If no arguments are used, it will ask for the host and user. The"
echo "user will default a hint to the user used in that moment. You can"
echo "specify the hostname alone and the user that will be used will be"
echo "prompted. You can also specify the user and the hostname in the"
echo "prompt in the format {{user@host{{ and that values will be used."
echo
echo "We cannot circumvent the problem that you need to type twice the"
echo "password. This is because we need it one time to send the key and"
echo "another one to put it correctly on the server."
echo
echo "Only SSH protocol version 2 is supported."
echo
echo "Report bugs to Rafael Seplveda ."
}
show_version()
{
#Text do be displayed as {{version{{
echo "ssh-send-key $version"
echo "Written by Rafael Seplveda."
echo
echo "Copyright (C) 2002, Rafael Seplveda"
echo "This is free software; see the source for copying conditions. There is NO"
echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
}
#define options. {{h{{ for help, {{v{{ for version and {{c{{ for identity.
getopts vhi: option
optexit=$? #save the exit status of getopts.
#help option
if [ "$option" = "h" ] || { [ $optexit -eq 0 ] && [ $OPTIND -eq 1 ]; }; then
show_help
exit 0;
fi
#version option
if [ "$option" = "v" ]; then
show_version
exit 0;
fi
#identity option
if [ -z "$OPTARG" ]; then
identity="dsa"
arg=$1
else
identity="$OPTARG"
arg=$3
fi
identity_uc=$( echo $identity | tr {{a-z{{ {{A-Z{{)
#read host and user name
if [ $arg ]; then
if [ $arg = $( echo $arg | sed -e {{s/(.*)@(.*)/1 2/{{ ) ]; then
host=$arg
ask_user
else
usr=$( echo $arg | sed -e {{s/(.*)@.*/1/{{ )
host=$( echo $arg | sed -e {{s/.*@(.*)/1/{{ )
fi
else
echo -n "SSH server: "
read host
ask_user
fi
#generate identity key if we don{{t find one
echo "Setting up $identity_uc authentication for ${usr}@${host}..."
if [ -f ~/.ssh/id_$identity.pub ]; then
echo "$identity_uc public key present. Using it."
else
echo -n "$identity_uc public key no found. Creating key..."
ssh-keygen -t $identity -f ~/.ssh/id_$identity -N ""
echo "done."
fi
#upload identity key and put it in the correct place with correct permissions
scp -oProtocol=2 ~/.ssh/id_$identity.pub ${usr}@${host}:~/ &&
ssh -oProtocol=2 ${usr}@${host} "if [ ! -d ~/.ssh ]; then
mkdir -m 700 ~/.ssh
fi
cat ~/id_$identity.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
rm ~/id_$identity.pub"
#see if we succed
ssh -oPasswordAuthentication=no -q -q
${usr}@${host} "echo SUCCEDED -- public key authentication as ${usr}@${host}." ||
{ echo "FAILED -- public key authentication could not be set."; exit 1; }
exit 0
# ssh-send-key ends here
[/code]
Espero que les sirva.
aahh muy importante para que esto funciono deben activar en el /etc/sshd/sshd_config la sentencia:
AuthorizedKeysFile .ssh/authorized_keys