SSH服务远程登录Linux主机操作实践

2019-05-0811:46:48服务器及运维Comments3,082 views字数 3208阅读模式

SSH是一种能够以安全的方式提供远程登录的协议,也是目前远程管理Linux系统的首选方式。它是以密文传输来保证安全。一般情况下在装Linux系统的过程中是默认安装的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

想要使用ssh协议来远程管理Linux系统,需要部署sshd服务程序。sshd是基于ssh协议开发的一款远程管理服务程序,不仅使用快捷方便,而且提供两种安全验证的方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

- 基于口令的验证-用于账户和密码来验证登录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

- 基于密钥的验证-需要在本地生成密钥文件,然后把密钥对中的公钥文件上传至服务器,并与服务器中的公钥进行比较,一致则可远程登录。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

实验环境:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

一个RedHat7系统作为ssh服务器,当前用户是linuxmi,IP地址是。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

另一个Redhat7系统作为客户机,当前用户是linuxidc,IP地址是。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

两个主机确保能够互联互通。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

用户口令验证登录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

在Redhat7系统中已经默认安装并启动了sshd服务,接下来在客户机上使用ssh命令进行远程连接。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

[linuxidc@RedHat7-2 ~]$ ssh linuxmi@  //ssh远程登录linuxmi用户的服务器
The authenticity of host ' ()' 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 '' (ECDSA) to the list of known hosts.
linuxmi@'s password:  //输入目标服务器的linuxmi用户的密码
Last login: Wed May 23 03:36:52 2018
[linuxmi@RedHat7-1 ~]$ exit  //退出远程登录
登出
Connection to closed.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

安全密钥验证文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

第1步: 在客户端主机中生成密钥对。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

[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/.
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+.  .          |
+-----------------+文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

[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/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

新生成的密钥对文件,id_rsa是私钥文件,权限默认为600,是公钥文件,用来提供给SSH服务器。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

第2步: 修改服务器主机ssh配置文件,使其只允许密钥验证,指定公钥数据文件位置。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

以root管理员身份修改配置文件(/etc/ssh/sshd_config)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

vim /etc/ssh/sshd_config文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

PubkeyAuthentication yes //启用密钥对验证文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

AuthorizedKeysFile .ssh/authorized_keys //指定公钥库数据文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

第3步: 把客户端主机中生成的公钥文件传送至服务器主机。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

[linuxidc@RedHat7-2 ~]$  ssh-copy-id -i ~/.ssh/ linuxmi@ //上传公钥库文件至服务器
/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@'s password: //输入服务器linuxmi用户的密码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

Number of key(s) added: 1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

Now try logging into the machine, with:  "ssh 'linuxmi@'"
and check to make sure that only the key(s) you wanted were added.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

[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文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

[linuxmi@RedHat7-1 ~]$ ls -l ~/.ssh/authorized_keys  //查看公钥库文件
-rw-------. 1 linuxmi lisi 400 5月  23 04:07 /home/linuxmi/.ssh/authorized_keys文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

注意任何用户对公钥库文件不能有写入的权限。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

第4步: 重新启动sshd服务程序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

service sshd restart文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

第5步: 在客户端主机使用密钥对验证文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

[linuxidc@RedHat7-2 ~]$ ssh linuxmi@    //远程登录服务器
Enter passphrase for key '/home/linuxidc/.ssh/id_rsa':123123 //输入私钥短语,以便调用私钥文件进行匹配
Last login: Wed May 23 03:37:45 2018 from
[linuxmi@RedHat7-1 ~]$文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

所以使用密钥对验证的方式登录时,不需要知道目标服务器用户的密码,只需要验证客户端用户的私钥短语,检查公私钥文件是否匹配,这样更安全。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/11872.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/11872.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定