centos

CentOS SSH安全设置指南

小樊
47
2025-03-19 11:39:27
栏目: 智能运维

CentOS SSH安全设置指南

简介

SSH(Secure Shell)是一种加密网络协议,用于在不安全的网络中安全地远程登录和管理服务器。通过正确配置和使用SSH,管理员可以远程管理服务器,传输文件,并确保通信的机密性。本文将深入探讨Linux中SSH的配置和使用,提供详实的示例代码,帮助管理员更全面地了解和运用SSH技术。

安装与基础配置

安装SSH服务

在CentOS系统上安装OpenSSH服务器:

sudo yum install openssh-server

安装完成后,启动SSH服务并设置开机自启动:

sudo systemctl start sshd
sudo systemctl enable sshd

服务器配置

编辑SSH配置文件 /etc/ssh/sshd_config

sudo nano /etc/ssh/sshd_config

重启SSH服务以应用更改:

sudo systemctl restart sshd

密钥认证与安全加固

生成密钥对

在本地计算机上生成SSH密钥对:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

按照提示操作,可以选择保存密钥的位置和输入一个可选的密码短语。

部署公钥到服务器

将公钥复制到远程服务器:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote_host

或者手动复制公钥到远程服务器的 ~/.ssh/authorized_keys 文件中:

cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

安全增强措施

sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
sudo yum install fail2ban
sudo systemctl start fail2ban
sudo systemctl enable fail2ban

客户端配置与实用命令

简化连接配置

编辑客户端的SSH配置文件 ~/.ssh/config

Host myserver
  HostName 192.168.1.100
  User user
  Port 2222
  IdentityFile ~/.ssh/id_rsa

连接时直接使用别名:

ssh myserver

文件传输

端口转发与隧道

通过上述步骤,可以显著提高CentOS系统的SSH安全性,减少受到攻击的风险。

0
看了该问题的人还看了