How to change root password in Ubuntu Linux

in Categories , last updated January 9, 2018

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.

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:
How to change root password in ubuntu Linux

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 0 comments/add one below):