Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Añadir inicio de sesión con certificado SSH en Ubuntu

Tiempo de lectura: 2 minutos

Hola, hoy vamos a aprender cómo podemos generar un certificado SSH en Ubuntu.

Lo primero que tenemos que hacer es editar el fichero de configuracion SSH:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo vi /etc/ssh/sshd_config
sudo vi /etc/ssh/sshd_config
sudo vi /etc/ssh/sshd_config

Tenemos que descomentar estas líneas:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

Una vez añadido estos cambios, tenemos que crear la clave.

Recomiendo hacer el proceso desde un ordenador externo al que estamos configurando. Para ello vamos a nuestra consola y ponemos:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh-keygen -t rsa -b 4096
ssh-keygen -t rsa -b 4096
ssh-keygen -t rsa -b 4096

Ahora tenemos que indicar dónde vamos a almacenar la clave:

En caso de que sea para el usuario actual, utilizamos el de defecto (pulsamos en enter).

*En caso de que lo queramos para root y estemos directamente en la máquina, introducimos:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/root/.ssh/id_rsa
/root/.ssh/id_rsa
/root/.ssh/id_rsa

Para más seguridad añadimos una contraseña de fase.

Ahora tenemos que añadir nuestra clave a las claves aceptadas para iniciar sesión.

En caso de hacer el proceso desde una máquina remota ponemos:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat ~/.ssh/id_rsa.pub | ssh usuario@host -p [numero_puerto] "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
cat ~/.ssh/id_rsa.pub | ssh usuario@host -p [numero_puerto] "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
cat ~/.ssh/id_rsa.pub | ssh usuario@host -p [numero_puerto] "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"

En usuario@host ponemos nuestro usuario y el host remoto.

En [numero_puerto] ponemos el número de puerto.

Este comando nos creará un authorized_keys con nuestra clave.

*En caso de hacerlo desde nuestra propia máquina ponemos:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Nos aseguramos que la ha copiado bien:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat .ssh/authorized_keys
cat .ssh/authorized_keys
cat .ssh/authorized_keys

Y ahora tenemos que copiar el contenido de la clave privada (que usaremos para iniciar sesión), en caso de que lo hagamos desde una máquina remota, si lo hacemos en nuestra propia máquina, usaremos la clave RSA Privada generada.

Si el nombre es por defecto usamos este comando:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat .ssh/id_rsa
cat .ssh/id_rsa
cat .ssh/id_rsa

Ahora tenemos que reiniciar el servicio ssh:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo service ssh restart
sudo service ssh restart
sudo service ssh restart

Ahora, teniendo nuestra clave RSA copiada, tenemos que iniciar sesión. Creamos un nuevo archivo llamado clave_rsa_privada y lo pegamos dentro de .ssh/ en caso de ubuntu. Ahora indicamos los permisos de la clave:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo chmod 600 /home/isma/.ssh/clave_rsa_privada
sudo chmod 600 /home/isma/.ssh/clave_rsa_privada
sudo chmod 600 /home/isma/.ssh/clave_rsa_privada

Configurando por ejemplo un fichero .ssh/config:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Host server
HostName IP_DE_SERVER
User USER_DEL_SERVER
Port PUERTO_SERVER
PubKeyAuthentication yes
IdentitiesOnly yes
IdentityFile ~/.ssh/clave_rsa_privada
Host server HostName IP_DE_SERVER User USER_DEL_SERVER Port PUERTO_SERVER PubKeyAuthentication yes IdentitiesOnly yes IdentityFile ~/.ssh/clave_rsa_privada
Host server
        HostName IP_DE_SERVER
        User USER_DEL_SERVER
        Port PUERTO_SERVER
        PubKeyAuthentication yes
        IdentitiesOnly yes
        IdentityFile ~/.ssh/clave_rsa_privada

Añadimos los datos necesarios, la ip del servidor, el usuario, el puerto y finalmente el archivo en el que hemos pegado la clave RSA.

Ahora podemos forzar a que solo se inicie sesión usando la RSA, para ello editamos de nuevo el archivo sshd_config

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo vi /etc/ssh/sshd_config
sudo vi /etc/ssh/sshd_config
sudo vi /etc/ssh/sshd_config

Y descomentamos la línea:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
PasswordAuthentication no
PasswordAuthentication no
PasswordAuthentication no

También tenemos que buscar en el fichero de configuración extra en el directorio /etc/ssh/sshd_config.d/

En mi caso he tenido que editar este fichero:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo vi 50-cloud-init.conf
sudo vi 50-cloud-init.conf
sudo vi 50-cloud-init.conf

Y comentamos la línea de:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
PasswordAuthentication yes
PasswordAuthentication yes
PasswordAuthentication yes

Y ahora escribimos el comando para reiniciar el ssh:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo systemctl restart ssh
sudo systemctl restart ssh
sudo systemctl restart ssh
0

Deja un comentario