すべてのプラットフォームにおけるセキュアな遠隔アクセス、トンネリング、システム管理のための包括的な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 | エージェントからすべてのキーを削除 |
設定
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 を有効にする |
一般的な問題と解決策
# 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
キーのアクセス許可の問題
# 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"
自動化とスクリプティング
非対話型SSH
#!/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
バッチ操作
#!/usr/bin/expect
spawn ssh user@hostname
expect "password:"
send "your_password\r"
interact
SSHとExpect(パスワード自動化)
# 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
パフォーマンスの最適化
圧縮と速度
# Keep connection alive
ssh -o ServerAliveInterval=60 user@hostname
# Persistent connection in background
ssh -f -N -L 8080:localhost:80 user@hostname
接続の持続性
# 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
プラットフォーム固有の考慮事項
Windows (OpenSSH)
# Add key to macOS keychain
ssh-add --apple-use-keychain ~/.ssh/id_rsa
# Configure automatic keychain loading
Host *
AddKeysToAgent yes
UseKeychain yes
macOSキーチェーン統合