Ubuntu SSH命令行操作主要包括以下幾個方面:
連接到遠程服務器
ssh username@hostname
username 是你在遠程服務器上的用戶名。hostname 是遠程服務器的IP地址或域名。使用密鑰認證連接
ssh -i /path/to/private_key username@hostname
-i 參數指定私鑰文件路徑。斷開連接
exit 或按 Ctrl+D。使用SCP(Secure Copy Protocol)復制文件
scp localfile username@hostname:/path/to/destination
scp username@hostname:/path/to/source localfile
使用SFTP(SSH File Transfer Protocol)進行交互式文件傳輸
sftp username@hostname
get、put、ls、cd 等命令。直接在遠程服務器上執行命令
ssh username@hostname 'command'
在后臺運行命令
ssh username@hostname 'command &'
本地端口轉發
ssh -L local_port:destination_host:destination_port username@hostname
遠程端口轉發
ssh -R remote_port:destination_host:destination_port username@hostname
動態端口轉發(SOCKS代理)
ssh -D local_port username@hostname
編輯SSH配置文件
nano ~/.ssh/config
生成SSH密鑰對
ssh-keygen -t rsa -b 4096
~/.ssh/authorized_keys 文件中。修改SSH服務配置
/etc/ssh/sshd_config 文件,然后重啟SSH服務:sudo systemctl restart sshd
更改SSH默認端口
sshd_config 中修改 Port 參數。禁用root登錄
sshd_config 中設置 PermitRootLogin no。使用防火墻限制訪問
ufw 或其他防火墻工具配置規則。查看SSH連接日志
sudo tail -f /var/log/auth.log
使用 netstat 或 ss 查看SSH端口監聽狀態
sudo netstat -tulnp | grep sshd
# 或者
sudo ss -tulnp | grep sshd
列出遠程目錄內容
ssh username@hostname 'ls -l /path/to/directory'
創建遠程目錄
ssh username@hostname 'mkdir -p /path/to/new_directory'
刪除遠程文件或目錄
ssh username@hostname 'rm -rf /path/to/file_or_directory'
通過掌握這些基本的SSH命令行操作,你可以高效地在Ubuntu系統上進行遠程管理和文件傳輸。