CentOS系统中的SSH(Secure Shell)命令主要用于远程登录和管理服务器。以下是一些常用的SSH命令:
基本SSH连接
ssh username@hostname
或者
ssh -p port username@hostname
使用密钥认证
ssh -i /path/to/private_key username@hostname
保持连接活跃
ssh -o ServerAliveInterval=60 username@hostname
使用SSH代理
ssh-add /path/to/private_key
ssh username@hostname
通过跳板机连接
ssh -J jump_host username@target_host
SCP(Secure Copy Protocol)
scp local_file username@hostname:/remote/path
scp username@hostname:/remote/path/local_file
SFTP(SSH File Transfer Protocol)
sftp username@hostname
ls:列出目录内容cd:改变远程目录get:下载文件put:上传文件mkdir:创建目录rm:删除文件或目录exit:退出SFTP会话修改SSH配置文件
sudo vi /etc/ssh/sshd_config
修改配置后重启SSH服务:
sudo systemctl restart sshd
查看SSH服务状态
sudo systemctl status sshd
允许特定IP访问
编辑/etc/hosts.deny和/etc/hosts.allow文件,添加相应的规则。
生成SSH密钥对
ssh-keygen -t rsa -b 4096
查看SSH连接日志
sudo tail -f /var/log/secure
更改SSH默认端口
修改/etc/ssh/sshd_config中的Port参数,然后重启SSH服务。
禁用root登录
在/etc/ssh/sshd_config中设置PermitRootLogin no,然后重启SSH服务。
限制用户登录尝试次数
在/etc/ssh/sshd_config中设置MaxAuthTries参数。
使用防火墙限制SSH访问
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
查看SSH版本
ssh -V
测试SSH连接
ssh -o BatchMode=yes -o ConnectTimeout=5 username@hostname
使用SSH隧道
ssh -L local_port:destination_host:destination_port username@hostname
ssh -R remote_port:destination_host:destination_port username@hostname
这些命令涵盖了SSH在CentOS系统中的大部分常用操作。根据实际需求,可以灵活组合和使用这些命令。