Ubuntu 云版本如何通过 ssh 强制执行“无 root 登录”?

Max*_*ler 8 security ubuntu cloud ssh root

我正在调整 ubuntu 云版本默认设置,其中拒绝 root 登录。

尝试连接到这样的机器会产生:

maxim@maxim-desktop:~/workspace/integration/deployengine$ ssh root@ec2-204-236-252-95.compute-1.amazonaws.com
The authenticity of host 'ec2-204-236-252-95.compute-1.amazonaws.com (204.236.252.95)' can't be established.
RSA key fingerprint is 3f:96:f4:b3:b9:4b:4f:21:5f:00:38:2a:bb:41:19:1a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ec2-204-236-252-95.compute-1.amazonaws.com' (RSA) to the list of known hosts.
Please login as the ubuntu user rather than root user.

Connection to ec2-204-236-252-95.compute-1.amazonaws.com closed.
Run Code Online (Sandbox Code Playgroud)

我想知道在哪个配置文件中配置了通过 ssh 阻止根目录以及如何更改打印的消息?

小智 14

老问题,但没有人真正回答你,我也有同样的问题:这个配置来自哪里?

它起源于cloudinit,恰好在cc_ssh.py内部/usr/lib/python2.7/dist-packages/cloudinit/config

这反过来又直接依赖于文件/etc/cloud/cloud.cfg。你找到一条线disable_root: true

您应该能够通过调整您的用户数据并添加 line 来覆盖它disable_root: false。您的云提供商应该使用户数据可配置。

  • cloud-init 所做的是在 `/root/.ssh/authorized_keys` 中以 `no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as用户 \"ubuntu\" 而不是用户 \"root\"。';echo;sleep 10" ssh-rsa xxxxxx`。因此,即使 `PermitRootLogin yes` ssh 命令也无法获得工作 shell。 (9认同)

Red*_*ick 5

假设您的 sshd 配置具有PermitRootLogin yes.

sudo grep "login as the ubuntu user" /root/.??*

但是,我强烈建议您仔细阅读并留意 Mike Scott 提供的链接。

根 SSH

最后,如果您希望绕过 Ubuntu 安全标准并恢复允许以 root 身份使用 ssh 和 rsync 的旧做法,此命令将为官方 Ubuntu 映像的新实例打开它:

ssh -i KEYPAIR.pem ubuntu@HOSTNAME 'sudo cp /home/ubuntu/.ssh/authorized_keys /root/.ssh/' 不推荐这样做,但它可能是一种让现有 EC2 自动化代码继续工作的方法,直到您可以升级到上述 sudo 实践。

我一直禁用 root SSH 登录,因为任何启用了 SSH 的面向公众的服务器都会被犯罪僵尸网络的 root 登录尝试日夜连续攻击。

其他地方的文件警告

很少需要启用 Root 帐户。作为 Ubuntu 系统的管理员,您需要做的几乎所有事情都可以通过 sudo 或 gksudo 完成。如果您确实需要持久的 Root 登录,最好的选择是使用以下命令模拟 Root 登录 shell...

sudo -i