无密码登录Linux服务器

1、使用windows上的SecureCRT生成密钥对。

Tools->Create Public Key..->RSA->Passphrase(最好输入,也可为空)->长度默认1024->选择 OpenSSH Key format->选择位置保存。

2、创建session,SSH2属性中的Authentication只选中PublicKey,单击Properites,选择Use Session public key setting和第一步生成的xxx.pub后确定。

3、将生成的xxx.pub上传到linux服务器上的~/.ssh目录下,如果没有则创建并将其属性改成700。

4、将xxx.pub放入~/.ssh/authorized_keys文件中,并将文件属性改成600

5、使用root身份修改/etc/ssh/sshd_config文件

PermitEmptyPasswords no
PasswordAuthentication no

RSAAuthentication yes

PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

6、重启sshd服务

sudo service sshd restart

7、使用securecrt连接linux服务器即可,第一次出现输入对话框,输入密码然后选择accept and save。

在linux上生成密钥对

1、ssh-keygen -t rsa 

根据提示输入名字和Passphrase

2、将生成的xxx.pub传输到要登录的服务器上,并追加到服务器用户~/.ssh/authorized_keys文件中,如果文件中已有内容要换行。

3、将生成的两个密钥文件放在~/.ssh目录下,输入下面命令进行登录:

ssh username@ip -i ~/.ssh/xxx(注意此处为私钥)

因为默认情况下ssh命令会使用~/.ssh/id_rsa作为私钥文件进行登录,如果需要连接多台服务器而又不希望每次使用ssh命令时指定私钥文件,

可以在ssh的客户端全局配置文件/etc/ssh/ssh_config(或本地配置文件~/.ssh/config, 如果该文件不存在则建立一份)

~/.ssh/config文件内容

Host 10.1.1.1
IdentityFile 私钥文件名
Port 端口号
User 服务器上的用户名

如果出现Bad owner or permissions on /home/username/.ssh/config

执行 chmod 600 config 

according to the manual of openssh 

~/.ssh/config
   This is the per-user configuration file. This file is used by the SSH client.Because of the potential for abuse,this file must have strict permissions:

 read/write for the user, and not accessible by others.

原文地址:https://www.cnblogs.com/zxpo/p/3862664.html