AWS ubuntu etc子目录权限777全开,ssh 无法远程登录

最近用AWS玩了下Redis的哨兵部署,因为是通过apt方式安装的redis-server与redis-sentinel, 部署的配置文件在/etc/redis文件夹下,每次修改配置文件不可避免碰到权限相关问题。索性作死输入了个命令 Orz...

bash 复制代码
sudo chmod 777 -R /etc

结果就是执行sudo开头的命令会出现 sudo is world writable错误,然后关闭ssh连接也会发现无法再次登录。

如果只是出现第一个问题,我们完全可以通过在命令对sudo相关文件进行权限设置解决。但如果根本都进去不了命令行,那就直接寄~

看到网上也有选择新开实例,利用快照重新挂载卷进行恢复的,但这种方式还是太麻烦了。毕竟我们原来的服务器中可能保存了很多重要的数据,做迁移实属让人emo。下面提供一种较为简单的方式来solve这个手贱导致的问题,当然仅供参考(笑)

1. 暂时停止出现问题的实例

2. 在实例完全停止之后,选择操作→实例设置→编辑用户数据

3. 将以下脚本复制并粘贴到++编辑用户数据++ 字段,然后选择++保存++

bash 复制代码
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/chown root:root /usr/bin/sudo
/bin/chmod 4111 /usr/bin/sudo
/bin/chmod 644 /usr/lib/sudo/sudoers.so
/bin/chmod 0440 /etc/sudoers
/bin/chmod 775 /etc/sudoers.d
/bin/chmod 440 /etc/sudoers.d/README
/bin/chmod 440 /etc/sudo.conf 
find /etc/sudoers.d/ -type f -exec /bin/chmod 0440 {} \;
find /etc/sudoers.d/ -type f -exec /bin/chown root:root {} \;
/bin/chmod 600 /etc/ssh/ssh_host_dsa_key /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_ecdsa_key
/bin/chmod 644 /etc/ssh/ssh_host_dsa_key.pub /etc/ssh/ssh_host_rsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub
/bin/chmod 644 /etc/ssh/sshd_config /etc/ssh/ssh_config
/bin/chmod 755 /etc/ssh/ssh_config.d /etc/ssh/sshd_config.d
--//

这里对ssh的设置做一个解释,引用一下:

因为sshd为了安全,对属主的目录和文件权限有所要求。如果权限不对,则ssh的免密码登陆不生效。

用户目录权限为 755 或者 700,就是不能是77x、777,需要保障other用户不能有w权限

.ssh目录权限一般为755或者700。

rsa_id.pub 及authorized_keys权限一般为644

rsa_id权限必须为600

最后再次启动实例,就可以通过ssh连接再次进入终端啦~

Tips:

当然你也可以选择通过EC2 串行控制台 利用root账户(前提是你记得账户密码)进行权限设置操作,值得注意的是实例得是支持串行控制台的实例类型,如上面图片中的t3系列的实例类型(t3.medium)

在终端中输入以下命令即可达到一样目的:

bash 复制代码
chmod 440 /etc/sudoers
chmod 775 /etc/sudoers.d
chmod 440 /etc/sudoers.d/README
chmod 440 /etc/sudo.conf 
chmod 600 ssh_host_dsa_key ssh_host_rsa_key ssh_host_ecdsa_key
chmod 644 ssh_host_dsa_key.pub ssh_host_rsa_key.pub ssh_host_ecdsa_key.pub
chmod 644 sshd_config ssh_config
chmod 755 ssh_config.d sshd_config.d

参考链接

https://repost.aws/knowledge-center/ec2-sudo-commands

https://blog.csdn.net/levy_cui/article/details/59524158

相关推荐
ai小陈4 小时前
GPU服务器租用存储验收:检查点写入与磁盘吞吐实战
运维·服务器·人工智能·ai·ssh·gpu算力
有毒的教程7 小时前
VMware仅主机模式:物理机 ↔ 虚拟机 文件共享
运维
山岚的运维笔记9 小时前
ComfyUI NVIDIA安装教程:官方便携版下载+run_nvidia_gpu.bat启动,8G显存Windows实操
运维·服务器·windows·笔记·prompt·aigc·comfyui
海宇大数据9 小时前
零信任架构实战:基于海宇活体识别V步骤1构建自动化远程公证会话初始化网关
运维·人工智能·架构·自动化
lisanmengmeng10 小时前
Nagios邮件报警的配置
linux·运维·服务器
Ruiery10 小时前
Linux 6.6内核 CPU 深度解析(六):cpufreq
linux·运维·服务器
天远数科10 小时前
零信任架构实战:基于天远全能消金报告构建自动化消费分期网关
运维·人工智能·架构·自动化
硅基手札11 小时前
【linux内核专栏 08】时间与定时器
linux·运维·服务器
想做小南娘,发现自己是女生喵11 小时前
Linux之Ubuntu入门篇知识总结
linux·运维·服务器
帷幕落秋12 小时前
Redis哨兵模式
运维·数据库