温馨提示×

SSH命令在CentOS上怎么用

小樊
41
2025-07-20 15:12:35
栏目: 智能运维

在CentOS系统上使用SSH(Secure Shell)命令可以让你安全地连接到远程服务器。以下是一些基本的SSH命令和用法:

1. 连接到远程服务器

ssh username@hostname_or_ip_address 
  • username 是你在远程服务器上的用户名。
  • hostname_or_ip_address 是远程服务器的主机名或IP地址。

例如:

ssh john@example.com 

2. 使用密钥认证

如果你已经生成了SSH密钥对(通常是id_rsaid_rsa.pub),可以使用密钥进行认证,而不需要每次都输入密码。

生成密钥对(如果还没有)

ssh-keygen -t rsa -b 4096 

按照提示操作,通常会生成~/.ssh/id_rsa(私钥)和~/.ssh/id_rsa.pub(公钥)。

将公钥复制到远程服务器

ssh-copy-id username@hostname_or_ip_address 

例如:

ssh-copy-id john@example.com 

3. 使用SSH配置文件

你可以创建一个~/.ssh/config文件来简化SSH连接。

Host example.com HostName example.com User john IdentityFile ~/.ssh/id_rsa 

然后你可以直接使用简化的命令连接:

ssh example.com 

4. 使用SSH隧道

SSH隧道可以用来加密和保护数据传输。

本地端口转发

ssh -L local_port:destination_host:destination_port username@hostname_or_ip_address 

例如:

ssh -L 8080:localhost:80 john@example.com 

这会将本地端口8080转发到远程服务器的localhost:80。

远程端口转发

ssh -R remote_port:destination_host:destination_port username@hostname_or_ip_address 

例如:

ssh -R 8080:localhost:80 john@example.com 

这会将远程服务器的端口8080转发到本地服务器的localhost:80。

5. 使用SSH代理

你可以使用ssh-agent来管理多个SSH密钥。

eval $(ssh-agent -s) ssh-add ~/.ssh/id_rsa 

6. 断开连接

在SSH会话中,你可以使用以下命令断开连接:

exit 

或者按Ctrl+D

7. 查看SSH连接日志

你可以查看SSH连接的详细日志:

ssh -vvv username@hostname_or_ip_address 

这些是SSH在CentOS上的一些基本用法。根据你的具体需求,可以进一步探索和使用更多的SSH功能和选项。

0