在Ubuntu系統中,使用SSH(Secure Shell)可以通過命令行工具進行遠程連接和管理。以下是一些常用的SSH命令行操作:
ssh username@hostname_or_ip_address
username
:遠程服務器的用戶名。hostname_or_ip_address
:遠程服務器的主機名或IP地址。例如:
ssh user@example.com
如果你已經生成了SSH密鑰對,并且將公鑰添加到了遠程服務器的~/.ssh/authorized_keys
文件中,可以使用密鑰進行認證:
ssh -i /path/to/private_key username@hostname_or_ip_address
-i
:指定私鑰文件的路徑。例如:
ssh -i ~/.ssh/id_rsa user@example.com
如果你需要在本地機器上使用SSH代理(如ssh-agent
),可以啟用代理轉發:
ssh -A username@hostname_or_ip_address
-A
:啟用代理轉發。你可以在~/.ssh/config
文件中配置多個SSH主機,以便更方便地管理連接:
Host example.com
HostName example.com
User user
IdentityFile ~/.ssh/id_rsa
然后可以直接使用主機名連接:
ssh example.com
你可以創建一個SSH隧道來轉發本地端口到遠程服務器的端口:
ssh -L local_port:remote_host:remote_port username@hostname_or_ip_address
-L
:本地端口轉發。例如:
ssh -L 8080:localhost:80 user@example.com
這會將本地的8080端口轉發到遠程服務器的80端口。
你可以在連接時直接執行遠程命令:
ssh username@hostname_or_ip_address 'command'
例如:
ssh user@example.com 'ls -l'
在SSH會話中,你可以使用以下命令斷開連接:
exit
或者按Ctrl+D
。
你可以使用以下命令查看當前的SSH連接信息:
who
或者查看詳細的SSH連接日志:
journalctl -u sshd
通過這些命令,你可以在Ubuntu系統中方便地使用SSH進行遠程連接和管理。