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

相关推荐
小小测试开发8 分钟前
RAG应用评测:从指标体系到LLM-as-a-Judge的自动化落地
android·运维·人工智能·自动化
深念Y12 分钟前
# CC-Switch + Claude/Codex 折腾教训记录
运维·服务器·网络·ai·agent·web·ccsiwtch
疯狂小猫咪13 分钟前
教培 SaaS vs 定制开发:技术架构与总拥有成本对比
运维
Lalolander34 分钟前
WorkBuddy App 可以连接电脑吗?远程操控桌面端怎么用?
运维·电脑·腾讯云ai代码助手·workbuddy
小HANN36 分钟前
保姆级实战:CentOS7 搭建 LAMP 环境部署 WordPress 个人博客
linux·运维·服务器·经验分享
前端 贾公子1 小时前
Linux云服务器安装与PostgreSQL安装
运维·服务器
严谨的麻辣烫1 小时前
批量静态 IP 如何管理?用 Python 建立一个简单的 IP 资源监控方案
运维·服务器·网络·python·tcp/ip
BugShare1 小时前
告别来回切换,Navop:一站式整合数据库、SSH、终端与 AI 的开发运维工作台
运维·数据库·ssh
奈斯先生Vector1 小时前
从“能调用”到“可替换”:Coze 多模型 API 编排层设计指南
linux·运维·人工智能·ubuntu·平面·ios
技术不支持1 小时前
wsl 离线安装 Debian 11, Debian 12等指定版本
运维·debian