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命令行操作的一些基础知识。根据具体需求,可以进一步学习和探索更多高级功能。