Configurar SSH para GitHub
Guía para configurar claves SSH y poder clonar repositorios privados desde tu VPS.
¿Por Qué SSH en Lugar de HTTPS?
Section titled “¿Por Qué SSH en Lugar de HTTPS?”- ✅ No necesitas introducir usuario/contraseña cada vez
- ✅ Más seguro con autenticación por clave
- ✅ Funciona con repositorios privados sin problemas
- ✅ Recomendado para servidores de producción
1. Generar Clave SSH en el VPS
Section titled “1. Generar Clave SSH en el VPS”# Conectar al VPSssh root@tu-ip-vps
# Generar clave SSH (usar email de tu cuenta GitHub)ssh-keygen -t ed25519 -C "tu@email.com"Cuando pregunte:
- Enter file: Presiona Enter (usar ubicación por defecto)
- Passphrase: Presiona Enter 2 veces (sin passphrase para automatización)
Esto crea dos archivos:
~/.ssh/id_ed25519- Clave privada (¡NO compartir!)~/.ssh/id_ed25519.pub- Clave pública (esta la copiarás a GitHub)
2. Copiar la Clave Pública
Section titled “2. Copiar la Clave Pública”# Mostrar la clave públicacat ~/.ssh/id_ed25519.pubCopia todo el contenido que aparece (empieza con ssh-ed25519).
3. Añadir Clave a GitHub
Section titled “3. Añadir Clave a GitHub”- Ve a GitHub.com y haz login
- Click en tu foto de perfil → Settings
- En el menú lateral → SSH and GPG keys
- Click en New SSH key
- Rellena:
- Title:
VPS Hostinger(o nombre descriptivo) - Key: Pega la clave pública copiada
- Title:
- Click en Add SSH key
- Confirma con tu contraseña si te la pide
4. Verificar Conexión
Section titled “4. Verificar Conexión”# Probar conexión con GitHubssh -T git@github.comDeberías ver:
Hi usuario! You've successfully authenticated, but GitHub does not provide shell access.Si es la primera vez, preguntará si confías en el host. Escribe yes y presiona Enter.
5. Configurar Git (Opcional)
Section titled “5. Configurar Git (Opcional)”# Configurar nombre y email para commitsgit config --global user.name "Tu Nombre"git config --global user.email "tu@email.com"
# Verificar configuracióngit config --list6. Clonar Repositorios
Section titled “6. Clonar Repositorios”Ahora puedes clonar usando SSH:
# Formato SSH (recomendado)git clone git@github.com:usuario/repositorio.git
# Ejemplo con vps-docsgit clone git@github.com:daniclemente/vps-docs.gitSolución de Problemas
Section titled “Solución de Problemas”Error: Permission denied (publickey)
Section titled “Error: Permission denied (publickey)”# Verificar que la clave existels -la ~/.ssh/
# Verificar agente SSHeval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519
# Verificar conexiónssh -T git@github.comError: Could not open a connection to your authentication agent
Section titled “Error: Could not open a connection to your authentication agent”# Iniciar agente SSHeval "$(ssh-agent -s)"
# Añadir clavessh-add ~/.ssh/id_ed25519Clave no se reconoce
Section titled “Clave no se reconoce”# Verificar permisos correctoschmod 700 ~/.sshchmod 600 ~/.ssh/id_ed25519chmod 644 ~/.ssh/id_ed25519.pubMúltiples Claves SSH (Avanzado)
Section titled “Múltiples Claves SSH (Avanzado)”Si usas varias cuentas de GitHub o servicios:
# Crear archivo de configuración SSHvim ~/.ssh/configContenido:
# GitHub personalHost github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519
# GitHub trabajo (ejemplo)Host github-work HostName github.com User git IdentityFile ~/.ssh/id_ed25519_workLuego clonar con:
git clone git@github-work:empresa/repositorio.gitRenovar o Cambiar Clave
Section titled “Renovar o Cambiar Clave”Si necesitas cambiar la clave:
# Generar nueva clave con nombre diferentessh-keygen -t ed25519 -C "tu@email.com" -f ~/.ssh/id_ed25519_new
# Mostrar nueva clave públicacat ~/.ssh/id_ed25519_new.pub
# Añadir a GitHub (repetir paso 3)# Actualizar ~/.ssh/config si usas múltiples clavesSeguridad
Section titled “Seguridad”⚠️ Nunca compartas tu clave privada
Section titled “⚠️ Nunca compartas tu clave privada”# Clave PRIVADA - NO compartir~/.ssh/id_ed25519
# Clave PÚBLICA - OK compartir~/.ssh/id_ed25519.pubBackup de Claves (Opcional)
Section titled “Backup de Claves (Opcional)”# Hacer backup de claves en local (desde tu PC)scp root@tu-ip-vps:~/.ssh/id_ed25519 ~/backup-ssh/vps-keyscp root@tu-ip-vps:~/.ssh/id_ed25519.pub ~/backup-ssh/vps-key.pubGuarda estas claves en lugar seguro (gestor de contraseñas, disco cifrado).
Verificación Final
Section titled “Verificación Final”# Probar clonado de repositoriocd /rootgit clone git@github.com:daniclemente/vps-docs.gitcd vps-docs
# Verificarls -laSi todo funciona, ya puedes clonar cualquier repositorio de tu cuenta GitHub.
Siguiente Paso
Section titled “Siguiente Paso”Vuelve a la guía de despliegue correspondiente para continuar: