This blog post explains how to disable (reject) any root password on Debian and Ubuntu, thus rejecting login attempts as root. Becoming root with sudo (by typing the calling user's password) or ssh (using a public key) remains possible.
TL;DR Run as root: passwd -d -l root
How to become root if password-based root logins are (or will be) disabled?
Before disabling password-based root logins, make sure you have other ways to become root. One possible way is running sudo (without arguments) from a non-root user. To make this work, first you have to install sudo by running (without the leading #
) as root:
# apt-get install sudo
as root. (Ubuntu systems come with sudo preinstalled, Debian systems don't have it by default.) Then run as root, replacing MyUser with your non-root login name:
# adduser MyUser sudo
After running this, running sudo as that user will ask for the user's password (not the root password), and when typed correctly, you will get a root shell, and will be able to run commands as root. (Type exit to exit from the root shell.)
An alternative to sudo for becoming root without a password is running ssh root@localhost. For that you need a properly configured sshd (with PermitRootLogin without-password
or PermitRootLogin yes
in /etc/ssh/sshd_config), creating an SSH key pair and appending the public key to /root/.ssh/authorized_keys. If you need help setting this up or using it, then please ask a Unix or Linux guru friend.
How to disable password-based root logins
To disable (reject) any root password on Debian and Ubuntu, run this (without the leading #
) as root:
# passwd -d -l root
This effectively changes the 2nd field line starting with root:
in /etc/shadow to !
, thus the line will start with root:!:
, making login, su, ssh (when using password authentication, i.e. no public key) reject login attempts as root. Typically the password wouldn't even be asked for, but if it is, any password would be rejected. An alternative to the command above is editing the /etc/shadow file manually (as root), and adding the !
. Also the -d
flag is not necessary, without it the password hash is still kept in /etc/shadow (but a !
is prepended to disable it).
Ubuntu comes with this default (root:!:
in /etc/shadow), Debian doesn't.
If you want to disable the root password in ssh only (and allow password-based root logins in login and su), then instead of running the command above, add (or change) the line
PermitRootLogin without-password
to /etc/ssh/sshd_config (as root), and then run (as root):
# /etc/init.d/ssh restart
Please note that there are ways to permit a root login without a password (or with an empty password), but this is very bad security practice, so this blog post doesn't explain how to do it.
How to enable password-based root logins
To enable password-based root logins again, run this as root:
# passwd root
It will ask you to specify the new password for root.
No comments:
Post a Comment