找回密码
 新建账号

Please login as the user "ubuntu" rather than the user "root".

[复制链接]
php 发表于 2024/4/23 00:40 | 显示全部楼层 |阅读模式
Please login as the user "ubuntu" rather than the user "root".
Please login as the user "ec2-user" rather than the user "root".

Amazon EC2 Ubuntu Linux ssh + 公钥/私钥对登录服务器时只能使用 ubuntu 或者 ec2-user 用户,不能使用 root 或者其它用户登录 ssh 服务(sshd),ubuntu 或 ec2-user 通常是创建系统实例时的默认用户,具体是哪一个名字因所选择的操作系统而异,非默认用户登录时会触发 Please login as the user "xxx" rather than the user "root" 错误,导致 ssh 无法登录。

这个问题的底层原因是 Amazon EC2 在初始化操作系统前会要求用户选择或创建一个公钥/私钥对,创建公钥/私钥对后,公钥文件会自动保存到服务器上默认用户家目录的 .ssh 目录中,文件名是 authorized_keys,而私钥文件会通过浏览器自动下载到用户电脑上供用户保存,使用 ssh 命令登录时通过 -i 参数指定这个私钥文件,实现不需要输入密码登录远端服务器,这个私钥文件一旦丢失无法恢复,需要妥善保存,root 用户及其它用户无法使用私钥通过公钥认证(PubkeyAuthentication)来登录 ssh 服务,是因为服务器上对应的用户家目录没有认证需要的公钥文件。

所以,想要使用默认用户以外的用户通过公钥认证来登录 ssh 服务,需要将默认用户家目录 .ssh/authorized_keys 复制到需要登录 ssh 服务的用户的家目录中的 .ssh 目录中。

需要说明的是,ssh/sshd 对权限要求极高,它通常要求密钥文件只能让登录的用户拥有权限,不能给其它用户权限,甚至连登录用户的用户组都不能有密钥文件的权限,否则密钥文件中的密钥会被当成无效密钥。
  1. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  2. @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
  3. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  4. Permissions for '.ssh/wuxiancheng.pem' are too open.
  5. It is required that your private key files are NOT accessible by others.
  6. This private key will be ignored.
  7. Failed to load key .ssh/wuxiancheng.pem: bad permissions
复制代码
最好在客户端将私钥文件权限设置为拟登录用户只读其它用户无任何权限,在服务端将公钥文件 authorized_keys 文件权限设置为拟登录用户只读其它用户无任何权限。
  1. chmod 0400 <密钥文件>
复制代码
Windows 上使用 icacls 命令设置权限
  1. icacls <密钥文件> /inheritance:r /grant <用户名>:R
复制代码
相关链接 ssh sshd icacls

手机版|轻松E站

GMT+8, 2024/5/4 05:08

快速回复 返回顶部 返回列表