Linux ssh配置

A机器ssh登录B机器无需输入密码;当应用有服务器很多的时候输入密码很浪费时间 在Hadoop安装时要求免密码登录;

一、创建在用户的home目录下创建  .ssh文件夹

mkdir .ssh

可以隐藏文件夹或文件内容

ls -a 

二、 生成证书

证书分为:dsa和rsa

ssh-keygen -t rsa -P  '' -b 1024

ssh-keygen 生成命令

-t 表示证书 rsa 

-p 密码提示语 ''

-b 证书大小 为:1024

执行后 将会生成密钥文件和私钥文件

ll

-rwx------ 1 apch apache 883 May 20 15:13 id_rsa
-rwx------ 1 apch apache 224 May 20 15:13 id_rsa.pub

三、 把公钥信息写入 authorized_keys 文档中

cat  id_rsa.pub  >>  authorized_keys

(将生成的公钥文件写入 authorized_keys 文件)

四、设置文件和目录权限

设置authorized_keys权限
$ chmod 600 authorized_keys 
设置.ssh目录权限
$ chmod 700 -R .ssh

五 修改/etc/ssh/sshd_config  (需要使用root用户登录)

vi  /etc/ssh/sshd_config

Protocol 2 (仅使用SSH2) 
PermitRootLogin yes (允许root用户使用SSH登陆,根据登录账户设置) 

ServerKeyBits 1024 (将serverkey的强度改为1024) 

PasswordAuthentication no (不允许使用密码方式登陆)

 PermitEmptyPasswords no   (禁止空密码进行登陆) 

RSAAuthentication yes  (启用 RSA 认证) 

PubkeyAuthentication yes (启用公钥认证)

AuthorizedKeysFile   .ssh/authorized_keys 

六、重启sshd 服务 (需要使用root用户登录)

service sshd restart

七、本地验证测试

ssh -v  localhost (开启登录调试模式)

如果出现输入密码说明没有成功

debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/hadoop/.ssh/identity
debug1: Offering public key: /home/hadoop/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 149
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: /home/hadoop/.ssh/id_dsa
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password
hadoop@localhost's password:

错误日志查看

用root用户登陆查看系统的日志文件

tail  -50f  /var/log/secure

May 20 16:35:37 JTMCRM195 sshd[7838]: Authentication refused: bad ownership or modes for directory  /home/hadoop
May 20 16:35:37 JTMCRM195 sshd[7838]: Authentication refused: bad ownership or modes for directory  /home/hadoop
May 20 16:36:05 JTMCRM195 sshd[7839]: Connection closed by 127.0.0.1
May 20 16:36:12 JTMCRM195 sshd[7848]: Authentication refused: bad ownership or modes for directory /home/hadoop
May 20 16:36:12 JTMCRM195 sshd[7848]: Authentication refused: bad ownership or modes for directory /home/hadoop

从日志上应该.ssh目录权限不正确,请重新执行第四步操作;

八、将 id_rsa、 id_rsa.pub复制到其它应用服务器上:

scp   id_rsa  hadoop@IP:/home/hadoop/.ssh  

远程复制

scp   id_rsa.pub  hadoop@IP:/home/hadoop/.ssh 

远程复制

登录到应用服务器(IP),再执行第三步到第七步;

九、将验证远程免密码登录:

ssh  10.196.20.194(远程IP)

总结:

1、文件和目录的权限千万别设置成chmod 777,这样权限太大了,存在安全问题;

2、生成的rsa/dsa签名的公钥是给对方机器使用的。

3、linux之间的访问直接 ssh 机器ip

4、配置出错情况:权限或/etc/ssh/sshd_config设置不正确

 

原文地址:https://www.cnblogs.com/scwanglijun/p/3739361.html