[Azure]Azure Linux虚拟机使用SSH Key登陆

来源:互联网 发布:淘宝上网上如何注册? 编辑:程序博客网 时间:2024/04/29 22:51


这里以CentOS 6.8为例,首先创建一个CentOS 6.8的虚拟机,先配置使用密码的方式进行登陆:

CREATE A VIRTUAL MACHINE Virtual machine configuration o VIRTUAL MACHINE NAME DanSSHDemo TIER BASIC STANDARD Al (1 core, 1.75 GB memory) NEW USER NAME daniel O AUTHENTICATION D UPLOAD COMPATIBLE SSH KEY FOR AUTHENTICATION g_] PROVIDE A PASSWORD NEW PASSWORD CONFIRM

 

创建成功后,需要首先使用工具创建一个“公钥/私钥”对用于认证,可以使用opensslputty或者xshell生成公钥和私钥,下面是使用xshell的步骤:

  1. 首先在Tools中找到New User Key Wizard...

:ree for Home/SchooI) View Tools Tab Window Help ) addres curr 'u ild 20 to Send Key Input To All Sessions Sun (Solaris) Shortcut Keys Host Key Manager... User Key Manager... New User Key Wizard... Launch Xagent Color Schemes... Quick Command Sets... Script Options...

  1. 这个步骤中尽量选择RSA加密算法(虽然DSA也可以用,但是Azure上建议使用RSA):

New User Key Wizard Key Generation Parameters Select key type and length to generate the public key. The key type determined by the version of SSH protocol. Only RSA algorithm can be used for SSHI protocol but OSA and RSA algorithms are available for SSH2 protocol. Key Type: RSA Longer key is more safe. The optimal éngth ofkey for most application is 1024 bits. Key Length: 2048 Click Next to create a public key. Next >

  1. key制定一个加密的passphrase

New User Key Wizard User Key Information Enter the name and passphrase of the created user Enter the name of the user key. Key Name: Enter a passphrase to encrypt the user key. Passphr ase: Con firma bon' (Retype passphrase) Click Next to register this public key at the SSH server. Next >

  1. 在最后一步完成前,可以将生成的公钥保存到文件(Save as a file...):

New User Key Wizard Register Public Key Register the public key to the SSH server. To use this user key, the public key part should be registered on the server Send the following public key to the administrator of SSH server or register it on the server per sonally Public Key Format: ssh-rsa ssH2 - openssH AAAAE3NzaC1yc2EAAAAE1nAAAQEA16yXWVlzKuckQAbj96fa2rhqMwuSFpEQeK +jLtAZAlapUpGvKxggRLIRFqzHYqDARqgryeXLelgY20CEsGqEjvadbPNDRyke3LrV 2UYXVxnskg0JprX1iRVQCyjD6DfuEK +uCMToTdhmftVcQPNzjWZ85HrM7xzWXRnxatbxE67M5sTCPN4udscuPjcmocn WOgZCH7r0X/E8a3uc35aoaTRHlrkmXkjcoH2PH 15Pz1rop02yusbF5bsisrszyDJmNbMQg4Q+F44Ne8tDcFccdeFi Save asa file...

  1. 完成创建后,可以在Tools -> User Key Manager…中查看刚刚创建好的私钥:

User Keys • , id rsa 204B Type Length 2048 bits Generate... Pr oper bes Imgu)rt... Export...

属性中可以查看其对应的公钥:

Properties General Public Key Public Key Format SSH2 - OpenSSH A.AAA83NzaC1yc2EAA.A.ABlwA.A.AQEA16yX',WzKuckGAbj9ffa2rhq MwuSFp8QeK SLtAZAapUpGvKxSgRLIRFqzHYqDARqgyaXLalSY20C8sGqavadb *-uCMT0TdhnfkVcQF-fvzjWZ85HrM7xz3UXRwxatbxE67M5sTCPN4u dsCuPjCmOCwWOgZCH7rOWE8a3uc35aOaTRHIkmkjCoH2PH *GOJYJCsuTGpr15Pz1rCp02yuSbF5bsisrSzyOJmNbMQg4Q Save as a

 

完成公钥和私钥创建后,登陆到虚拟机内(使用用户名和密码的方式),修改/etc/ssh/sshd_config,将下面三条配置取消注释:

#RSAAuthentication yes

#PubkeyAuthentication yes

#AuthorizedKeysFile     .ssh/authorized_keys

 

接着在/home/<username>/.ssh下面创建一个authorized_keys文件(<username>换成需要使用key进行登陆的用户名),如果没有这个目录,就创建一个,将之前生成的公钥的内容拷贝到其中:

:sh-rsa AAAAB3NzaC1yc2EAAAAB1wAAAQEA16ymnzKuckQAbj96fa2rhqwuSFpBQeK+jLtAZA1apUpGvKxgg mfkvcQPfvzjWZ85HrM7xz3uxmxatbxE67M5sTCPN4udscuPjanocwwogZCH7r€X/E8a3uc3i51aoaTRJ-n rkmxki COH2PH+GDJYjCsuTGp rszyDJmNbMQg4Q+F44Ne8tDcFCCdeFi+AgC45011hyp4jiWeT 6EP+hd9BsTKTGdYOq'ßZDMe3Fw= "authorized keys" IL, 381C

 

保存后,使用xshell登陆,首先输入用户名:

SSH User Name Remote Host: Server Type: 139.219.234.123:55547 (o,'odefal ssH2, openssH_5.3 Enter a user name to login: daniel

选择公钥登陆的方式,User Key选择我们之前创建的私钥,并填写创建时指定的passphrase

SSH User Authentication Remote Host: Server Type: 139.219.234.123:55547 (o,'odefaule ssH2, openssH_5.3 Select a proper user a uthentication method among the methods below and provide necessary information to login. C) Password Password: @Public Key user Key: Passphr ase: C) Keyboard Interactive use keyboard input For user authentication.

确定后即可登录成功。

 

对于同一个用户,可以为其配置多组公钥/私钥对儿,将所有的公钥都放在对应用户目录下的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的私钥登陆了。

1 0