SSH Commands - Lệnh SSH Cơ Bản Nhất
12 PHÚT ĐỌCTMT ELECTRONIC TEAM
Tổng hợp các lệnh SSH quan trọng nhất: tạo key, kết nối, cấu hình bảo mật, đổi port, troubleshooting với ví dụ thực tế từ cơ bản đến nâng cao.

🔐 SSH Commands - Lệnh SSH Cơ Bản Nhất
Tổng hợp các lệnh SSH quan trọng nhất: tạo key, kết nối, cấu hình và troubleshooting.
🔑 Tạo SSH Keys
Generate SSH Key Pairs
# Tạo ED25519 key (recommended)
ssh-keygen -t ed25519 -C "your-email@example.com"
ssh-keygen -t ed25519 -C "multipass-tmt-prj-controller"
# Tạo RSA key (compatibility)
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
# Tạo key với tên file cụ thể
ssh-keygen -t ed25519 -f ~/.ssh/id_server -C "server-key"
# Tạo key không có passphrase (cho automation)
ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_autoXem Public Key
# Xem public key
cat $HOME/.ssh/id_ed25519.pub
cat $HOME/.ssh/id_rsa.pub
# Copy public key to clipboard (nếu có xclip)
cat $HOME/.ssh/id_ed25519.pub | xclip -selection clipboard
# List tất cả keys
ls -la $HOME/.ssh/🔌 SSH Connection
Kết Nối Cơ Bản
# Kết nối basic
ssh user@hostname
ssh user@192.168.1.100
ssh ubuntu@server.example.com
# Kết nối với port khác
ssh -p 2222 user@hostname
ssh user@hostname -p 2222
# Kết nối với specific key
ssh -i ~/.ssh/id_server user@hostname
ssh -i ~/.ssh/id_ed25519 ubuntu@192.168.1.100SSH với Verbose Mode (Debug)
# Debug level 1 (basic)
ssh -v user@hostname
# Debug level 2 (more info)
ssh -vv user@hostname
# Debug level 3 (maximum detail)
ssh -vvv user@hostname
# Debug với specific key
ssh -vvv -i ~/.ssh/id_server user@hostname📤 Copy SSH Keys
Copy Public Key to Server
# Copy key using ssh-copy-id (recommended)
ssh-copy-id user@hostname
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostname
# Copy key với port khác
ssh-copy-id -p 2222 user@hostname
# Manual copy (nếu không có ssh-copy-id)
cat ~/.ssh/id_ed25519.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# Copy multiple keys
ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostname⚙️ SSH Server Configuration
Enable SSH Service
# Install SSH server (Ubuntu/Debian)
sudo apt update
sudo apt install openssh-server
# Install SSH server (CentOS/RHEL)
sudo yum install openssh-server
sudo dnf install openssh-server
# Start và enable SSH service
sudo systemctl start ssh
sudo systemctl enable ssh
sudo systemctl start sshd # CentOS/RHEL
# Check SSH service status
sudo systemctl status ssh
sudo systemctl status sshdSSH Config File Location
# SSH server config
sudo nano /etc/ssh/sshd_config
# SSH client config (user)
nano ~/.ssh/config
# SSH client config (system-wide)
sudo nano /etc/ssh/ssh_config🔧 Đổi SSH Port
Thay Đổi Default Port (22)
# Edit SSH config
sudo nano /etc/ssh/sshd_config
# Tìm và sửa dòng:
# Port 22
# Thành:
Port 2222
# Restart SSH service
sudo systemctl restart ssh
sudo systemctl restart sshd # CentOS/RHEL
# Test port mới
ssh -p 2222 user@hostnameKiểm Tra Port SSH
# Xem port SSH đang listen
sudo ss -tlnp | grep ssh
sudo netstat -tlnp | grep ssh
sudo lsof -i :22
sudo lsof -i :2222
# Check từ remote
nmap -p 22,2222 hostname
telnet hostname 22🛡️ SSH Security Settings
Disable Password Authentication
# Check current setting
sudo grep -r "PasswordAuthentication" /etc/ssh/
sudo grep "PasswordAuthentication" /etc/ssh/sshd_config
# Edit config
sudo nano /etc/ssh/sshd_config
# Set to:
PasswordAuthentication no
PubkeyAuthentication yes
# Restart SSH
sudo systemctl restart ssh
sudo service ssh restartDisable Root Login
# Edit SSH config
sudo nano /etc/ssh/sshd_config
# Set to:
PermitRootLogin no
# Restart SSH
sudo systemctl restart sshChange User Password
# Change current user password
passwd
# Change specific user password (as root)
sudo passwd ubuntu
sudo passwd username
# Force password change on next login
sudo passwd -e username📁 SSH Config File
Client Config (~/.ssh/config)
# Tạo SSH client config
nano ~/.ssh/config
# Example config:
Host server1
HostName 192.168.1.100
User ubuntu
Port 2222
IdentityFile ~/.ssh/id_server
Host production
HostName prod.example.com
User deploy
IdentityFile ~/.ssh/id_production
IdentitiesOnly yes
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
# Set permissions
chmod 600 ~/.ssh/configSử Dụng Config
# Kết nối bằng alias
ssh server1
ssh production
# Thay vì phải gõ:
ssh -i ~/.ssh/id_server -p 2222 ubuntu@192.168.1.100📋 SSH File Transfer
SCP (Secure Copy)
# Copy file to server
scp file.txt user@hostname:/path/to/destination/
scp -P 2222 file.txt user@hostname:/home/user/
# Copy file from server
scp user@hostname:/path/to/file.txt ./
scp -P 2222 user@hostname:/home/user/file.txt ./
# Copy directory (recursive)
scp -r folder/ user@hostname:/path/to/destination/SFTP
# Start SFTP session
sftp user@hostname
sftp -P 2222 user@hostname
# SFTP commands:
# put file.txt # Upload file
# get file.txt # Download file
# ls # List remote files
# lls # List local files
# cd /path # Change remote directory
# lcd /path # Change local directory🔍 SSH Troubleshooting
Common Issues & Solutions
# Check SSH service is running
sudo systemctl status ssh
# Check SSH config syntax
sudo sshd -t
# View SSH logs
sudo journalctl -u ssh
sudo tail -f /var/log/auth.log
sudo tail -f /var/log/secure # CentOS/RHEL
# Test SSH connection with debug
ssh -vvv user@hostname
# Check firewall
sudo ufw status
sudo iptables -LPermission Issues
# Fix SSH directory permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_*
chmod 644 ~/.ssh/id_*.pub
chmod 600 ~/.ssh/config
# Fix ownership
chown $USER:$USER ~/.ssh
chown $USER:$USER ~/.ssh/*🌐 SSH Tunneling
Local Port Forwarding
# Forward local port to remote
ssh -L 8080:localhost:80 user@hostname
ssh -L 3306:localhost:3306 user@dbserver
# Access via: http://localhost:8080Remote Port Forwarding
# Forward remote port to local
ssh -R 8080:localhost:80 user@hostname
# Keep connection alive
ssh -R 8080:localhost:80 -N user@hostnameDynamic Port Forwarding (SOCKS Proxy)
# Create SOCKS proxy
ssh -D 1080 user@hostname
# Configure browser to use localhost:1080 as SOCKS proxy🔐 SSH Agent
Manage SSH Keys with Agent
# Start SSH agent
eval $(ssh-agent)
# Add key to agent
ssh-add ~/.ssh/id_ed25519
ssh-add ~/.ssh/id_rsa
# List loaded keys
ssh-add -l
# Remove all keys from agent
ssh-add -D
# Remove specific key
ssh-add -d ~/.ssh/id_rsa🛠️ Useful SSH Commands
SSH Key Management
# Check key fingerprint
ssh-keygen -lf ~/.ssh/id_ed25519.pub
ssh-keygen -lf ~/.ssh/id_rsa.pub
# Change key passphrase
ssh-keygen -p -f ~/.ssh/id_ed25519
# Remove host from known_hosts
ssh-keygen -R hostname
ssh-keygen -R 192.168.1.100SSH Connection Testing
# Test SSH without executing commands
ssh -T user@hostname
# Execute single command
ssh user@hostname "ls -la"
ssh user@hostname "df -h"
# Execute multiple commands
ssh user@hostname "cd /var/log && tail auth.log"🎯 Quick Reference
Essential Commands
# Generate key
ssh-keygen -t ed25519 -C "email@example.com"
# View public key
cat ~/.ssh/id_ed25519.pub
# Copy key to server
ssh-copy-id user@hostname
# Connect
ssh user@hostname
# Connect with debug
ssh -vvv user@hostname
# Connect with specific key
ssh -i ~/.ssh/key user@hostname
# Change SSH port (in /etc/ssh/sshd_config)
Port 2222
# Disable password auth (in /etc/ssh/sshd_config)
PasswordAuthentication no
# Restart SSH service
sudo systemctl restart sshCommon File Locations
~/.ssh/id_ed25519 # Private key
~/.ssh/id_ed25519.pub # Public key
~/.ssh/authorized_keys # Allowed public keys
~/.ssh/config # SSH client config
~/.ssh/known_hosts # Known server fingerprints
/etc/ssh/sshd_config # SSH server configSSH là công cụ quan trọng nhất để quản lý server Linux từ xa một cách bảo mật!


