Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote login as root in ubuntu

Tags:

linux

ssh

ubuntu

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

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

it logs in successfully.

I have login in ubuntu in directly using

 $ root@remotehostname
 root@remotehostname's password:

it is throw error message in

Permission denied, please try again.

How to solve this problem?

like image 241
Reegan Miranda Avatar asked Aug 23 '13 05:08

Reegan Miranda


4 Answers

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

you can change it to yes.

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

like image 122
vvy Avatar answered Nov 19 '22 11:11

vvy


Ubuntu documentation says:

By default, the Root account password is locked in 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.

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:

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.

like image 31
2 revs Avatar answered Nov 19 '22 13:11

2 revs


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

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.

  1. Open /etc/ssh/sshd_config and check if PermitRootLogin is set to yes. If not, then set it to yes and restart ssh with sudo service ssh restart

  2. 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
    
  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_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
    

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

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

sudo passwd -l root
like image 11
janos Avatar answered Nov 19 '22 13:11

janos


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:

ssh user@remotehostname
user@remotehostname's password:

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

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.

See Ubuntu Sudo/Root documentation

like image 4
h1618 Avatar answered Nov 19 '22 13:11

h1618