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
Remote login as root in ubuntu
提问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:
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_config
whether the configure PermitRootLogin yes
below # 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:
只要:
The remote host allows the user connecting to be able to sudo without asking for a further password.
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
远程主机允许连接的用户无需再输入密码即可进行 sudo。
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 邮件列表档案中有解释。
Open
/etc/ssh/sshd_config
and check ifPermitRootLogin
is set toyes
. If not, then set it to yes and restartssh
withsudo service ssh restart
Create the
.ssh
directory 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
Create a public/private key pair in the system you want to login from.
Copy your public key to your regular user account.
Append your public key to
.ssh/authorized_keys
of 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
打开
/etc/ssh/sshd_config
并检查是否PermitRootLogin
设置为yes
。如果没有,那么它设置为yes并重新启动ssh
与sudo service ssh restart
.ssh
如果目录不存在,则在 root 的家中创建该目录,并确保它具有严格的权限:sudo -i mkdir -p .ssh sudo -i chmod 700 .ssh
在您要登录的系统中创建一个公钥/私钥对。
将您的公钥复制到您的常规用户帐户。
将您的公钥附加到
.ssh/authorized_keys
root,并确保该文件具有严格的权限: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 root
account, 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”,以保持当前环境。
回答by RoyEp
- Edit sshd_config file
vi /etc/ssh/sshd_config
- Comment out
#PermitRootLogin without-password
under Authentication: - insert below
PermitRootLogin yes
- Save the file.
- Restart the ssh service
service ssh restart
- 编辑 sshd_config 文件
vi /etc/ssh/sshd_config
#PermitRootLogin without-password
在身份验证下注释掉:- 在下面插入
PermitRootLogin yes
- 保存文件。
- 重启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 root
account 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-password
with yes
, as in the following line:
只需更换这个词without-password
用yes
,如下面的一行:
PermitRootLogin yes
Then restart SSH:
然后重启SSH:
sudo service ssh restart;
Finally Restart the system:
最后重启系统:
sudo reboot;
Done !!!
完毕 !!!
回答by Ali Azhar
Edit the
/etc/ssh/sshd_config
file and change the line that says “PermitRootLogin forced-commands-only” to “PermitRootLogin without-password”.Restart the sshd server with “
service sshd restart
”.Edit the
/root/.ssh/authorized_keys
file. 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”.
编辑该
/etc/ssh/sshd_config
文件并将“PermitRootLogin 强制命令-only”行更改为“PermitRootLogin without-password”。使用“
service sshd restart
”重新启动 sshd 服务器。编辑
/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 连接您的机器