使用SSH服务远程登录Linux主机

SSH是一种能够以安全的方式提供远程登录的协议,也是目前远程管理Linux系统的首选方式。它是以密文传输来保证安全。一般情况下在装Linux系统的过程中是默认安装的。

想要使用ssh协议来远程管理Linux系统,需要部署sshd服务程序。sshd是基于ssh协议开发的一款远程管理服务程序,不仅使用快捷方便,而且提供两种安全验证的方法。

- 基于口令的验证-用于账户和密码来验证登录

- 基于密钥的验证-需要在本地生成密钥文件,然后把密钥对中的公钥文件上传至服务器,并与服务器中的公钥进行比较,一致则可远程登录。

实验环境:

一个RedHat7系统作为ssh服务器,当前用户是linuxmi,IP地址是192.168.182.188。

另一个Redhat7系统作为客户机,当前用户是linuxidc,IP地址是192.168.10.80。

两个主机确保能够互联互通。

用户口令验证登录

在Redhat7系统中已经默认安装并启动了sshd服务,接下来在客户机上使用ssh命令进行远程连接。

[linuxidc@RedHat7-2 ~]$ ssh linuxmi@192.168.182.188  //ssh远程登录linuxmi用户的服务器
The authenticity of host '192.168.182.188 (192.168.10.70)' can't be established.
ECDSA key fingerprint is 93:8e:e7:3f:9a:22:6f:66:3a:f7:57:68:a1:57:3b:09.
Are you sure you want to continue connecting (yes/no)? yes  //确认连接
Warning: Permanently added '192.168.182.188' (ECDSA) to the list of known hosts.
linuxmi@192.168.182.188's password:  //输入目标服务器的linuxmi用户的密码
Last login: Wed May 23 03:36:52 2018
[linuxmi@RedHat7-1 ~]$ exit  //退出远程登录
登出
Connection to 192.168.182.188 closed.

安全密钥验证

第1步: 在客户端主机中生成密钥对。

[linuxidc@RedHat7-2 ~]$ ssh-keygen -t rsa                //创建密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/home/linuxidc/.ssh/id_rsa): //按回车键默认指定私钥位置在宿主目录中的隐藏文件夹.ssh下
Enter passphrase (empty for no passphrase):  //设置私钥短语123123
Enter same passphrase again:      //确认所设置的短语
Your identification has been saved in /home/linuxidc/.ssh/id_rsa.
Your public key has been saved in /home/linuxidc/.ssh/id_rsa.pub.
The key fingerprint is:
d0:b6:b9:96:b2:00:b8:f3:ea:a5:61:96:64:bd:b0:54 linuxidc@RedHat7-2
The key's randomart image is:
+--[ RSA 2048]----+
|                |
|      .        |
|  E  . o        |
| .o    o o      |
|.=..    S        |
|+.+..    o      |
|o* o. . +        |
|oo+  . +        |
|o+.  .          |
+-----------------+

[linuxidc@RedHat7-2 ~]$ ls -lh ~/.ssh/id_rsa*  //确认生成的密钥文件
-rw-------. 1 linuxidc zhangsan 1.8K 5月  22 19:50 /home/linuxidc/.ssh/id_rsa
-rw-r--r--. 1 linuxidc zhangsan  400 5月  22 19:50 /home/linuxidc/.ssh/id_rsa.pub

新生成的密钥对文件,id_rsa是私钥文件,权限默认为600,id_rsa.pub是公钥文件,用来提供给SSH服务器。

第2步: 修改服务器主机ssh配置文件,使其只允许密钥验证,指定公钥数据文件位置。

以root管理员身份修改配置文件(/etc/ssh/sshd_config)

vim /etc/ssh/sshd_config


PubkeyAuthentication yes //启用密钥对验证

AuthorizedKeysFile .ssh/authorized_keys //指定公钥库数据文件

第3步: 把客户端主机中生成的公钥文件传送至服务器主机。

[linuxidc@RedHat7-2 ~]$  ssh-copy-id -i ~/.ssh/id_rsa.pub linuxmi@192.168.182.188 //上传公钥库文件至服务器
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
linuxmi@192.168.182.188's password: //输入服务器linuxmi用户的密码

Number of key(s) added: 1

Now try logging into the machine, with:  "ssh 'linuxmi@192.168.182.188'"
and check to make sure that only the key(s) you wanted were added.

[linuxmi@RedHat7-1 ~]$ tail -1 ~/.ssh/authorized_keys  //显示公钥库文件信息
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/p8OHTUBJMKqJbxxwUiNJvKVHv8KSMywr
tFB3BEsC02MyU29NKXkGUVM/lC++7b/bK1j/xVg6gJXqCHk2lNrMk/jHjvR6qR7aLYhzWlCa
oDW0/Df9V9nrJNIg82DbXHUziwe6WoR9l+pzzQqYyI1Yq0iPTD4VZM5T94wRMX4taSgO8EQ
umWEeGtoHX/vgklapyMaG3ncA4SBxC0G4JUHo3q2KAfJ4eECrZ9LBwVsPq+4exlzDSeXmGh
aZO+VGo6Kbp7Q6ReA5U1YUbfsa9nKyAexiKxyzaGMXzBEri/aXGUpDibBWzRT4JDocF7PV
wHr+sshYqt4ULdG0wj91SK+D
linuxidc@RedHat7-2

[linuxmi@RedHat7-1 ~]$ ls -l ~/.ssh/authorized_keys  //查看公钥库文件
-rw-------. 1 linuxmi lisi 400 5月  23 04:07 /home/linuxmi/.ssh/authorized_keys

注意任何用户对公钥库文件不能有写入的权限。

第4步: 重新启动sshd服务程序

service sshd restart

第5步: 在客户端主机使用密钥对验证

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/f21536f6a9a70476e903cca5ac12e518.html