ssh远程登录
ssh root@192.168.56.101
以上命令表示使用root用户登录主机192.168.56.101(也可以是主机名)
登录过程:
第一次登录时,回车后出现以下内容
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.
ECDSA key fingerprint is SHA256:IZH5yMt0HzjWTSND2zwLhQmTxIaILAddQc2hXhw6vK8.
Are you sure you want to continue connecting (yes/no)?
上面的大概意思为:无法确认主机的真实性,公钥指纹为SHA256:IZH5yMt0HzjWTSND2zwLhQmTxIaILAddQc2hXhw6vK8.
,确定继续吗
输入yes后提示要输入密码
root@192.168.56.101's password:
输入密码后就连接至远程主机了
原理为:
当使用ssh登录时,将会向远程主机发送请求,远程主机将自己的公钥发送至本地主机(保存在~/.ssh/known_hosts文件中),本地主机将密码用公钥加密后发送至远程主机,远程主机根据自己的私钥进行解密,如果密码正确,则登录
ssh免密登录
专业的叫法为 ssh公钥认证
首先用户需要有自己的私钥/公钥,然后将公钥保存在需要登录的远程主机的~/.ssh/authorized_keys文件中,即可实现免密登录
生成自己的私钥/公钥很简单,一条命令搞定:
ssh-keygen -t rsa
-t rsa 表示使用rsa算法
中间会有一些提示,一路回车即可
过程如下:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yangxk/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/yangxk/.ssh/id_rsa.
Your public key has been saved in /home/yangxk/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:X8wAd9ftrD4WHCujk0r2ViI4CdE2MjpgIi3N073eBTk yangxk@DESKTOP-HJ45PG9
The key's randomart image is:
+---[RSA 2048]----+
| + . o ... . ...|
|=.= = = Eo . . o|
|+o o = o o. o |
| o . . .+ . o|
| . o +S. +. + |
| = o...+ = |
| .o..= + . |
| o .= + |
| .o.. . . |
+----[SHA256]-----+
此时在~/.ssh目录下会生成两个文件id_rsa 和 id_rsa.pub
id_rsa为私钥,不能泄露
id_rsa.pub为公钥,需要保存至远程主机上
将公钥发送至远程主机上
ssh-copy-id root@192.168.56.101
在此过程中需要输入密码。
好了,现在就可以实现免密登录了