在CentOS上使用SSH(Secure Shell)主要分为以下几个步骤:
CentOS默认已经安装了OpenSSH服务器,但如果没有,可以使用以下命令安装:
sudo yum install openssh-server
安装完成后,启动SSH服务并设置开机自启:
sudo systemctl start sshd
sudo systemctl enable sshd
确保SSH服务正在运行:
sudo systemctl status sshd
确保防火墙允许SSH连接。CentOS 7及以上版本使用firewalld
:
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
对于CentOS 6或其他不使用firewalld
的系统,可以使用iptables
:
sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT
sudo service iptables save
从另一台计算机连接到CentOS服务器:
ssh username@server_ip_address
例如:
ssh user1@192.168.1.100
系统会提示你输入密码。
为了提高安全性,可以使用SSH密钥认证代替密码认证。
在本地计算机上生成SSH密钥对:
ssh-keygen -t rsa -b 4096
按照提示操作,通常会生成id_rsa
(私钥)和id_rsa.pub
(公钥)。
使用ssh-copy-id
命令将公钥复制到服务器:
ssh-copy-id username@server_ip_address
例如:
ssh-copy-id user1@192.168.1.100
系统会提示你输入服务器用户的密码。
现在可以使用SSH密钥登录服务器:
ssh username@server_ip_address
例如:
ssh user1@192.168.1.100
可以编辑/etc/ssh/sshd_config
文件来配置SSH服务器。例如,更改默认端口、禁用root登录等。
sudo vi /etc/ssh/sshd_config
修改配置后,重启SSH服务:
sudo systemctl restart sshd
为了进一步提高安全性,可以考虑以下措施:
sudo sed -i 's/^PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo vi /etc/hosts.deny
sshd: ALL
/etc/hosts.allow
中添加允许访问的IP:sshd: 192.168.1.0/24
通过以上步骤,你可以在CentOS上成功使用SSH进行远程连接和管理。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:怎样在CentOS上安装SSH