SSH — Secure Shell
SSH (Secure Shell) adalah protokol untuk mengakses komputer lain secara aman melalui jaringan. Ini adalah cara utama developer mengakses server dan mengelola infrastruktur.
Koneksi SSH Dasar
# Format: ssh user@hostname
ssh [email protected]
ssh [email protected]
# Dengan port tertentu (default: 22)
ssh -p 2222 [email protected]
# Setelah terhubung, kamu bisa menjalankan perintah
# seperti biasa — seolah-olah kamu di depan server
SSH Key (Password-less Login)
Menggunakan SSH key lebih aman dan praktis dari password:
# 1. Generate SSH key pair
ssh-keygen -t ed25519 -C "[email protected]"
# Tekan Enter untuk lokasi default (~/.ssh/id_ed25519)
# Masukkan passphrase (opsional tapi direkomendasikan)
# Hasil: 2 file
# ~/.ssh/id_ed25519 — Private key (JANGAN PERNAH dibagikan!)
# ~/.ssh/id_ed25519.pub — Public key (boleh dibagikan)
# 2. Copy public key ke server
ssh-copy-id [email protected]
# atau manual:
cat ~/.ssh/id_ed25519.pub | ssh [email protected] "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# 3. Sekarang bisa login tanpa password
ssh [email protected]
SSH Config File
Buat file ~/.ssh/config untuk shortcut:
# ~/.ssh/config
Host myserver
HostName 192.168.1.100
User budi
Port 22
IdentityFile ~/.ssh/id_ed25519
Host production
HostName prod.myapp.com
User deploy
Port 2222
Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Sekarang cukup ketik:
ssh myserver # instead of: ssh [email protected]
ssh production # instead of: ssh -p 2222 [email protected]
scp — Secure Copy
Transfer file antara lokal dan remote:
# Upload file ke server
scp app.js budi@server:/home/budi/project/
# Download file dari server
scp budi@server:/var/log/app.log ./
# Upload folder (recursive)
scp -r ./dist/ budi@server:/var/www/html/
# Menggunakan SSH config alias
scp app.js myserver:~/project/
rsync — Sinkronisasi Cerdas
Lebih baik dari scp untuk transfer besar karena hanya mengirim perubahan:
# Sync folder lokal ke server
rsync -avz ./dist/ budi@server:/var/www/html/
# Flags:
# -a (archive): preserve permissions, timestamps, dll
# -v (verbose): tampilkan progress
# -z (compress): kompres saat transfer
# Dry run (lihat apa yang akan terjadi tanpa menjalankan)
rsync -avzn ./dist/ budi@server:/var/www/html/
# Exclude file tertentu
rsync -avz --exclude="node_modules" --exclude=".env" ./ server:~/project/
SSH Tunnel (Port Forwarding)
Akses service di server melalui port lokal:
# Local forwarding: akses database server di localhost:5432
ssh -L 5432:localhost:5432 budi@server
# Sekarang kamu bisa connect ke database server
# menggunakan localhost:5432 di komputer lokal
# Akses web app server di localhost:8080
ssh -L 8080:localhost:3000 budi@server
# Buka http://localhost:8080 di browser lokal
Keamanan SSH
# Permission SSH key harus ketat!
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config
# Cek fingerprint server (pertama kali connect)
# "Are you sure you want to continue connecting?"
# → Jawab "yes" hanya jika yakin servernya benar
Tips keamanan:
- Selalu gunakan SSH key, hindari password
- Gunakan passphrase pada SSH key
- Jangan bagikan private key
- Disable password authentication di server production
- Gunakan port non-standar (bukan 22) untuk mengurangi brute force