如何设置无密码SSH登录?

我希望能够通过ssh登录到远程,而无需一直输入密码。

  • 我该如何设置?
  • 执行无密码会话需要不同的命令吗?

回答

执行以下命令:

 ssh-keygen 

然后,您需要将新密钥复制到您的服务器 :

 ssh-copy-id user@host ## or if your server uses custom port no: ssh-copy-id "user@host -p 1234" 

复制密钥后,正常ssh进入机器:

 ssh user@host 

您现在可以在不输入执行命令的特定计算机的密码的情况下登录。

 not-marco@rinzwind-desktop:~$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/not-marco/.ssh/id_rsa): Created directory '/home/not-marco/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/not-marco/.ssh/id_rsa. Your public key has been saved in /home/not-marco/.ssh/id_rsa.pub. The key fingerprint is: b1:25:04:21:1a:38:73:38:3c:e9:e4:5b:81:e9:ac:0f not-marco@rinzwind-desktop The key's randomart image is: +--[ RSA 2048]----+ |.o= . oo. | |*B.+ . . | |*=o . o . | | = . = | |. o S | |E. | | o | | . | | | +-----------------+ not-marco@rinzwind-desktop:~$ ssh-copy-id not-marco@server not-marco@server's password: Now try logging into the machine, with "ssh 'not-marco@server'", and check in: ~/.ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. 

说明

这假设您已经可以通过SSH成功​​连接到服务器。

您需要生成一个SSH密钥对,这样您就可以在不使用密码的情况下将自己识别为自己。 如果您愿意,可以选择使用密码保护密钥,但这可以留空,允许完全无密码的SSH访问。

  1. 首先通过运行ssh-keygen创建SSH密钥对,这将创建一个id_rsaid_rsa.pub文件。 pub文件是服务器上的内容,私钥( id_rsa )是您的最佳选择,也是您识别自己的方式。
  2. 接下来,使用ssh-copy-id user@server将公钥复制到服务器, ssh-copy-id user@server替换为远程用户和服务器,并使用计算机DNS名称或IP地址。 它将提示您输入SSH密码,输入密码,如果一切顺利完成,您将能够通过ssh user@server访问该计算机而无需密码。

参考

输入以下命令:

  1. ssh-keygen

    Enter键直到出现提示

  2. ssh-copy-id -i root@ip_address

    (它会一次询问主机系统的密码)

  3. ssh root@ip_address

现在您应该可以在没有任何密码的情况下登录。

我通常这样做的方式如下:

ssh-keygen -t rsa

(提示输入密码时,请将其留空)

然后: cat ~/.ssh/id_rsa.pub | ssh username@hostname 'cat >> .ssh/authorized_keys' cat ~/.ssh/id_rsa.pub | ssh username@hostname 'cat >> .ssh/authorized_keys'

(这要求文件夹.ssh位于目标主机名的主目录中,其中包含authorized_keys文件)

当然,将username替换为所需的用户名,将hostname替换为所需的主机名或IP地址

在那之后,只需像你习惯的那样通过SSH连接到那个盒子。

我通常使用sshpass ,用sudo apt-get install sshpass安装它并像这样使用它

 sshpass -p 'password' ssh your_username@your_server 

此解决方案专门用于使用Windows进入其远程计算机的用户,包括AWS CloudGCE Cloud上的云图像

放弃

最近使用此解决方案在GCE上远程登录新部署的vm映像。


使用的工具:

  1. puttygen puttygen下载
  2. winscp winscp下载

执行步骤:

  1. 使用puttygen生成公钥/私钥对。
  2. 将公钥上传到云端或远程位置的服务器。

怎么做:

1.生成密钥/对或使用现有私钥

如果您拥有私钥:

打开puttygen ,按加载按钮,然后选择您的私钥( *.pem )文件。


如果您没有私钥:

  • 打开puttygen
  • 在“ 参数”部分中选择所需的密钥类型SSH2 DSA(您可以使用RSA或DSA)。 将密码字段留空是很重要的。
  • 生成并按照说明生成(公共/私人)密钥对。

样本密钥生成图

(来自源1,链接如下)

2.创建一个新的’authorized_keys’文件(带notepad

将您的公钥数据从PuTTY密钥生成器的“公钥粘贴到OpenSSH authorized_keys文件”部分复制,并将密钥数据粘贴到authorized_keys文件。


确保此文件中只有一行文本。


3.将密钥上传到Linux服务器

  • 打开WinSCP,
  • 选择SFTP文件协议并使用ssh凭据登录。
  • 成功时,您会在远程计算机上看到主目录结构。

将authorized_keys文件上传到远程计算机的主目录。


4.设置适当的权限

制作.ssh目录(如果不存在)


authorized_keys文件复制到.ssh目录。
(这将替换任何现有的authorized_keys文件,请注意这一点)。

如果文件存在,只需将此文件的内容添加到现有文件即可。


运行命令以设置权限:

 sudo chmod 700 .ssh && chmod 600 .ssh/authorized_keys 

现在,您可以在不输入凭据的情况下进入远程计算机。

进一步阅读:

  • 在Windows下生成和上载SSH密钥

  • OpsenSSH密钥证书认证PEM PUB CRT

禁用密码validation

由于许多使用SSH服务器的人使用弱密码,许多在线攻击者会寻找SSH服务器,然后开始随机猜测密码。 攻击者可以在一小时内尝试数千个密码,并且只要有足够的时间就可以猜出最强的密码。 建议的解决方案是使用SSH密钥而不是密码。 要像普通的SSH密钥一样难以猜测,密码必须包含634个随机字母和数字。 如果您始终能够使用SSH密钥登录计算机,则应完全禁用密码身份validation。

如果禁用密码validation,则只能从您经过特别批准的计算机进行连接。 这大大提高了您的安全性,但是如果您没有预先批准PC,或者在您意外删除密钥时从您自己的笔记本电脑连接到您自己的计算机,则无法从朋友的PC连接到您自己的计算机。

除非您有特殊原因,否则建议禁用密码validation。

要禁用密码身份validation,请在sshd_config文件中查找以下行:

 #PasswordAuthentication yes 

将其替换为如下所示的行:

 PasswordAuthentication no 

保存文件并重新启动SSH服务器后,登录时甚至不应要求输入密码。

https://help.ubuntu.com/community/SSH/OpenSSH/Configuring#disable-password-authentication

如果您创建公共/ pricate密钥对并使用我们新创建的公钥登录,则无需键入密码。 根据密钥环和/或ssh代理的配置,您可能需要使用密码来保护密钥。

这是为您准备的众多简短指南之一。 对于这种方法的安全性至关重要,生成的私钥仍然是私有的! 您永远不应与任何人共享或允许以任何身份访问它。

此命令在~/.ssh/生成一个相当强的键:

 ssh-keygen -b 4096 

~/.ssh/您会发现您的公钥为id_rsa.pub 。 它的内容应通过传输可传输介质(笔式驱动器)或在服务器上短暂启用密码validation,然后使用ssh-copy-id ~/.ssh/id_rsa.pub username@server和服务器附加到您的服务器authorized_keys文件中。然后再次禁用它。

如果您选择使用密码来保护密钥(在第一步中),则可以使用ssh-agent或Ubuntu密钥环在本地保护该分析,这样您就不必一直输入密码。

远程登录/复制而不提供密码

用于远程登录和远程复制的应用程序sshscp分别允许您在不提供密码的情况下与远程主机通信。 这要求您遵循如下所述的身份validation过程。 客户端我们指的是您所坐的机器,服务器是指您想要登录的机器而不提供密码。 身份validation过程的步骤如下:

  1. 运行ssh-keygen以生成私钥和公钥,除非您的计算机已经完成此操作。 它们存储在$HOME/.ssh中的文件中。
  2. 将公钥文件的内容附加到服务器上的$HOME/.ssh/authorized_keys$HOME/.ssh/authorized_keys2文件中。

有三种不同类型的身份validation协议。 您在运行ssh-keygen时指定类型:

  1. SSH协议版本1, RSA1:这是默认选择并导致文件标识(私钥,应采用chmod 0700以确保此文件对其他人不可读)和identity.pub (公钥)。
  2. SSH协议版本1, RSA :这是通过运行ssh-keygen -t rsa获得的,并导致文件id_rsa (私钥)和id_rsa.pub (公钥)
  3. SSH协议版本1, DSA :这是通过运行ssh-keygen -t dsa获得的,并导致文件id_dsa (私钥)和id_dsa.pub (公钥)

运行ssh-keygen时,您可以依赖默认答案(暗示您不提供密码)。 这使得整个设置简单,但也不安全。

您可以指定ssh选项使用的键类型; ssh -1强制使用RSA1密钥(协议版本1),而ssh -2强制ssh仅尝试RSADSA密钥(协议版本2)。 在下面的示例中,我们在远程主机上生成并安装RSA1DSA密钥,以便您具有更大的灵活性。 您可以使用该行在.ssh目录中创建配置文件

 Protocol 1,2 

这使得sshRSA / DSA (协议版本2)之前尝试RSA1 (协议版本1)连接。

使用RSA1密钥

 remote=user@remotehost # fill in correct user and remotehost names cd $HOME/.ssh # create .ssh on remote host if it is non-existing: ssh $remote 'if [ ! -d .ssh ]; then mkdir .ssh; fi' # copy RSA1 key: scp identity.pub ${remote}:.ssh ssh $remote "cd .ssh; cat identity.pub >> authorized_keys" 

使用DSA密钥

 remote=user@remotehost # fill in correct user and remotehost names cd $HOME/.ssh # create .ssh on remote host if it is non-existing: ssh $remote 'if [ ! -d .ssh ]; then mkdir .ssh; fi' # copy DSA key: scp id_dsa.pub ${remote}:.ssh ssh $remote "cd .ssh; cat id_dsa.pub >> authorized_keys2" 

如果您在生成密钥时未使用密码短语,则需要执行此操作。 您可以通过运行ssh $ remote来测试连接,看看您是否可以在不提供密码的情况下登录(您可能需要使用-1-2作为ssh的选项)。 当然,对于您要登录的任何机器,可以重复该过程。

如果您确实使用了密码,则必须运行程序ssh-agent来启动一个特殊的shell,然后使用ssh-add来注册密钥/密码sshd组合。 有关更多信息,请参见这些程序的手册页。

用于自动进行无密码连接的脚本: ssh-no-password.sh

 #!/bin/sh # create ssh connections without giving a password if [ $# -lt 1 ]; then echo Usage: $0 username@remotehost exit fi remote="$1" # 1st command-line argument is the user@remotehost address this=$HOST # name of client host # first check if we need to run ssh-keygen for generating # $HOME/.ssh with public and private keys: if [ ! -d $HOME/.ssh ]; then echo "just type RETURN for each question:" # no passphrase - unsecure!! # generate RSA1, RSA and DSA keys: echo; echo; echo ssh-keygen echo; echo; echo ssh-keygen -t rsa echo; echo; echo ssh-keygen -t dsa else # we have $HOME/.ssh, but check that we have all types of # keys (RSA1, RSA, DSA): if [ ! -f $HOME/.ssh/identity ]; then # generate RSA1 keys: echo "just type RETURN for each question:" # no passphrase - unsecure!! ssh-keygen fi if [ ! -f $HOME/.ssh/id_rsa ]; then # generate RSA keys: echo "just type RETURN for each question:" # no passphrase - unsecure!! ssh-keygen -t rsa fi if [ ! -f $HOME/.ssh/id_rsa ]; then # generate DSA keys: echo "just type RETURN for each question:" # no passphrase - unsecure!! ssh-keygen -t dsa fi fi cd $HOME/.ssh if [ ! -f config ]; then # make ssh try ssh -1 (RSA1 keys) first and then ssh -2 (DSA keys) echo "Protocol 1,2" > config fi # copy public keys (all three types) to the destination host: echo; echo; echo # create .ssh on remote host if it's not there: ssh $remote 'if [ ! -d .ssh ]; then mkdir .ssh; fi' # copy RSA1 key: scp identity.pub ${remote}:.ssh/${this}_rsa1.pub # copy RSA key: #scp id_rsa.pub ${remote}:.ssh/${this}_rsa.pub # copy DSA key: scp id_dsa.pub ${remote}:.ssh/${this}_dsa.pub # make authorized_keys(2) files on remote host: echo; echo; echo # this one copies all three keys: #ssh $remote "cd .ssh; touch authorized_keys authorized_keys2; cat ${this}_rsa1.pub >> authorized_keys; cat ${this}_rsa.pub >> authorized_keys2; cat ${this}_dsa.pub >> authorized_keys2;" # this one copies RSA1 and DSA keys: ssh $remote "cd .ssh; touch authorized_keys authorized_keys2; cat ${this}_rsa1.pub >> authorized_keys; cat ${this}_dsa.pub >> authorized_keys2;" echo; echo; echo echo "try an ssh $remote" 

复制自: http : //folk.uio.no/hpl/scripting/doc/ssh-no-password.html

做一些补充:

  • 默认情况下,Mac没有ssh-copy-id ,你必须自己安装:

     curl https://raw.github.com/beautifulcode/ssh-copy-id-for-OSX/master/ssh-copy-id.sh -o /usr/local/bin/ssh-copy-id chmod +x /usr/local/bin/ssh-copy-id 

在这里找到更多: https : //github.com/beautifulcode/ssh-copy-id-for-OSX

  • 如果你已经进行了端口转发,那么命令应该是这样的:

     ssh-copy-id "not-marco@127.0.0.1 -p 2222" 

请注意,报价是必要的。

我想为那些可能会发现他们应该输入密码的人添加一个答案,即使他们已经阅读了所有答案,因为您已将IdentitiesOnly设置为是。 这里的答案可以节省你很多时间来管理多个密钥,作为git或服务器的密钥。

生成密钥并将其复制到服务器后:

 ssh-keygen # change the file to /home/yourname/.ssh/something ssh-copy-id -i ~/.ssh/something.pub lerner@192.168.20.160 

我发现它不起作用。

然后我去检查客户端上的~/.ssh/config文件,我在底部看到了这个:

 Host * IdentitiesOnly yes 

然后我在上面添加:

 Host somename HostName 192.168.20.160 User lerner IdentityFile ~/.ssh/something 

我可以通过输入ssh somename

然后,您可以使用自己喜欢的名称添加多个ssh密钥,并且只需将上述四行的设置添加到配置文件中。

主机是您稍后连接服务器时要输入的名称; HostName是服务器的ip; 用户是您登录服务器的用户名; identityfile是存储您生成的密钥的文件。