SSH คืออะไร ใช้งานอย่างไร คู่มือ SSH สำหรับผู้เริ่มต้น VPS และ Linux Server
SSH guide for beginners: what is SSH, how to connect from Windows/Mac/Linux, essential commands, SSH key setup.
สารบัญ
SSH คืออะไร
SSH (Secure Shell) คือ Protocol สำหรับเชื่อมต่อและควบคุม Server จากระยะไกลผ่าน Command Line อย่างปลอดภัย ใช้ Encryption เข้ารหัส Traffic ทั้งหมด ต่างจาก Telnet ที่ส่งข้อมูลแบบ Plain Text SSH เป็น Default สำหรับ VPS และ Cloud Server Linux ทุกเจ้า
SSH (Secure Shell) is a protocol for securely connecting to and controlling servers remotely via command line. It encrypts all traffic — unlike Telnet which sends data in plain text. SSH is the default access method for VPS and Linux cloud servers.
- SSH Port: 22 (Default)
- Encryption: AES, ChaCha20 — ข้อมูลถูก Encrypt ทั้งหมด
- Authentication: Password หรือ SSH Key
- ใช้ได้กับ: VPS, Dedicated Server, Cloud
- Replace Telnet: Telnet ไม่ Encrypt — อันตราย
วิธีเชื่อมต่อ SSH
Command: ssh username@hostname หรือ ssh username@IP Port เป็น 22 by default ถ้าเปลี่ยน Port ใช้ -p เช่น ssh -p 2222 user@server ครั้งแรกจะถาม Fingerprint ให้พิมพ์ yes เพื่อ Trust เข้าสู่ Server
Command: ssh username@hostname or ssh username@IP — port 22 by default. If port changed, use -p: ssh -p 2222 user@server. First connection asks for fingerprint confirmation — type yes to trust.
- ssh [email protected] — เชื่อมต่อ Default
- ssh -p 2222 user@server — Custom Port
- ssh -i ~/.ssh/key.pem user@server — ใช้ Key File
- First Time: yes เพื่อ Trust Fingerprint
- บันทึก Alias ใน ~/.ssh/config
SSH บน Windows
Windows 10/11 มี Built-in SSH Client แล้ว เปิด Command Prompt หรือ PowerShell แล้วพิมพ์ ssh user@server หรือใช้ PuTTY (ฟรี) สำหรับ GUI ที่บันทึก Session ได้ง่าย Windows Terminal + OpenSSH Client แนะนำสำหรับ Developer
Windows 10/11 has a built-in SSH client. Open Command Prompt or PowerShell and type ssh user@server. Or use PuTTY (free) for GUI that easily saves sessions. Windows Terminal + OpenSSH Client recommended for developers.
- Built-in SSH: Windows 10/11 พิมพ์ ssh ใน CMD/PowerShell
- PuTTY: GUI ฟรี บันทึก Session ได้
- MobaXterm: All-in-One SSH + SFTP + Editor
- Windows Terminal: Modern Interface แนะนำ
- WSL: Run Ubuntu บน Windows สำหรับ Full Linux Experience
SSH บน Mac และ Linux
Mac และ Linux มี SSH Client ติดตั้งมาแล้ว เปิด Terminal แล้วพิมพ์ ssh user@server โดยตรง Mac Terminal, iTerm2, hoặือ Linux Gnome Terminal ทุกอันรองรับ SSH ไม่ต้องติดตั้งเพิ่ม
Mac and Linux have SSH client pre-installed. Open Terminal and type ssh user@server directly. Mac Terminal, iTerm2, or Linux Gnome Terminal all support SSH without any additional installation.
- Terminal (Mac): ใช้ ssh โดยตรง
- iTerm2 (Mac): Terminal ที่ดีกว่า Support Split Pane
- Linux Terminal: ทุก Distro มี ssh ติดตั้งแล้ว
- ~/.ssh/config: บันทึก Server หลายตัว Alias ได้
- ssh-keygen: สร้าง SSH Key ใน Terminal
คำสั่ง SSH และ Linux พื้นฐาน
เมื่อเข้า Server แล้ว ใช้ Linux Command ควบคุม Server ls (List Files) cd (Change Directory) mkdir (สร้าง Folder) rm (ลบ) cp (Copy) mv (Move) cat (อ่านไฟล์) nano/vim (แก้ไขไฟล์) chmod (เปลี่ยน Permission) chown (เปลี่ยนเจ้าของ)
Once connected, use Linux commands: ls (list files), cd (change directory), mkdir (create folder), rm (delete), cp (copy), mv (move), cat (read file), nano/vim (edit file), chmod (change permission), chown (change owner).
- ls -la — แสดงไฟล์ทั้งหมด + Permission
- cd /var/www/html — ไปยัง Web Root
- nano /etc/nginx/nginx.conf — แก้ไขไฟล์
- chmod 755 script.sh — เปลี่ยน Permission
- ps aux | grep apache — ดู Process ที่รัน
- df -h — ดูพื้นที่ Disk
SSH Key Authentication
SSH Key ปลอดภัยกว่า Password เพราะไม่มี Password ส่งผ่าน Network สร้าง Key: ssh-keygen -t ed25519 จะได้ ~/.ssh/id_ed25519 (Private Key) และ ~/.ssh/id_ed25519.pub (Public Key) Copy Public Key ไปวางบน Server: ssh-copy-id user@server
SSH Key is more secure than passwords because no password is transmitted over the network. Create key: ssh-keygen -t ed25519 gives ~/.ssh/id_ed25519 (private key) and ~/.ssh/id_ed25519.pub (public key). Copy public key to server: ssh-copy-id user@server.
- ssh-keygen -t ed25519 — สร้าง Key (แนะนำ Ed25519)
- Public Key: เก็บบน Server (authorized_keys)
- Private Key: เก็บในเครื่องของเราเท่านั้น ห้าม Share
- ssh-copy-id user@server — Copy Key ไป Server
- Passphrase: เพิ่มความปลอดภัย Key ด้วย Password
ความปลอดภัย SSH
Disable Root Login: PermitRootLogin no ใน /etc/ssh/sshd_config ใช้ User แยก แล้ว sudo แทน ใช้ SSH Key แทน Password ปิด Password Authentication: PasswordAuthentication no ถ้าใช้ Key แล้ว ติดตั้ง Fail2Ban Block IP ที่ Login ผิดซ้ำ
Disable root login: PermitRootLogin no in /etc/ssh/sshd_config. Use a separate user with sudo instead. Use SSH Key instead of password. Disable password authentication: PasswordAuthentication no if using keys. Install Fail2Ban to block IPs with repeated failed logins.
- PasswordAuthentication no: ปิด Password Login
- PermitRootLogin no: ห้าม Login ด้วย root โดยตรง
- Fail2Ban: Block IP ที่ Login ผิดซ้ำ
- MaxAuthTries 3: จำกัดความพยายาม Login
- AllowUsers username: จำกัดว่าใช้ Login ได้บ้าง
เปลี่ยน Port SSH
Default Port 22 ถูก Scan บ่อยโดย Bot ถ้าเปลี่ยนเป็น Port อื่น (เช่น 2222) จะลด Noise ใน Log แก้ /etc/ssh/sshd_config เปลี่ยน Port 22 เป็น Port ใหม่ ต้อง Restart sshd และเปิด Port ใหม่ใน Firewall ก่อนปิด Session เดิม
Default port 22 is frequently scanned by bots. Changing to another port (e.g., 2222) reduces log noise. Edit /etc/ssh/sshd_config, change Port 22 to new port. Must restart sshd and open new port in firewall before closing current session.
- /etc/ssh/sshd_config: แก้ Port 22 → Port ใหม่
- sudo systemctl restart sshd: Restart SSH Service
- ufw allow 2222/tcp: เปิด Port ใหม่ใน Firewall
- อย่าปิด Session เดิมก่อนทดสอบ Port ใหม่
- Port 22 ยังต้องเปิดไว้จนกว่าทดสอบสำเร็จ
SCP และ SFTP โอนไฟล์ผ่าน SSH
SCP (Secure Copy) ใช้โอนไฟล์ผ่าน SSH: scp file.txt user@server:/path/ SFTP คือ FTP บน SSH: sftp user@server FileZilla รองรับ SFTP Protocol สามารถใช้แทน FTP ได้ปลอดภัยกว่ามาก rsync ใช้ Sync ไฟล์ Directory ที่เปลี่ยนแปลงได้
SCP (Secure Copy) transfers files via SSH: scp file.txt user@server:/path/ SFTP is FTP over SSH: sftp user@server. FileZilla supports SFTP protocol — more secure than regular FTP. rsync syncs only changed files in a directory.
- scp file.txt user@server:/home/user/ — Upload ไฟล์
- scp user@server:/file.txt . — Download ไฟล์
- sftp user@server: แล้ว put/get
- rsync -avz ./folder/ user@server:/path/ — Sync
- FileZilla: Protocol SFTP Port 22