Changing the root password on your server

Two cases cover nearly everything: you are logged in and want a new password, or you are locked out and need the rescue system to set one.

How do I change the root password while logged in?

Run passwd as root and type the new password twice. If you log in as a normal user with sudo rights, run sudo passwd root instead. The change takes effect at once for new logins; existing sessions stay open. Nothing in our console needs updating afterwards, because the console never stores or uses your system passwords.

sudo passwd root
# New password:
# Retype new password:
# passwd: password updated successfully

If the system refuses the password as too simple, that is the PAM quality module doing its job. Pick something longer rather than disabling the check.

What makes a good root password?

Length beats complexity. A random phrase of five or six unrelated words, or 20 random characters from a password manager, resists guessing far better than a short string full of symbols. Generate one on the server if you have no manager to hand:

openssl rand -base64 24
tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 22; echo

Store it in a password manager, never in a shell history, a ticket or a chat. If you must type it on the command line for automation, use chpasswd reading from a file with mode 600 and delete the file afterwards.

Should root be able to log in over SSH with a password at all?

Ideally not. The safest arrangement is SSH keys for every login and a password kept only for local use, such as the rescue chroot or a sudo prompt. When you choose SSH keys at order time, the server is delivered with keys for root and no root password is sent; you can still set one later for sudo or emergencies. To stop password guessing over the network, set this in /etc/ssh/sshd_config or a file in /etc/ssh/sshd_config.d/:

printf '%s\n' 'PermitRootLogin prohibit-password' 'PasswordAuthentication no' 'KbdInteractiveAuthentication no' > /etc/ssh/sshd_config.d/20-keys-only.conf

Test with sshd -t, reload with systemctl reload ssh (or sshd on AlmaLinux, Rocky Linux and Fedora), and keep your current session open while you try a fresh login from a second terminal.

I forgot the root password. How do I reset it?

Use rescue mode: it boots a separate system from the network, from which you can mount your drives and set a new password. The whole job takes about ten minutes, and no data is touched.

lsblk -f
mount /dev/md2 /mnt
chroot /mnt passwd root
umount /mnt

On a single-disk layout the root partition might be /dev/nvme0n1p2 instead of an md device; lsblk -f shows which one holds an ext4 or XFS filesystem of the right size. If the system uses LVM, activate it first with vgchange -ay.

Why does the new password not work after a rescue reset?

On AlmaLinux, Rocky Linux and Fedora, SELinux may block a password file that was written outside the running system. The fix is to ask for a full relabel on the next boot: create an empty file named .autorelabel at the root of the mounted filesystem before you leave rescue mode. The first boot then takes a few minutes longer while labels are restored.

touch /mnt/.autorelabel

On Debian and Ubuntu the usual cause is a mismatch between the keyboard layout you typed with and the one you think you used. Retype the password slowly, or set it again with only letters and digits to rule this out.

How often should the root password change?

Change it whenever someone who knew it leaves, whenever it may have been exposed, and after any suspected intrusion; regular forced changes add little when the password is long and unique. If several people administer the machine, give each person an own account with sudo rather than sharing root, so access can be removed per person. The Reset a user password guide covers those personal accounts, and Create SSH keys explains the key setup that makes the root password almost irrelevant for daily work.

Related pages

Reset a user password

Set or reset passwords for normal accounts, unlock users after failed logins, force a change at next login and manage sudo access.

Read more

Create SSH keys

Generate an Ed25519 key pair on Windows, macOS or Linux, add it to your account before ordering, and manage keys on a running server.

Read more

IPMI and rescue mode

What out-of-band management is, and how console power control, reinstall and rescue boot cover the same needs on our machines.

Read more

Hardening a new dedicated server

The first hour on a fresh machine: SSH keys, a firewall, automatic updates, brute force blocking and a habit of checking what listens.

Read more

Questions and answers

Do you send me a root password at delivery?

Only if you did not choose an SSH key. When keys are picked at order time, the server starts with your keys for root and no password is sent.

Can support reset my root password for me?

You do it yourself from rescue mode in about ten minutes. The console starts the rescue system and shows its own password; your system password stays entirely in your hands.

Does a reinstall change the root password?

Yes. A reinstall wipes the drives and sets either your SSH keys or a new one-time root password, shown in the console.

Why does SELinux matter for a password reset?

On AlmaLinux, Rocky Linux and Fedora a password file written from rescue mode may carry the wrong label. Creating /.autorelabel before rebooting fixes it.

Is it safe to allow root login with a password?

It works but invites constant guessing. Use SSH keys for logins and keep the root password for local use only.