Este tutorial te guía paso a paso para cambiar el puerto de conexión SSH en un servidor Linux, asegurarte de que esté escuchando correctamente y resolver el error típico:

Missing privilege separation directory: /run/sshd

Ideal para configuraciones minimalistas, VPS o contenedores.


1. Editar el archivo de configuración de SSH

Abre el archivo:

nano /etc/ssh/sshd_config

Busca esta línea:

#Port 22

Y cámbiala por:

Port 2222

(Quita el # si está comentado). Guarda y cierra.


2. Revisar si hay algún firewall bloqueando el puerto

Comprueba qué firewall tienes:


which ufw && ufw status
systemctl is-active firewalld
iptables -L -n

Si no hay respuesta en los tres casos, no tienes firewall activo. Si usas UFW o firewalld, abre el puerto:


# UFW
ufw allow 2222/tcp

# Firewalld
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload

3. Reiniciar el servicio SSH


systemctl restart ssh
# o
systemctl restart sshd

⚠️ 4. Solución al error /run/sshd

Este error significa que el directorio temporal requerido por sshd no existe.

✔️ Solución inmediata:


mkdir -p /run/sshd
chmod 755 /run/sshd
systemctl restart ssh

? 5. Verificar que SSH está escuchando en el nuevo puerto

ss -tuln | grep 2222

Salida esperada:


LISTEN 0 128 0.0.0.0:2222 0.0.0.0:*  
LISTEN 0 128 [::]:2222   [::]:*

? 6. Probar conexión SSH desde otra máquina

ssh root@IP_DEL_SERVIDOR -p 2222

7. Hacer permanente la solución al error de /run/sshd

Para evitar que el error reaparezca tras reiniciar el sistema, crea un servicio systemd personalizado.

✏️ Crear el archivo de servicio:

nano /etc/systemd/system/fix-sshd-run-dir.service

Pega esto:

[Unit]
Description=Crear /run/sshd si no existe
Before=ssh.service
ConditionPathExists=!/run/sshd

[Service]
Type=oneshot
ExecStart=/bin/mkdir -p /run/sshd
ExecStartPost=/bin/chmod 755 /run/sshd

[Install]
WantedBy=multi-user.target

Recargar systemd y habilitar el servicio:


systemctl daemon-reexec
systemctl daemon-reload
systemctl enable fix-sshd-run-dir.service

Resultado final

  • Puerto SSH cambiado con éxito a 2222 .
  • Servicio SSH funcionando sin errores.
  • Conexión establecida correctamente desde otro cliente.
  • Solución persistente al problema /run/sshd.