Linux ssh配置

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

1、建立在用户的home目录下建立  .ssh文件夹apache

mkdir .ssh安全

能够隐藏文件夹或文件内容服务器

ls -a ssh

2、 生成证书ide

证书分为:dsa和rsaoop

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

ssh-keygen 生成命令spa

-t 表示证书 rsa debug

-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

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

cat  id_rsa.pub  >>  authorized_keys

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

4、设置文件和目录权限

设置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 

6、重启sshd 服务 (须要使用root用户登陆)

service sshd restart

7、本地验证测试

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目录权限不正确,请从新执行第四步操做;

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

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

远程复制

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

远程复制

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

 

9、将验证远程免密码登陆:

ssh  10.196.20.194(远程IP)

总结:

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

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

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

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

相关文章
相关标签/搜索