# Generate SSH key pair
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-keygen -t ed25519 -C "your_email@example.com" # Modern, secure
# Copy public key to remote server
ssh-copy-id user@hostname
ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname
# Manual key installation
cat ~/.ssh/id_rsa.pub|ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
```_
### Gestion des clés
|Command|Description|
|---------|-------------|
|`ssh-keygen -t ed25519`|Generate Ed25519 key (recommended)|
|`ssh-keygen -t rsa -b 4096`|Generate 4096-bit RSA key|
|`ssh-keygen -f ~/.ssh/custom_key`|Generate key with custom name|
|`ssh-add ~/.ssh/private_key`|Add key to SSH agent|
|`ssh-add -l`|List loaded keys|
|`ssh-add -D`|Remove all keys from agent|
## Configuration
### Config client SSH (~/.ssh/config)
```bash
# Global defaults
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
TCPKeepAlive yes
# Specific host configuration
Host myserver
HostName server.example.com
User myusername
Port 2222
IdentityFile ~/.ssh/myserver_key
ForwardAgent yes
# Jump host configuration
Host target
HostName 192.168.1.100
User admin
ProxyJump jumphost
Host jumphost
HostName jump.example.com
User jumpuser
```_
### Options communes de configuration
|Option|Description|Example|
|--------|-------------|---------|
|`HostName`|Real hostname or IP|`HostName server.example.com`|
|`User`|Username for connection|`User admin`|
|`Port`|SSH port number|`Port 2222`|
|`IdentityFile`|Private key file|`IdentityFile ~/.ssh/id_rsa`|
|`ForwardAgent`|Enable agent forwarding|`ForwardAgent yes`|
|`Compression`|Enable compression|`Compression yes`|
## Transbordement et tunnel
### Transmission locale des ports
```bash
# Forward local port to remote service
ssh -L 8080:localhost:80 user@hostname
# Forward to different remote host
ssh -L 3306:database.internal:3306 user@gateway
# Multiple port forwards
ssh -L 8080:localhost:80 -L 3306:localhost:3306 user@hostname
Transfert de port à distance
# Forward remote port to local service
ssh -R 8080:localhost:3000 user@hostname
# Allow remote connections to forwarded port
ssh -R 0.0.0.0:8080:localhost:3000 user@hostname
Transmission dynamique des ports (SOCKS Proxy)
# Create SOCKS proxy on local port 1080
ssh -D 1080 user@hostname
# Use with applications
# Configure browser to use SOCKS proxy: localhost:1080
# Generate user certificate
ssh-keygen -s ca_key -I user_id -n username user_key.pub
# Use certificate for authentication
ssh -o CertificateFile=user_key-cert.pub user@hostname
Dépannage
Problèmes de connexion
# Debug connection problems
ssh -vvv user@hostname
# Test specific authentication method
ssh -o PreferredAuthentications=publickey user@hostname
# Check SSH service status
systemctl status ssh # Linux
service ssh status # Linux (older)