Linux 在 ubuntu 中以 root 身份远程登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18395622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:38:33  来源:igfitidea点击:

Remote login as root in ubuntu

linuxubuntussh

提问by Reegan Miranda

In my project, I have to install some package remotely. If I have to login in debian, I say:

在我的项目中,我必须远程安装一些包。如果我必须在 debian 中登录,我会说:

$ ssh root@remotehostname
root@remotehostname's password: 

it logs in successfully.

它成功登录。

I have login in ubuntuin directly using

我已经在ubuntu中直接使用登录

 $ root@remotehostname
 root@remotehostname's password:

it is throw error message in

它抛出错误消息

Permission denied, please try again.

How to solve this problem?

如何解决这个问题呢?

采纳答案by devnull

Ubuntu documentationsays:

Ubuntu 文档说:

By default, the Root account password is locked in Ubuntu.

默认情况下,Root 帐户密码在 Ubuntu 中是锁定的。

It also says:

它还说:

Please keep in mind, a substantial number of Ubuntu users are new to Linux.There is a learning curve associated with any OS and many new users try to take shortcuts by enabling the root account, logging in as root, and changing ownership of system files.

请记住,相当多的 Ubuntu 用户是 Linux 新手。任何操作系统都有一个学习曲线,许多新用户尝试通过启用 root 帐户、以 root 身份登录和更改系统文件的所有权来走捷径。

It talks at length about why it's been done this way.

它详细讨论了为什么要这样做。



Enabling the root account:

启用根帐户:

sudo -i

To enable the Root account (i.e. set a password) use:

要启用 Root 帐户(即设置密码),请使用:

sudo passwd root

Use at your own risk!

使用风险自负!

Logging in to X as root may cause very serious trouble.If you believe you need a root account to perform a certain action, please consult the official support channels first, to make sure there is not a better alternative.

以 root 身份登录 X 可能会导致非常严重的问题。如果您认为您需要一个 root 帐户来执行某个操作,请先咨询官方支持渠道,以确保没有更好的选择。

回答by vvy

check the /etc/ssh/sshd_configwhether the configure PermitRootLogin yesbelow # Authentication:. If not yes, it doesn't permit login as root.

检查/etc/ssh/sshd_config是否配置PermitRootLogin yes如下# Authentication:。如果不是yes,则不允许以 root 身份登录。

you can change it to yes.

您可以将其更改为yes.

Then, restart ssh service to apply the changes: sudo service sshd restart

然后,重新启动 ssh 服务以应用更改: sudo service sshd restart

回答by Chamullo

edit your /etc/sshd_config doing:

编辑你的 /etc/sshd_config 做:

sudo /etc/sshd_config

search for line having PermitRootLogininto AuthenticationSection and uncomment PermitRootLoginor set PermitRootLogin yes. Or simply add this line to the end of file:

AuthenticationSection 中搜索具有PermitRootLogin 的行并取消注释PermitRootLogin或设置PermitRootLogin yes。或者简单地将此行添加到文件末尾:

PermitRootLogin yes

Next you'll gonna restart ssh daemon doing

接下来你将重新启动 ssh 守护进程

/etc/init.d/ssh restart 

or

或者

service ssh restart

depending on your linux distribution Now you'll login via ssh with your root account Warning: For security reason this is not recommended. What you can do is login with any other accound via ssh and within your user session do:

取决于您的 linux 发行版 现在您将使用您的 root 帐户通过 ssh 登录警告:出于安全原因,不建议这样做。您可以做的是通过 ssh 使用任何其他帐户登录,并在您的用户会话中执行以下操作:

sudo command

or simply:

或者干脆:

sudo su

to continue session under your root account

在您的 root 帐户下继续会话

回答by V H

ssh -l user localhost "sudo whoami"

returns:

返回:

root

So long as:

只要:

  1. The remote host allows the user connecting to be able to sudo without asking for a further password.

  2. ssh-copy-id has been run or correct keys copied across to remote host so that the user can connect again without a password via ssh

  1. 远程主机允许连接的用户无需再输入密码即可进行 sudo。

  2. ssh-copy-id 已运行或将正确的密钥复制到远程主机,以便用户无需密码即可通过 ssh 再次连接

All that is different was the word sudo was added before carrying out the command

不同的是在执行命令之前添加了sudo这个词

If there are security issues with keys etc then you could take a look at a language called expect - using an expect script you will be able to ssh / telnet what ever it is to a remote host and send passwords etc accordingly

如果密钥等存在安全问题,那么您可以查看一种名为 expect 的语言 - 使用 expect 脚本,您将能够通过 ssh/telnet 将任何内容发送到远程主机并相应地发送密码等

回答by stink

I think the only time I ever tired to log in as root was the first time I used AWS EC2 and I tried to long in as root because I knew it existed. The user name ec2-user.

我认为我唯一一次厌倦以 root 身份登录是我第一次使用 AWS EC2 并且我尝试以 root 身份登录,因为我知道它存在。用户名 ec2-user。

回答by janos

Do not enable the root account. Do not set a password for the root account.

不要启用 root 帐户。不要为 root 帐户设置密码。

A better way is to allow root login using public key authentication, not with password. The reasoning is explained in the Debian mailing list archives.

更好的方法是允许使用公钥身份验证而不是密码进行 root 登录。原因在 Debian 邮件列表档案中有解释。

  1. Open /etc/ssh/sshd_configand check if PermitRootLoginis set to yes. If not, then set it to yes and restart sshwith sudo service ssh restart

  2. Create the .sshdirectory in root's home if it doesn't exist and make sure it has strict permissions:

    sudo -i mkdir -p .ssh
    sudo -i chmod 700 .ssh
    
  3. Create a public/private key pair in the system you want to login from.

  4. Copy your public key to your regular user account.

  5. Append your public key to .ssh/authorized_keysof root, and make sure the file has strict permissions:

    cat id_rsa.pub | sudo -i tee -a .ssh/authorized_keys
    sudo -i chmod 600 .ssh/authorized_keys
    
  1. 打开/etc/ssh/sshd_config并检查是否PermitRootLogin设置为yes。如果没有,那么它设置为yes并重新启动sshsudo service ssh restart

  2. .ssh如果目录不存在,则在 root 的家中创建该目录,并确保它具有严格的权限:

    sudo -i mkdir -p .ssh
    sudo -i chmod 700 .ssh
    
  3. 在您要登录的系统中创建一个公钥/私钥对。

  4. 将您的公钥复制到您的常规用户帐户。

  5. 将您的公钥附加到.ssh/authorized_keysroot,并确保该文件具有严格的权限:

    cat id_rsa.pub | sudo -i tee -a .ssh/authorized_keys
    sudo -i chmod 600 .ssh/authorized_keys
    

With this setup you should be able to login as root using your private key.

通过此设置,您应该能够使用您的私钥以 root 身份登录。

If you have previously enabled the rootaccount, make sure to disable it now:

如果您之前已启用该root帐户,请确保现在将其禁用:

sudo passwd -l root

回答by h1618

You could set a password for root, but this is not recommended and could open a security risk. But if you have an user account on the target system that has sudo credentials, you could log in as user:

您可以为 root 设置密码,但不建议这样做,并且可能会带来安全风险。但是,如果您在目标系统上有一个具有 sudo 凭据的用户帐户,则可以以用户身份登录:

ssh user@remotehostname
user@remotehostname's password:

and then do what you want using sudo or get a root shell the recommended way:

然后使用 sudo 执行您想要的操作或以推荐的方式获取 root shell:

user@remotehostname$ sudo su
Password:
root@remotehostname#

and then do your housekeeping. Instead of 'sudo su' you also could use 'sudo -i', which is equivalent, or 'sudo -s', that keeps the current environment.

然后做你的家务。您还可以使用等效的“sudo -i”或“sudo -s”来代替“sudo su”,以保持当前环境。

See Ubuntu Sudo/Root documentation

请参阅Ubuntu Sudo/Root 文档

回答by RoyEp

  1. Edit sshd_config file vi /etc/ssh/sshd_config
  2. Comment out #PermitRootLogin without-passwordunder Authentication:
  3. insert below PermitRootLogin yes
  4. Save the file.
  5. Restart the ssh service service ssh restart
  1. 编辑 sshd_config 文件 vi /etc/ssh/sshd_config
  2. #PermitRootLogin without-password在身份验证下注释掉:
  3. 在下面插入 PermitRootLogin yes
  4. 保存文件。
  5. 重启ssh服务 service ssh restart

回答by Pratik Patil

By default, the Root account password is locked in Debian based systems like Ubuntu:

默认情况下,Root 帐户密码在基于 Debian 的系统(如 Ubuntu)中是锁定的:

Switch to rootaccount user:

切换到root帐号用户:

sudo -i;

Now set the password for root account:

现在为 root 帐户设置密码:

sudo passwd;

Simply adding a password for root is not enough.

简单地为 root 添加密码是不够的。

Incoming ssh connections need to be enabled as below:

需要启用传入的 ssh 连接,如下所示:

Edit /etc/ssh/sshd_config, and find out the following line:

编辑/etc/ssh/sshd_config,并找出以下行:

PermitRootLogin without-password

Just replace the word without-passwordwith yes, as in the following line:

只需更换这个词without-passwordyes,如下面的一行:

PermitRootLogin yes

Then restart SSH:

然后重启SSH:

sudo service ssh restart;

Finally Restart the system:

最后重启系统:

sudo reboot;

Done !!!

完毕 !!!

回答by Ali Azhar

  1. Edit the /etc/ssh/sshd_configfile and change the line that says “PermitRootLogin forced-commands-only” to “PermitRootLogin without-password”.

  2. Restart the sshd server with “service sshd restart”.

  3. Edit the /root/.ssh/authorized_keysfile. This file should have one line only. It starts by “echo” or “command … sleep 10;” then you can see “ssh-rsa [big long key]”. Remove the text from the beginning of the line until where it says “ssh-rsa”.

  1. 编辑该 /etc/ssh/sshd_config文件并将“PermitRootLogin 强制命令-only”行更改为“PermitRootLogin without-password”。

  2. 使用“ service sshd restart”重新启动 sshd 服务器。

  3. 编辑/root/.ssh/authorized_keys文件。这个文件应该只有一行。它以“echo”或“command ... sleep 10;”开头。然后你可以看到“ssh-rsa [big long key]”。从行首删除文本,直到它显示“ssh-rsa”。

SSH your machine using root with your keypair

使用 root 和您的密钥对通过 SSH 连接您的机器