这里以CentOS 6.8为例,首先创建一个CentOS 6.8的虚拟机,先配置使用密码的方式进行登陆:
创建成功后,需要首先使用工具创建一个“公钥/私钥”对用于认证,可以使用openssl,putty或者xshell生成公钥和私钥,下面是使用xshell的步骤:
- 首先在Tools中找到New User Key Wizard...:
- 这个步骤中尽量选择RSA加密算法(虽然DSA也可以用,但是Azure上建议使用RSA):
- 为key制定一个加密的passphrase:
- 在最后一步完成前,可以将生成的公钥保存到文件(Save as a file...):
- 完成创建后,可以在Tools -> User Key Manager…中查看刚刚创建好的私钥:
属性中可以查看其对应的公钥:
完成公钥和私钥创建后,登陆到虚拟机内(使用用户名和密码的方式),修改/etc/ssh/sshd_config,将下面三条配置取消注释:
#RSAAuthentication yes #PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys |
接着在/home/<username>/.ssh下面创建一个authorized_keys文件(<username>换成需要使用key进行登陆的用户名),如果没有这个目录,就创建一个,将之前生成的公钥的内容拷贝到其中:
保存后,使用xshell登陆,首先输入用户名:
选择公钥登陆的方式,User Key选择我们之前创建的私钥,并填写创建时指定的passphrase:
确定后即可登录成功。
对于同一个用户,可以为其配置多组公钥/私钥对儿,将所有的公钥都放在对应用户目录下的authorized_keys文件中即可,这样同一个用户可以用不同的私钥来登陆。
要为root用户配置公钥登陆方式,首先还是要修改/etc/ssh/sshd_config文件,将下面语句取消注释:
#PermitRootLogin yes
接着在/root/.ssh下面创建一个authorized_keys文件(如果目录不存在,则创建出来):
[root@DanCentOS65 ssh]# cd ~/.ssh/ [root@DanCentOS65 .ssh]# vim authorized_keys |
然后与前面的步骤一样,使用xshell生成一对儿“公钥/私钥”,将公钥拷贝到刚刚创建的authorized_keys文件中,然后就可以使用root的私钥登陆了。