使用 Linux 来登录有两种方式:密码登录/口令登录,但是密钥对登录(最好)
现在已经安装好 LNMP + Wordpress,来操作一下 非对称密钥
1、拿 非对称加密公共实验平台地址:118.126.105.5,密码:Aa111111 这个主机做客户端,自己的云主机作为服务器来练习。使用 putty 成功登陆实验平台,使用命令 生成密钥对
ssh-keygen -t rsa

输入 y 后就可以再次输入密码,需要给个密码,一样是给 Aa111111

这时候就已经是生成了公钥的状态图,现在进去看一下
ls
cd .ssh
ls
已经生成了这几个文件

分别是 认证文件 私钥 公钥
2、上传公钥到服务器端上去
scp id_rsa.pub root@182.254.201.18:/root


回答 yes 后输入 云主机密码

已经拷贝过来了,我们再来登录 putty 的自己云主机上面

通过直接输入 ls 看到了刚刚考过来的公钥。在 云主机的 putty 上面要重定向到 ssh 文件上面,但是还没有 ssh 文件夹 ,所以想要进行新建文件夹 ssh,不过得先输入命令看看是否它已经建好了这个文件夹 ssh
cat id_rsa.pub >>/root/.ssh/authorized_keys
来看一下它是否自动生成,cat 是一种追加方式,如果执行就是是说明他已经建好了这个文件夹

命令是成功的,ls 进入文件夹查看

可以看到他已经把 公钥 自动生成了这个文件

并且看好文件夹的目录在哪里 /root/.ssh

做一下权限的修改,别的用户不能读,修改权限
chmod 600 /root/.ssh/authorized_keys

ll 查看

只有用户本身可以读可以写,其他的不行’,其他用户权限全部收回来了
3、修改配置文件
vim /etc/ssh/sshd.config
或者先进入 /etc/ssh 看看配置文件在哪里

找到 sshd_config 配置文件来修改它
vim sshd_config

进入后需要修改两个地方,点 i 进行编辑,密钥保存放的位置就不需要进行修改了
AuthorizedKeyFile .ssh/authorized_keys #公钥保存的位置

第二个是允许密码自动登录吗,也就是禁止使用密码验证登录,改成
PasswordAuthentication no
我们改为不允许,设置为锁定用户不能用密码登录,将后面的 yes 变成了 no

第三个是是否使用公钥验证

它被隐藏起来了,我们要去掉 # ,并且将它改为
PubkeyAuthenticaion yes

存盘退出

4、重启服务器
systemctl restart sshd

重启服务器成功
现在重新登进去云主机的服务器里面是没有办法登进去了吗,是因为他已经限制了不能够使用密码登录了,就是在上面我们修改的的密码禁止密码登录那里

现在要回到 实验平台的 putty 里面直接输入并且登录后面的云主机服务器是可以使用
Aa111111 密码登陆进去的,输入命令
ssh root@182.254.201.18

要求输入密码

成功进入

之前是13,现在显示是12,打印路径来看一下

cd .ssh
ls
进入显示的不是我们的客户端的服务器,而是云主机的服务器,所以是已经登陆在云主机的服务器上面去了
ip 是13 ,登陆过了之后就变成了 12 ,并且登陆了之后就变成本地端的事情了
通过了非对称密钥登录进去了,而不是通过密码登录的
这时候想以密码的方式来进行登陆的话那就不能够登录进去了,只能够通过公钥进行登录
使用window 来登录的话就是只能够使用xshell,你连接他的话就是必须要下载它的私钥保存在本地,你才能够进行登录
现在想要登录 118 这个服务器来将它的私钥给弄下来,先登录 118 的putty,这个是允许密码登录的
打开是可以看到有 公钥 和 私钥 ,所以我们必须要把私钥下载下来

先进行 copy 将私钥拷贝过来
cp id_rsa /home
先拷贝到 home 里面再来查看

已经将私钥复制到了 /home 目录下面了
现在使用一个传输软件把他传到本地,并且使用一个 windows 的方法来连接
这时就可以使用 FXP 来进行文件传输,其实是使用 SSH,但我现在还没有安装,可以去看看之前的教程是怎么样子弄的,用的就是 SSH 传输也就是所说的 windows 方法,应该就是图形界面的意思

登录的用户名一般都是最高权限 root 才对


找到 /home 文件夹,看到有了刚刚复制过来的 id_rsa文件,就将它复制到本地端,直接拖移动复制一份过来

现在要下载一个 xshell 文件来进行配置



菜单栏文件里面的新建

用户身份验证将密码登录变成了公钥登录,并且上传刚刚下载好的公钥文件

导入刚刚从客户端中下载下来的私钥


自己再次设置密码 00000000


密码错误原来不是自己设置

那应该就是客户端的密码 Aa111111


也不对那就是我自己的服务器的密码了


这个也是错的,3个密码都错了那该怎么办
最后还是成功了,输入的是客户端的 118 的密码 Aa111111

再次输入密钥非常重要,免得被别人登录浏览,选择点击确定

密钥已经有了就可以输入用户名,并点击连接来进行登录了

选择一次性接受,就只是会有一次,但是如果是选择和保存的话那就是永远都是可以使用的


现在是已经登录成功了

所以这个方法是使用 windows 方法的公钥来进行登录

但是打出了 ls 并不是在 /home的目录下面,cd /home 是报错的。我打印了当前路径竟然显示我的是在自己电脑的本地端上面的C盘文件夹里面,所以就很奇怪,于是我退出来重新进入后,就需要我先一次性登录再来进行密码登录,输入密码就显示连上了服务器云主机。所以说刚刚的连不上是因为它没有更新还是在本地端,因为下载软件的配置文件就是在本地端没有更新,打出来的命令也一样就是在本地端运行



这个样子才是登录成功了


我再来尝试了一遍,没有输入正确的用户名,所以密码也没有输入就退出来了。依然是显示182key,我 ls 看到了刚刚出现的问题,并且我打印当前路径就是显示 xshell 所安装的路径,就是说它如果连不上去的话就会跳转到本地端文件安装的路径位置
所以说我现在要好好保存密钥别轻易乱动密钥,位置的移动以后再说,就先暂时放在桌面的功课里面的 linux 文件夹里面先,因为如果将它放在第四周课件的话我怕密钥位置的改变也会影响我的 xshell 的登录,那么就又要重新来操作一遍
网友评论