解决linux配置ssh免密登录遇到的异常:-bash: cd: /root/.ssh: Not a directory

解决异常:-bash: cd: /root/.ssh: Not a directory

事件场景:搭建了一个kafka集群,现在做脚本启动集群,发现需要做免密登录,在正常的免密设置中一台机遇到了该问题,花了不少时间解决所以记录一下

一:免密登录的设置步骤
原理:执行ssh-keygen生成秘钥 id_rsa.pub,将产生的id_rsa.pub放到authorized_keys文件,authorized_keys文件依次在集群中添加公钥id_rsa.pub,再覆盖之前集群里的authorized_keys,现在每个集群里的authorized_keys都有每个集群的公钥,免密登录完成

1:执行 
ssh-keygen
2追加公钥
cat id_rsa.pub >> authorized_keys
3:传递 authorized_keys(xxx是用户,安装在root用户的就用root)(aaa:你的映射名称,可以在hosts文件里看)(~/.ssh:指定传送到目标及机器的某个文件夹下,这里是.ssh文件夹)
scp authorized_keys xxx@aaa:~/.ssh/
4:按照上面步骤依次执行,再把最终的authorized_keys放到所有集群机器上

二:我遇到的问题:-bash: cd: /root/.ssh: Not a directory
我的某台机器执行ssh-keygen后,cd ~/.ssh 是没有这个文件夹的,网上说 ssh localhost 就可以生成这个文件夹,我试了但是没有生效,我在网上找了很久方法没有生效,最终在一个linux群里求问才解决该问题,以下是探索过程
1:cd ~/.ssh

[root@linux3 ~]# cd ~/.ssh
-bash: cd: /root/.ssh: Not a directory

2:ssh-keygen

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Saving key "/root/.ssh/id_rsa" failed: Not a directory

3:可以看到.ssh提示不是一个文件,查看文件ls -la

total 208528
dr-xr-x---. 21 root root      4096 Oct  5 06:52 .
dr-xr-xr-x. 18 root root       242 Nov  6  2019 ..
-rw-------.  1 root root      2766 Nov  6  2019 anaconda-ks.cfg
drwxr-xr-x.  3 root root        21 Nov 16  2019 apps
-rw-------.  1 root root     15701 Oct  5 06:19 .bash_history
-rw-r--r--.  1 root root        18 Dec 28  2013 .bash_logout
-rw-r--r--.  1 root root       176 Dec 28  2013 .bash_profile
-rw-r--r--.  1 root root       176 Dec 28  2013 .bashrc
drwx------. 14 root root      4096 Jul 14 00:43 .cache
drwx------. 14 root root       285 Dec  4  2019 .config
-rw-r--r--.  1 root root       100 Dec 28  2013 .cshrc
drwx------.  3 root root        25 Nov  6  2019 .dbus
drwxr-xr-x.  2 root root         6 Dec  4  2019 Desktop
drwxr-xr-x.  2 root root         6 Dec  4  2019 Documents
drwxr-xr-x.  2 root root         6 Dec  4  2019 Downloads
-rw-------.  1 root root        16 Dec  4  2019 .esd_auth
-rw-------.  1 root root       310 Dec  4  2019 .ICEauthority
-rw-r--r--.  1 root root      2770 Nov  6  2019 initial-setup-ks.cfg
drwxr-xr-x.  3 root root        63 Jan  1  2020 .ivy2
-rw-r--r--.  1 root root 181389058 Nov 13  2019 jdk-8u92-linux-x64.tar.gz
drwx------.  3 root root        19 Nov  6  2019 .local
drwxr-xr-x.  2 root root         6 Dec  4  2019 Music
-rw-r--r--.  1 root root      9116 Apr 11  2016 mysql57-community-release-el7-8.noarch.rpm
-rw-------.  1 root root       549 Oct  5 03:38 .mysql_history
-rw-r--r--.  1 root root   3444125 Oct  5 03:44 nohup.out
drwxr-xr-x.  2 root root        40 Nov 13  2019 .oracle_jre_usage
-rw-------.  1 root root      2046 Nov  6  2019 original-ks.cfg
drwxr-xr-x.  2 root root         6 Dec  4  2019 Pictures
drwxr-----.  3 root root        19 Jan  1  2020 .pki
drwxr-xr-x.  2 root root         6 Dec  4  2019 Public
drwxr-xr-x.  2 root root        26 Jan  1  2020 sbt
drwxr-xr-x.  6 root root        58 Jul 14 00:43 .sbt
-rwx------.  1 root root       393 Oct  5 04:07 .ssh

仔细观察.ssh 和.sbt的区别,sbt实测是可以打开的文件夹,发现在头部的代码有区别即drwxr-xr-x.和-rwx------.
经过查询了解:
d:第一位表示文件类型。d是目录文件,l是链接文件,-是普通文件,p是管道

rwx:第2-4位表示这个文件的属主拥有的权限,r是读,w是写,x是执行。

r-x:第5-7位表示和这个文件属主所在同一个组的用户所具有的权限。

r-x:第8-10位表示其他用户所具有的权限。
所以我们已经存在的.ssh并不是一个文件夹,我们可以删除已有的 .ssh,再ssh-keygen就会有.ssh文件夹了,
希望能帮助遇到这个问题的其他同学,也希望对这个问题有更深入了解的大佬给我一点指点,让我学习更多更深入的东西

  • 13
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值