如何在linux中如何配置ssh免密码登录

如何在linux中如何配置ssh免密码登录,第1张

首先需要在服务器端设置/etc/ssh/sshd_config

# vim /etc/ssh/sshd_config

修改如下两行为yes。其实大多数情况下不用修改,默认就是yes。

RSAAuthentication yes

PubkeyAuthentication yes

(1) 如果客户机和服务器都是Linux机器,那么我们使用下面的方法:(后面第2节会提到怎么在Windows下使用Putty生成密钥对)

我们需要在客户端生成RSA密钥对。使用ssh-keygen命令:

# ssh-keygen -t rsa

参数t的意思是type,后面跟着加密类型,这里我们是rsa。

然后会提示你输入密钥保存完成文件名,这里我们需要使用默认的id_rsa,之后才能正常才能登录。如果你生成的密钥作为其他用处,那么可以命名为其他名称:

Generating public/private rsa key pair.

Enter file in which to save the key (/home/cake/.ssh/id_rsa):

之后会提示你输入一个passphrase,我们这里可以留空,这样我们登录的时候就不许输入密码。

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

然后会提示你密钥生成成功。这是你的私钥保存为~/.ssh/id_rsa,你的公钥是~/.ssh/id_rsa.pub

我们现在需要做的是,把id_rsa.pub的内容,添加的服务器端的~/.ssh/autherized_keys文件最后。

你可以把这个文件上传到服务器端,然后使用命令:

# cat id_rsa.pub >>~/.ssh/autherized_keys

到这里就完成了。

(2) 在Windows下使用Putty生成密钥对:

Putty的安装目录下有个puttygen.exe程序,我们运行这个程序。

之后点击Generate,开始生成密钥对。我们需要根据提示,在指定方框内随机滑动鼠标。这是为了根据鼠标轨迹,产生一些随机数据。

之后生成结束,我们点击Save Private Key将私钥存放在某个目录中。然后赋值最上面文本框中的全部内容,粘贴到Linux服务器端的autherized_key的最后。

我们现在可以关闭这个小程序。

现在打开Putty,在左边的选项中,选择Conneciton–SSH–Auth,在Private key file for authentication中,选择刚才保存的私钥路径就可以了。

到此位置,Putty也可以不用密码登录了。

有两种方法,可以用证书实现无密码登录,或者sshpass命令可以指定ssh登陆密码。

1、派出间谍

Step 1:执行ssh-keygen -t rsa,会在~/.ssh下生成两个文件,id_isa和id_isa.pub

On one of the local LINUX (not SGI) machines, generate an id_rsa and id_rsa.pub pair as follows (this machine will subsequently be referred to as your home machine):

ssh-keygen will create the files ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.

Step 2: Copy the contents of ~/.ssh/id_rsa.pub to ~/.ssh/authorized_keys on any and all machines to which you want to be able to ssh to without being prompted for a password

可以用下面的命令完成此步骤:

cat id_rsa.pub |ssh root@172.16.1.5 ‘cat ->>~/.ssh/authorized_keys’

2、用sshpass程序

安装:

tar zxf sshpass-1.05.tar.gz

cd sshpass-1.05

./configuremakemake install

which sshpass

/usr/local/bin/sshpass

实例:

sshpass -p “passwd” ssh root@ip


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8976578.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-23
下一篇 2023-04-23

发表评论

登录后才能评论

评论列表(0条)

保存