Templario_7777

Configuracion Basica Ubuntu Server

Aug 27th, 2025 (edited)
469
0
Never
10
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.90 KB | None | 0 0
  1. ## Creacion de un usuario nuevo
  2. sudo adduser apiuser
  3. sudo usermod -aG sudo apiuser
  4.  
  5. ##Genera un par de llaves publico-privadas para la Autoridad Certificadora (crear en Maquina Local)
  6. ssh-keygen -t ed25519 -f ~/.ssh/"nombre_de_llave_CA" -C "SSH User CA" (String que permirita identificar es es para CA solamente)
  7.  
  8. ##Genera un par de llaves publico-privadas para un usario del servidor
  9. ssh-keygen -t ed25519 -f ~/.ssh/"nombre_de_llave" -C "user@pc" (String que permitira identificar de quien es el certificado)
  10.  
  11. ##FIRMA LA LLAVE PUBLICA PARA GENERAR EL CERTIFICADO
  12.  
  13. ssh-keygen -s ~/.ssh/"nombre_de_llave_CA" \
  14.   -I usuario@vps-cert \               # un identificador legible
  15.   -n usuario_remoto \                 # principal debe coincidir con el de /etc/ssh/authorized_principals/apiuser
  16.   -V +52w \                           # vigencia del certificado (52 semanas de ejemplo)
  17.   ~/.ssh/"nombre_de_llave".pub
  18. # Esto crea: ~/.ssh/id_ed25519-cert.pub
  19.  
  20. ##COMPROBAR "Principals" del certificado creado###
  21.  
  22. ssh-keygen -L -f ~/.ssh/id_ed25519-cert-pub ##nombre del certificado.pub
  23.  
  24. # En la VPS
  25. mkdir -p /etc/ssh
  26. nano /etc/ssh/trusted_user_ca_keys #PEGA_AQUI_EL_CONTENIDO_DE_tu_~/.ssh/"nombre_de_llave_CA".pub
  27. chmod 644 /etc/ssh/trusted_user_ca_keys #Le otorga permisos de solo lectura a otros usuarios que no sean root al archivo
  28.  
  29.  
  30. # En la VPS
  31. mkdir -p /etc/ssh/authorized_principals
  32.  
  33. echo "usuario_remoto" > /etc/ssh/authorized_principals/apiuser
  34. chmod 644 /etc/ssh/authorized_principals/apiuser #Le otorga permisos de solo lectura a otros usuarios que no sean root al archivo
  35.  
  36.  
  37. ##EDITAR sshd_config en la VPS
  38.  
  39. # === Autenticación con certificados de usuario (OpenSSH CA) ===
  40. PubkeyAuthentication yes
  41. TrustedUserCAKeys /etc/ssh/trusted_user_ca_keys
  42. # Opción A (global): todos los usuarios usan el mismo archivo de principals
  43. AuthorizedPrincipalsFile /etc/ssh/authorized_principals/%u
  44.  
  45.  
  46. # === Endurecimiento recomendado (fase de transición) ===
  47. # Mantén passwords activados por ahora hasta probar el certificado:
  48. PasswordAuthentication yes
  49. KbdInteractiveAuthentication no
  50. ChallengeResponseAuthentication no
  51.  
  52. # Paso intermedio: bloquea root por password, pero permite su llave/cert mientras migras
  53. PermitRootLogin prohibit-password
  54.  
  55. # Límites sensatos
  56. LoginGraceTime 30
  57. MaxAuthTries 3
  58. ClientAliveInterval 300
  59. ClientAliveCountMax 2
  60.  
  61. ##COMPROBAR ERRORES DE SINTAXIS EN EL "sshd_config"
  62.  
  63. sshd -t
  64.  
  65. ##AUTORIZANDO QUE SSHD (ROOT) LEA LOS ARCHIVOS
  66.  
  67. sudo chmod 644 /etc/ssh/authorized_principals/apiuser
  68. sudo chown root:root /etc/ssh/authorized_principals/apiuser
  69.  
  70. ## REINICIAR SERVICIO SSH
  71. systemctl reload ssh
  72.  
  73. ##Inicia una conexion ssh "evitando el error Too may auth failures"
  74. ssh -vvv -o IdentitiesOnly=yes -i ~/.ssh/id_MyPrivKey apiuser@IP
  75.  
  76. ## SI LA CONEXION ES EXITOSA SE DEBE EDITAR NUEVAMENTE EL ARCHIVO SSHD_CONFIG
  77.  
  78. PermitRootLogin no
  79. PasswordAuthentication no
  80.  
  81. sudo systemctl reload ssh
  82.  
  83.  
  84.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment