
Change Root Password & Switch Accounts on Ubuntu Server
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:
- Restart the server
- Access the GRUB menu during boot (hold Shift)
- Select recovery mode or single-user mode
- You'll have root shell access without a password
- Run
passwdto set a new root password - 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
| Task | Command |
|---|---|
| Change root password | sudo passwd root |
| Change password as root | passwd |
| Lock root account | sudo passwd -l root |
| Unlock root account | sudo passwd -u root |
| Switch to root | su - |
| Switch to another user | su - username |
| Switch without profile | su username |
Security Notes
- Root access should be restricted to necessary operations only
- Use
sudofor 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
Derrk Samuel
Web3, crypto, and Linux PC insights — practical guides, security tips, mining setups, blockchain tools, and decentralized tech strategies for techs.
Related Articles
Discussion
Leave a comment
No comments yet. Be the first to start the discussion!
