欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSH 常见使用及问题

程序员文章站 2022-06-17 14:25:48
...

 

             关于ssh实现无密登录server,连接操作github之类的找google大叔即可;

 

<1> SSH私钥取消密码(cancel passphrase )

 

以前使用的ssh密钥设置时候是有密码的,这样每次同步时候还需要手动输入一次密码才可以。

Google后有人提示可以使用.bash_profile配合ssh-agent来实现每次自动加载密钥到内存中,省去输入密码的环节,但.bash_profile有各种弊端,有人专门写了一个叫做keychain的小软件来解决ssh-agent 的不足。

 

但实际使用的时候依然不是很顺手,就干脆取消了私钥中的密码:

1. 使用openssl命令去掉私钥的密码 openssl rsa -in ~/.ssh/id_rsa -out ~/.ssh/id_rsa_new

2. 备份旧私钥 mv ~/.ssh/id_rsa ~/.ssh/id_rsa.backup

3. 使用新私钥 mv ~/.ssh/id_rsa_new ~/.ssh/id_rsa

4. 设置权限 chmod 600 ~/.ssh/id_rsa

 

取消掉原私钥的密码,使用远程自动同步设置连接之类的操作就简单方便了;而且只要注意私钥不泄漏出去,多台服务器之间无密码登录很方便。

 

<2> SSH配置authorized_keys却仍然需要输密码

 

前阵子配置ssh的公钥到authorized_keys文件中,但是在ssh连接时仍然提示需要输入密码,后来发现是$HOME/.ssh/authorized_keys这个文件的权限问题引起的。其原因是,不能让所有者之外的用户对authorized_keys文件有写权限;否则,sshd将不允许使用该文件,因为它可能会被其他用户篡改。

 

-rw-rw-r--1 hadoop hadoop  395 Jan 1218:37 authorized_keys
-rw-------1 hadoop hadoop 1675 Jan 1218:36 id_rsa
-rw-r--r--1 hadoop hadoop  395 Jan 1218:36 id_rsa.pub

注意: authorized_keys文件的权限,这时该文件中的key是不起作用的。

 

用chmod修改authorized_keys文件的权限

chmod g-w authorized_keys

如果authorized_keys文件、$HOME/.ssh目录 或 $HOME目录让本用户之外的用户有写权限,那么sshd都会拒绝使用 ~/.ssh/authorized_keys 文件中的key来进行认证的。

“man sshd” 命令查看对authorized_keys文件的描述如下:

~/.ssh/authorized_keys

             Lists the public keys (DSA/ECDSA/RSA) that can be used for logging in as this user.  The format of this file is described above.  The content

             of the file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others.

             If this file, the ~/.ssh directory, or the user's home directory are writable by other users, then the file could be modified or replaced by

             unauthorized users.  In this case, sshd will not allow it to be used unless the StrictModes option has been set to “no”.

 

 

 

相关标签: linux ssh