如何在服务器上为 root 用户设置公共 SSH 密钥?

如何在服务器上为 root 用户设置公共 SSH 密钥?

我正在尝试跟随Slicehost 文档设置我的服务器。我到达了 SSH 部分。我按照所写操作,但当我从 root 注销时,我无法再次访问 root@IP_ADDRESS -p 30000!但我可以访问 user@IP_ADDRESS -p 30000。

那么,问题是,如何为 root 用户设置公共 SSH 密钥?

答案1

我不太明白你的意思。你的意思是你不能用你的 public_key 以 root 身份登录?如果是这样,请检查 /root/.ssh/authorized_keys

还查看了 /etc/ssh/sshd_config 它应该包含:

PermitRootLogin yes

答案2

查看日志。sshd 通常会给出有用的消息。根据您的平台尝试/var/log/auth.log/var/log/messages或(或其他内容)。/var/log/syslog

不过,这听起来像是权限问题。尝试以下命令(以 root 身份): chmod 700 ~root ~root/.ssh && chmod 600 ~root/.ssh/authorized_keys

答案3

警告:您将需要直接物理访问机器,或者已经具有 ssh 登录功能(通过密码验证或超级用户密钥对)。

为了让它在我的 Debian 机器(我的 SSH 主机)上工作,我需要生成一个新的密钥对(我在 Windows 10 上使用了 Putty;我的 SSH 客户端),然后通过编辑“sshd_config”确保以下内容已经讨论过:

$ sudo nano /etc/ssh/sshd_config

并输入以下行,或根据需要取消注释,然后保存/写出:

# Authentication:
PermitRootLogin yes

然后我需要访问 root 帐户,因此我发出:

$ sudo su

...然后为 ssh 创建必要的文件夹并为 authorized_keys 创建文件:

# cd /root
# mkdir .ssh
# cd .ssh
# nano authorized_keys

然后将相关的公钥放在这里,我从 Putty-Gen 顶部附近粘贴,然后保存/写出。

然后以 root 身份重新启动 sshd 守护进程,使用以下命令:

# systemctl restart sshd
# exit
# exit

然后,在我向 Putty 中的 SSH Auth 中添加相关的 .ppk 文件后,它就运行得很好了!

关键在于所有用户(root 和其他用户)都在 /etc/ssh/sshd_config 中共享相同的配置,但他们并不都共享相同的“authorized_keys”文件,因此我需要制作特定于 root 的文件才能使其正常工作。

您不能简单地在 /home/yournameuser/.ssh/authorized_keys 文件中添加为 root 帐户生成的公钥 - 似乎系统不会在那里寻找 root 访问权限。

答案4

这个 /etc/ssh/sshd_config 工作正常!

# Package generated configuration file
# See the sshd(8) manpage for details

# What ports, IPs and protocols we listen for
Port 30000
# 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
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768

# 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 no

# 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

UsePAM yes

相关内容