我可以通过 SSH 以用户“ubuntu”身份成功登录到我的云实例,但无法以用户“root”身份登录。目前有几篇帖子涉及更改 ssh 配置以允许 root 访问,但我没有一个成功。例如,我对 /etc/ssh/sshd_config 文件进行了常规更改,即:
#Authentication
PermitRootLogin yes
这没有效果。
现在,当我尝试以“root”身份登录时收到的消息是:
请以用户“ubuntu”而不是“root”身份登录。
搜索“请登录”,我在'/usr/lib/python3/dist-packages/cloudinit/config/cc_ssh.py'文件(所有地方)中发现以下几行:
DISABLE_ROOT_OPTS = (
"no-port-forwarding,no-agent-forwarding,"
"no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\""
" rather than the user \\\"root\\\".\';echo;sleep 10\"")
我对此不太理解。我猜想这个 Python 脚本是在首次创建实例时运行的,并且负责某些阻止我以“root”身份登录的附加 SSH 设置。
在我看来,有问题的 ssh 配置指令是“no-X11-forwarding”和另外两个。我得出这个结论是因为它们似乎与有问题的消息有关。
我猜想该command
指令指示 ssh 守护程序在违反相关指令时显示此消息。我猜对了吗?
我要补充的是,我知道反对以 root 用户身份登录的论点。但是,这是一个私人实例,我将是唯一的用户。我需要以“root”身份进行 ssh 访问,因为我的部署脚本需要以 root 身份运行,以避免复杂化。
更新:下面提到的python脚本是Ubuntu的一部分云初始化包裹。
进一步更新:以下是 /etc/pam.d/sshd 文件的内容...
# PAM configuration for the Secure Shell service
# Standard Un*x authentication.
@include common-auth
# Disallow non-root logins when /etc/nologin exists.
account required pam_nologin.so
# Uncomment and edit /etc/security/access.conf if you need to set complex
# access limits that are hard to express in sshd_config.
# account required pam_access.so
# Standard Un*x authorization.
@include common-account
# SELinux needs to be the first session rule. This ensures that any
# lingering context has been cleared. Without this it is possible that a
# module could execute code in the wrong domain.
session [success=ok ignore=ignore module_unknown=ignore default=bad] pam_selinux.so close
# Set the loginuid process attribute.
session required pam_loginuid.so
# Create a new session keyring.
session optional pam_keyinit.so force revoke
# Standard Un*x session setup and teardown.
@include common-session
# Print the message of the day upon successful login.
# This includes a dynamically generated part from /run/motd.dynamic
# and a static (admin-editable) part from /etc/motd.
session optional pam_motd.so motd=/run/motd.dynamic
session optional pam_motd.so noupdate
# Print the status of the user's mailbox upon successful login.
session optional pam_mail.so standard noenv # [1]
# Set up user limits from /etc/security/limits.conf.
session required pam_limits.so
# Read environment variables from /etc/environment and
# /etc/security/pam_env.conf.
session required pam_env.so # [1]
# In Debian 4.0 (etch), locale-related environment variables were moved to
# /etc/default/locale, so read that as well.
session required pam_env.so user_readenv=1 envfile=/etc/default/locale
# SELinux needs to intervene at login time to ensure that the process starts
# in the proper default security context. Only sessions which are intended
# to run in the user's context should be run after this.
session [success=ok ignore=ignore module_unknown=ignore default=bad] pam_selinux.so open
# Standard Un*x password updating.
@include common-password
...以及 /etc/ssh/sshd_config 文件的内容...
# Package generated configuration file
# See the sshd_config(5) manpage for details
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication yes
# Change to no to disable tunnelled clear text passwords
PasswordAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
...按照要求。
答案1
查看该/root/.ssh/authorized_keys
文件。你很可能会发现类似这样的内容:
root@instance-00:~# cat /root/.ssh/authorized_keys
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"ubuntu\" rather than the user \"root\".';echo;sleep 10" ssh-rsa AAAA...= user@host
(这是从不同的云提供商那里获得的,但配置似乎与亚马逊的相同)。
只需删除前面的所有内容ssh-rsa
,您就应该能够以 root 身份远程登录(当然,其他人已经告诉过您这不是最佳做法的所有内容仍然适用!)
如果您真的很懒,您可以authorized_keys
使用用户的文件覆盖该文件ubuntu
:
root@instance-00:~# cp /home/ubuntu/.ssh/authorized_keys /root/.ssh/authorized_keys
如果你好奇这个前缀是怎么来的,你已经找到的相同 Python 代码 ( /usr/lib/python3/dist-packages/cloudinit/config/cc_ssh.py
) 中有答案apply_credentials
。请注意它如何设置授权密钥的前缀:
if disable_root:
if not user:
user = "NONE"
key_prefix = disable_root_opts.replace('$USER', user)
else:
key_prefix = ''
答案2
强烈建议阻止远程root
登录服务器(出于安全原因)。
建议的方式是以普通用户身份登录,并使用sudo
以获得root
访问权限。
sudo
为您提供每个命令的完全访问权限的最终命令root
是:
sudo bash
对于应该执行的特定命令,root
您可以使用:
sudo specific-command
执行命令的示例如下root
:
sudo ls -lsa /root
注意:如果你想更改亚马逊政策并允许root
登录,你可以使用以下答案amazon-ec2-root-login SO问答
参考下面设置root登录:
sudo -s (to become root) vi /root/.ssh/authorized_keys
删除文件开头的行,直到看到单词
ssh-rsa
。vi /etc/ssh/sshd_config
将变量设置
PermitRootLogin
为PermitRootLogin without-password
(不带引号)sudo /etc/init.d/sshd restart