Tabla de Contentenido
Conexión remota ssh sin contraseña:
En muchas ocasiones necesitamos conectar de forma programada a un servidor remoto para realizar diferentes tareas de mantenimiento. Normalmente realizamos backups diarios a máquinas externas, automatizamos reinicios cuando detectamos fallos mediante monitorización, etc.
En multitud de ocasiones necesitamos poder conectar a un servidor remoto mediante ssh sin password.
Seguiremos éstos sencillos pasos para configurar la clave pública que nos permita ésta conexión remota:
Entramos en el servidor orígen como usuario root
entramos en el directorio /root/.ssh:
cd .ssh
Generamos la clave pública:
ssh-keygen -t rsa
Le damos todo a enter, es decir no ponemos ningún passwd:
root@ns210:~/.ssh# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:XKF7Q/WnfIIG56LcFu/zkOCRFFPum5unKepiIDWsdkk root@ns110
The key’s randomart image is:
+—[RSA 1024]—-+
| +.o |
| . . . |
| . + . = o . .|
| . + . = . o o |
| + o S O = + .|
| + + . = . + o |
| . o . o + * |
| o+ ….=. |
| . oo. .*=. |
+—-[SHA256]—–+
Copiamos la clave al servidor remoto que queremos conectarnos sin contraseña:
ssh-copy-id -i id_rsa.pub root@251.83.47.72
root@ns210:~/.ssh# ssh-copy-id -i id_rsa.pub root@251.83.47.72
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: «id_rsa.pub»
The authenticity of host ‘251.83.47.72 (251.83.47.72)’ can’t be established.
ECRSA key fingerprint is SHA256:9kjCKKHXer5yPpx06xGjlc5EvF3hYE/xg8X/yP3Rx3U.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
root@251.83.47.72’s password:
Number of key(s) added: 1
Now try logging into the machine, with: «ssh ‘root@251.83.47.72′»
and check to make sure that only the key(s) you wanted were added.
Nos pedirá el passwd del servidor remoto para copiar la clave.
Verificamos el acceso remoto sin contraseña:
ssh root@251.83.47.72
root@ns110:~/.ssh# ssh ‘root@251.83.47.72’
Debian GNU/Linux 9 (stretch)
Linux ns132.servidoresadmin.com 4.19-ovh-xxxx-std-ipv6-64 #1083389 SMP Thu Jan 23 08:31:47 UTC 2020 x86_64 GNU/Linux
Server : 566885
IPv4 : 251.83.47.72
IPv6 : 2001:41d0:a:e2a::1
Hostname : ns132.servidoresadmin.com
installed ISPConfig-Version: 3.1.15p2
current ISPConfig-Version: 3.1.15p2
Last login: Sun Jul 5 20:45:05 2020 from 90.51.105.112
root@ns132:~#