ssh - 보안 셸 원격 접속
모든 플랫폼에서 안전한 원격 접속, 터널링, 시스템 관리를 위한 포괄적인 SSH 명령어.
기본 연결
간단한 연결
| 명령어 | 설명 |
|---|---|
ssh user@hostname | 원격 호스트에 연결 |
ssh user@192.168.1.100 | IP 주소를 사용하여 연결 |
ssh -p 2222 user@hostname | 사용자 지정 포트에 연결 |
ssh hostname | 현재 사용자 이름으로 연결 |
연결 옵션
| 명령어 | 설명 |
|---|---|
ssh -v user@hostname | 디버깅을 위한 상세 출력 |
ssh -vv user@hostname | 더 자세한 출력 |
ssh -vvv user@hostname | 최대 상세 정보 |
ssh -q user@hostname | 조용한 모드 (경고 표시 억제) |
인증 방법
비밀번호 인증
# Standard password login
ssh user@hostname
# Force password authentication
ssh -o PreferredAuthentications=password user@hostname
# Disable password authentication
ssh -o PasswordAuthentication=no user@hostname
키 기반 인증
# 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"
키 관리
| 명령어 | 설명 |
|---|---|
ssh-keygen -t ed25519 | Ed25519 키 생성 (권장) |
ssh-keygen -t rsa -b 4096 | 4096비트 RSA 키 생성 |
ssh-keygen -f ~/.ssh/custom_key | 사용자 지정 이름으로 키 생성 |
ssh-add ~/.ssh/private_key | SSH 에이전트에 키 추가 |
ssh-add -l | 로드된 키 목록 |
ssh-add -D | 모든 키를 agent에서 제거 |
구성
SSH 클라이언트 설정 (~/.ssh/config)
# 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
일반적인 구성 옵션
| 옵션 | 설명 | 예시 |
|---|---|---|
HostName | 실제 호스트 이름 또는 IP | HostName server.example.com |
User | 연결을 위한 사용자 이름 | User admin |
Port | SSH 포트 번호 | Port 2222 |
IdentityFile | 개인 키 파일 | IdentityFile ~/.ssh/id_rsa |
ForwardAgent | 에이전트 포워딩 활성화 | ForwardAgent yes |
Compression | 압축 활성화 | Compression yes |
포트 포워딩 및 터널링
로컬 포트 포워딩
# 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
원격 포트 포워딩
# 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
동적 포트 포워딩 (SOCKS 프록시)
# Create SOCKS proxy on local port 1080
ssh -D 1080 user@hostname
# Use with applications
# Configure browser to use SOCKS proxy: localhost:1080
X11 포워딩
# Enable X11 forwarding for GUI applications
ssh -X user@hostname
# Trusted X11 forwarding
ssh -Y user@hostname
# Run GUI application
ssh -X user@hostname firefox
파일 전송 통합
SCP 통합
# Copy file to remote host
scp file.txt user@hostname:/path/to/destination/
# Copy from remote host
scp user@hostname:/path/to/file.txt ./
# Recursive copy
scp -r directory/ user@hostname:/path/to/destination/
SFTP 통합
# Start SFTP session
sftp user@hostname
# SFTP with custom port
sftp -P 2222 user@hostname
고급 기능
점프 호스트 및 베스천 서버
# Connect through jump host
ssh -J jumphost user@target
# Multiple jump hosts
ssh -J jump1,jump2 user@target
# Using ProxyCommand
ssh -o ProxyCommand="ssh -W %h:%p jumphost" user@target
SSH 에이전트 및 키 관리
# Start SSH agent
eval $(ssh-agent)
# Add key to agent
ssh-add ~/.ssh/id_rsa
# Add key with timeout (1 hour)
ssh-add -t 3600 ~/.ssh/id_rsa
# List agent keys
ssh-add -l
# Remove specific key
ssh-add -d ~/.ssh/id_rsa
# Remove all keys
ssh-add -D
연결 멀티플렉싱
# Enable connection sharing in ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600
# Create socket directory
mkdir -p ~/.ssh/sockets
보안 및 강화
보안 연결 옵션
# Disable password authentication
ssh -o PasswordAuthentication=no user@hostname
# Use specific key only
ssh -o IdentitiesOnly=yes -i ~/.ssh/specific_key user@hostname
# Disable host key checking (development only)
ssh -o StrictHostKeyChecking=no user@hostname
# Use specific cipher
ssh -c aes256-ctr user@hostname
호스트 키 검증
# Check host key fingerprint
ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub
# Remove host key from known_hosts
ssh-keygen -R hostname
# Add host key manually
ssh-keyscan hostname >> ~/.ssh/known_hosts
인증서 기반 인증
Would you like me to continue with the remaining translations?```bash
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
```bash
# 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)
```### 연결 문제
| 문제 | 증상 | 솔루션 |
|---------|----------|----------|
| Permission denied | 인증 실패 | 키 권한 확인 (개인 키용 600) |
| Connection timeout | 응답 없음 | 방화벽 확인, 네트워크 연결성 점검 |
| Host key verification failed | 키 불일치 경고 | known_hosts 업데이트 또는 호스트 신원 확인 |
| Agent forwarding not working | 원격에서 키를 사용할 수 없음 | config에서 ForwardAgent 활성화 |### 일반적인 문제 및 해결책
```bash
# Fix SSH key permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/known_hosts
chmod 600 ~/.ssh/config
```### 키 권한 문제
```bash
# Run single command
ssh user@hostname "ls -la /var/log"
# Run multiple commands
ssh user@hostname "cd /var/log && tail -f syslog"
# Execute local script on remote host
ssh user@hostname 'bash -s' < local_script.sh
# Execute with sudo
ssh user@hostname "sudo systemctl restart nginx"
```## 자동화 및 스크립팅
```bash
#!/bin/bash
# Deploy to multiple servers
servers=("web1.example.com" "web2.example.com" "web3.example.com")
for server in "$\\\\{servers[@]\\\\}"; do
echo "Deploying to $server"
ssh user@$server "cd /var/www && git pull origin main"
ssh user@$server "sudo systemctl restart nginx"
done
```### 비대화형 SSH
```bash
#!/usr/bin/expect
spawn ssh user@hostname
expect "password:"
send "your_password\r"
interact
```### 배치 작업
```bash
# Enable compression
ssh -C user@hostname
# Disable compression for fast networks
ssh -o Compression=no user@hostname
# Use faster cipher for trusted networks
ssh -c arcfour user@hostname
```### Expect를 사용한 SSH (비밀번호 자동화)
```bash
# Keep connection alive
ssh -o ServerAliveInterval=60 user@hostname
# Persistent connection in background
ssh -f -N -L 8080:localhost:80 user@hostname
```## 성능 최적화
```powershell
# Windows OpenSSH client
ssh user@hostname
# Windows SSH config location
%USERPROFILE%\.ssh\config
# Start SSH agent on Windows
Start-Service ssh-agent
ssh-add ~/.ssh/id_rsa
```### 압축 및 속도
```bash
# Add key to macOS keychain
ssh-add --apple-use-keychain ~/.ssh/id_rsa
# Configure automatic keychain loading
Host *
AddKeysToAgent yes
UseKeychain yes
```### 연결 지속성