CentOS(Community Enterprise Operating System)是一個基于Red Hat Enterprise Linux(RHEL)源代碼的開源Linux發行版。在CentOS系統中,SSH(Secure Shell)是一種加密的網絡傳輸協議,用于在不安全的網絡上安全地訪問和管理遠程計算機。
以下是一些CentOS SSH命令行操作的基礎知識:
使用ssh
命令連接到遠程服務器:
ssh username@hostname_or_ip_address
例如:
ssh user@example.com
為了提高安全性,可以使用SSH密鑰對進行認證。首先生成密鑰對:
ssh-keygen -t rsa -b 4096
然后將公鑰復制到遠程服務器的~/.ssh/authorized_keys
文件中:
ssh-copy-id username@hostname_or_ip_address
可以在~/.ssh/config
文件中配置常用的SSH連接參數,以便更方便地管理多個服務器:
Host example.com
HostName example.com
User username
Port 22
IdentityFile ~/.ssh/id_rsa
之后可以直接使用簡化的命令連接:
ssh example.com
SSH隧道可以將本地端口轉發到遠程服務器上的端口,或者將遠程端口轉發到本地機器上:
ssh -L local_port:remote_host:remote_port username@hostname_or_ip_address
ssh -R remote_port:local_host:local_port username@hostname_or_ip_address
SSH代理(如ssh-agent
)可以管理私鑰,避免每次連接時都需要輸入密碼:
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa
可以使用journalctl
命令查看SSH服務的日志:
journalctl -u sshd
可以編輯/etc/ssh/sshd_config
文件來修改SSH服務器的配置,例如更改默認端口、禁用密碼登錄等:
sudo vi /etc/ssh/sshd_config
修改完成后,重啟SSH服務以應用更改:
sudo systemctl restart sshd
可以在SSH連接中直接執行遠程命令:
ssh username@hostname_or_ip_address 'command_to_run'
可以使用scp
命令在本地和遠程服務器之間復制文件:
scp local_file username@hostname_or_ip_address:/path/to/remote_directory
scp username@hostname_or_ip_address:/path/to/remote_file local_directory
可以使用分號(;
)或雙與號(&&
)在SSH連接中執行多個命令:
ssh username@hostname_or_ip_address 'command1; command2'
或
ssh username@hostname_or_ip_address 'command1 && command2'
這些是CentOS SSH命令行操作的一些基礎知識。根據具體需求,可以進一步學習和探索更多高級功能。