centos怎么设置远程ssh登录

简介

SSH(Secure Shell)是一种网络协议,用于在不安全的网络环境中提供安全的远程登录服务,CentOS 系统默认支持 SSH 服务,用户可以通过 SSH 客户端进行远程登录,本文将详细介绍如何在 CentOS 系统中设置远程 SSH 登录。

安装 OpenSSH 服务器

1、确保系统已经更新到最新版本,执行以下命令:

centos怎么设置远程ssh登录

sudo yum update

2、安装 OpenSSH 服务器软件包,执行以下命令:

sudo yum install openssh-server

3、启动 OpenSSH 服务并设置开机自启,执行以下命令:

sudo systemctl start sshd
sudo systemctl enable sshd

4、查看 OpenSSH 服务状态,执行以下命令:

sudo systemctl status sshd

配置防火墙

为了允许外部设备通过 SSH 连接到 CentOS 系统,需要开放 SSH 端口,执行以下命令:

centos怎么设置远程ssh登录

sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

生成 SSH 密钥对

1、在本地计算机上生成 SSH 密钥对,如果已经有密钥对,可以跳过此步骤,执行以下命令:

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

按提示操作,可以选择默认路径和文件名,也可以自定义,这将在指定路径下生成一个私钥文件(默认为 ~/.ssh/id_rsa)和一个公钥文件(默认为 ~/.ssh/id_rsa.pub)。

2、将公钥文件内容添加到远程服务器的 ~/.ssh/authorized_keys 文件中,可以使用以下命令将公钥复制到远程服务器:

ssh-copy-id user@remote_host

user 是远程服务器上的用户名,remote_host 是远程服务器的 IP 地址或域名,输入密码后,公钥将被自动添加到远程服务器的 ~/.ssh/authorized_keys 文件中,如果没有安装 ssh-copy-id,可以手动将公钥内容追加到远程服务器的 ~/.ssh/authorized_keys 文件中。

centos怎么设置远程ssh登录

测试远程 SSH 登录

1、从本地计算机尝试使用 SSH 客户端登录远程服务器,执行以下命令:

ssh user@remote_host

2、如果一切正常,将会看到类似以下的输出信息:

Last login: Mon Jan _2 15:30:00 UTC 2022 from your_local_ip_address on remote_host by user@PC name with Linux (x86_64) using X11 forwarding; type 'help' or 'h' for help. user@remote_host logged in on remote host. Welcome! You have successfully authenticated to the remote system. If this is the first time you log in, please run "passwd" and follow the instructions to set your password. Otherwise, you can continue using your existing password. If you forget your password, use "passwd" to change it. Bye! user@remote_host has finished logging in at Mon Jan _2 15:30:00 UTC 2022. user has been granted access to your_remote_home directory. Type "exit" when you're done. user has finished logging out of your_remote_session at Mon Jan _2 15:30:00 UTC 2022. exit|logout|quit|sync|bye|exit|logoff|signout are valid commands for quitting a shell session. Type "help" for more information about these commands or type "h" for help with a specific command. Please enter your command now. [root@remote_host ~]

至此,已经成功设置了远程 SSH 登录,如有其他问题,请参考下面的相关问题与解答栏目。

原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/120348.html

(0)
酷盾叔订阅
上一篇 2023-12-28 07:56
下一篇 2023-12-28 07:59

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入