CentOS配置:远程登陆【ssh】

12/31/2024 操作系统LinuxCentOS

目录


参考:

  • 链接1:

# CentOS配置:远程登陆【ssh】

# 安装SSH

查看SSH是否安装

rpm -qa | grep ssh
1

没有的话安装

yum install -y openssh-server
1

# 配置SSH

# 备份
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak20240725
ll -a /etc/ssh |grep sshd_config
# 编辑
vi /etc/ssh/sshd_config
1
2
3
4
5

修改以下配置

# 端口
Port 22
# 访问协议
AddressFamily any
# 访问地址
ListenAddress 0.0.0.0
ListenAddress ::
# 允许使用root登陆则设为yes,否则为no
PermitRootLogin yes
# 允许使用密码登陆则设为yes,否则为no
PasswordAuthentication yes
# 允许公钥登陆(用于免密登录)则设为yes,否则为no
PubkeyAuthentication yes
1
2
3
4
5
6
7
8
9
10
11
12
13

# 启动SSH

# 启动
sudo systemctl start sshd
# 重启
sudo systemctl restart sshd
# 重启
sudo systemctl status sshd
# 开机启动
sudo systemctl enable sshd
1
2
3
4
5
6
7
8

防火墙放行

# 确保防火墙允许SSH端口通行
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
# 列出已允许的服务
sudo firewall-cmd --list-services
1
2
3
4
5

参数解析:

  • --permanent:永久生效,需重载或重启生效,未加此参数仅临时生效。
  • --add-service=ssh:添加预定义的 SSH 服务规则。
  • --reload重新加载防火墙规则
上次更新时间: 3/21/2025, 6:24:14 AM