How to change root password in Ubuntu Linux

I am a new Ubuntu Linux server admin. How can I change root password in Ubuntu Linux server using the bash shell over ssh based session?

By default, the root user account password is locked in Ubuntu Linux for security reasons. As a result, you can not login using root user or use a command such as ‘su -‘ to become a SuperUser.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Ubuntu
Est. reading time 1m

You need to use the passwd command to change the password for user accounts on Ubuntu Linux. A typical user can only change the password for his/her account only. A SuperUser (root) can change the password for any user account. Your user account info stored in /etc/passswd and an encrypted password stored in /etc/shadow file.

How to change root password in Ubuntu

The procedure to change the root user password on Ubuntu Linux:

  1. Type the following command to become root user and issue passwd:
    sudo -i
  2. OR set a password for root user in a single go:
    sudo passwd root
  3. Test it your root password by typing the following command:
    su -

How to disable your root account on Ubuntu

One can disable the root account by typing the following command:
$ sudo passwd -dl root
$ sudo passwd --delete --lock root
Sample session from the above commands:

Understanding passwd command option

  • -d OR --delete : Delete a user’s password (make it empty for root user). This is a quick way to disable a password for an account. It will set the named account passwordless.
  • -l OR --lock: Lock the password of the named account such as root. This option disables a password by changing it to a value which matches no possible encrypted value (it adds a ‘!’ at the beginning of the password)
  • root : Lock and disble root account i.e. re-disabling your root account

A note about root password on an Ubuntu server/desktop

Enabling the root account by setting the password is not needed. Almost everything you need to do as SuperUser (root) of an Ubuntu server can be done using sudo command. For example, restart apache server:
$ sudo systemctl restart apache2
You can add additional user to sudo by typing the following command:
$ sudo adduser {userNameHere} sudo
For example, add a user named tom to sudo:
$ sudo adduser tom sudo
You can in as another user say logging as tom from jerry account:
{jerry@nixcraft}$ sudo -i -u tom
When prompted enter jerry’s password i.e. the password being asked for is your own, not tom’s. For more info see “How to create a new sudo user on Ubuntu Linux server” and RootSudo.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Appa Dec 1, 2017 @ 21:21

    Pretty simple:
    sudo passwd root
    Type YOUR OWN password tho.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum