LinuxMastery
•
•
By asdlkfj
LinuxMastery 17: 遠端連線 SSH 與 SCP
連接世界的橋樑
SSH (Secure Shell) 是 Linux 遠端管理的標準。它對所有傳輸進行加密。除了登入,它還能用來傳輸檔案 (scp)。
核心指令詳解
ssh
- 語法:
ssh user@host - 指定 Port:如果伺服器改了 Port (例如 2222),要加
-p:ssh -p 2222 [email protected] - 免密碼登入 (Key-based Auth):
- 在本機產生金鑰:
ssh-keygen -t rsa(一路按 Enter)。 - 將公鑰複製到伺服器:
ssh-copy-id [email protected]。 - 之後登入就不需要輸入密碼了,既安全又方便自動化。
- 在本機產生金鑰:
- 語法:
scp (secure copy)
- 用途:透過 SSH 傳輸檔案。
- 上傳 (本機 -> 遠端):
scp local_file.txt [email protected]:/home/hank/ - 下載 (遠端 -> 本機):
scp [email protected]:/var/log/syslog ./local_dir/ - 複製目錄:記得加
-r。
實戰心法
如果你管理多台伺服器,建議設定 ~/.ssh/config 檔案。例如:
Host myserver
HostName 192.168.1.10
User hank
Port 2222
IdentityFile ~/.ssh/id_rsa_custom
設定好後,你只需要打 ssh myserver 就能自動套用所有參數登入,省去記憶 IP 和 Port 的麻煩。