Change Root Password & Switch Accounts on Ubuntu Server

Change Root Password & Switch Accounts on Ubuntu Server

Derrk SamuelJune 5, 20264 min read
Set or change the root password using sudo passwd root, lock the root account, and learn how to switch between user accounts with the su command.

Change Root Password on Ubuntu Server

To change the root password on an Ubuntu server, log in with a user that has sudo privileges and run sudo passwd root. You will be prompted to enter your current user password, followed by the new password for the root account.


Steps to Change Root Password

1. Log In to Your Ubuntu Server

Connect via SSH or open a terminal:

ssh user@your-server-ip

Or simply open a terminal window locally.

2. Run the Password Change Command

sudo passwd root

3. Enter Your Current User Password

When prompted, type your current user's password (not the root password):

[sudo] password for user:

4. Enter the New Root Password

Type the new root password:

New password:

Re-type it to confirm:

Retype new password:

Once confirmed, the root password is updated.


Optional: Lock the Root Account After

If you want to disable root login after setting the password:

sudo passwd -l root

This locks the root account so it cannot be used directly (you can still use sudo commands).


If You're Already Logged In as Root

If you're already running as the root user, you can simply type:

passwd

No sudo required—it will directly change the root password.


If You Forgot the Root Password

If you've forgotten the root password and need to reset it:

  1. Restart the server
  2. Access the GRUB menu during boot (hold Shift)
  3. Select recovery mode or single-user mode
  4. You'll have root shell access without a password
  5. Run passwd to set a new root password
  6. Reboot normally

This requires physical or console access to the server.


Switch Accounts on Ubuntu Server

Switch to Root Account

su -

The - flag loads the root user's profile and environment variables. You'll be prompted for the root password.

Switch to Another User Account

su - ExampleUserName

Replace ExampleUserName with the actual username. You'll be prompted for that user's password.

Quick Account Switch (Without Profile)

To switch accounts without loading their shell profile:

su root        # Just the username, no environment loaded
su ExampleUserName

The difference is subtle—su - is generally preferred because it fully loads the user's environment.


Quick Reference

TaskCommand
Change root passwordsudo passwd root
Change password as rootpasswd
Lock root accountsudo passwd -l root
Unlock root accountsudo passwd -u root
Switch to rootsu -
Switch to another usersu - username
Switch without profilesu username

Security Notes

  • Root access should be restricted to necessary operations only
  • Use sudo for individual commands rather than switching to root
  • Consider locking the root account (passwd -l) if direct root login isn't needed
  • Always use strong passwords for the root account
  • Keep sudo access auditable by using it instead of direct root login
D

Derrk Samuel

Web3, crypto, and Linux PC insights — practical guides, security tips, mining setups, blockchain tools, and decentralized tech strategies for techs.

Discussion

Leave a comment

No comments yet. Be the first to start the discussion!