Before you begin, it is important to understand the difference between an administrator user
and the root account.
In modern Ubuntu installations, the root account
is usually not used for regular logins. Administrative
operations are performed as a regular user via the
sudo mechanism.
For example, a server may have a user:
admin
who is allowed to run commands as root:
sudo systemctl restart nginx
In this case, if the password is forgotten, you need to change the password of the
admin user, not necessarily the root password.
If you really need to set a password for the root account, you can do so with the command:
sudo passwd root
However, having a root password does not mean that remote root login via SSH will automatically be allowed. SSH settings may prohibit this method of connection.
A password reset may be required in several typical situations:
sudo;If you still have access to another user with administrator privileges, recovery is much easier — the password can be changed directly from the running system.
This is the simplest and safest option if the server already has
another user with sudo privileges.
First, connect to the server as an existing administrator. For example:
ssh admin@SERVER-IP
Then run:
sudo passwd USERNAME
For example, if you need to change the password of the user
ubuntu:
sudo passwd ubuntu
The system will ask you to enter the new password twice.
After the password is successfully changed, you will see a message similar to this:
passwd: password updated successfully
After that, the user will be able to log in with the new password.
If you forgot the password of the only administrative user, one of the standard recovery methods is Recovery Mode.
Ubuntu provides a recovery mode from which you can get a
root shell and run the passwd command.
Reboot the server in the usual way, if possible. For a VDS, this can be done through the control panel or VNC console.
During boot, you need to open the GRUB menu. Depending on the system configuration, you may need to hold the Shift key or press Esc.
In the GRUB menu, select:
Advanced options for Ubuntu
A list of installed Linux kernels will appear.
Select the required kernel version marked with:
(recovery mode)
For example:
Ubuntu, with Linux 6.x.x-x-generic (recovery mode)
Press Enter.
After Recovery Mode boots, the recovery menu will appear. Select the option:
root - Drop to root shell prompt
You will then get a command prompt with
root superuser privileges.
In Recovery Mode, the root file system may be mounted
as read-only (read-only).
In that case, the passwd command will not be able to change the password.
Run:
mount -o remount,rw /
After the command completes, the file system should be writable.
If you do not remember the username exactly, you can look at the user directories:
ls /home
For example, the result may look like this:
admin
ubuntu
user
Suppose the required user is named:
ubuntu
Now run the command:
passwd ubuntu
Instead of ubuntu, specify the name of the required user.
The system will ask you to enter a new password:
New password:
Then you will need to enter the password again:
Retype new password:
If everything went well, you will see the message:
passwd: password updated successfully
After changing the password, you need to exit Recovery Mode and boot Ubuntu in normal mode.
You can run:
reboot
If the command does not work because of Recovery Mode specifics, you can use the corresponding reboot item in the menu.
After booting, try to log in with the new password.
If the SSH server is configured to use password authentication, try to connect:
ssh ubuntu@203.0.113.10
Enter the new password.
If login succeeds, check administrator privileges:
sudo -v
If necessary, you can check whether the user belongs to the
sudo group:
groups ubuntu
The result should include the sudo group if that is
what is used to grant administrator privileges.
This is an important point. Successfully changing the Ubuntu password does not mean that SSH necessarily allows password login.
The server may be configured for authentication exclusively by SSH key.
In that case, the command:
passwd ubuntu
will indeed change the user's password, but the connection:
ssh ubuntu@SERVER-IP
may still fail with an error.
In this situation, you need to check the SSH configuration:
/etc/ssh/sshd_config
In particular, pay attention to the parameter:
PasswordAuthentication
If password authentication is disabled, having a new password will not let you log in via SSH with a password.
If the task is specifically to set or change the password
of the root account, use the following from a running system with
administrator privileges:
sudo passwd root
After running the command, set the new root password.
You can check whether a root password is set directly from an administrative session, but the mere presence of a password does not mean remote SSH login as root is allowed.
For example, SSH may contain the restriction:
PermitRootLogin no
In that case, root login via SSH is prohibited regardless of the password that is set.
In some cases, Recovery Mode may be unavailable or may not work correctly. Then you can use emergency boot via GRUB by changing the Linux boot parameters.
This method requires especially careful handling, because it gives you a root shell even before the system starts normally.
In the GRUB menu, select the required Ubuntu entry and press:
e
Find the line that begins something like this:
linux /boot/vmlinuz...
You can add the following to the end of the line:
init=/bin/bash
Then boot the system with the modified parameters. Depending on the GRUB version, booting is usually done with the Ctrl+X key combination or the corresponding hint at the bottom of the screen.
After getting the root shell, the file system must be remounted as writable:
mount -o remount,rw /
Then you can change the password:
passwd username
When the recovery is complete, reboot.
If the Ubuntu system disk is protected with encryption, for example
LUKS, the passwd command alone is not enough to gain
access to the file system.
First, you need to successfully unlock the encrypted volume. Without the encryption key or passphrase, resetting the system password will not give you access to the encrypted data.
For servers with critically important data, you should check in advance where the recovery key is stored and how the disk is unlocked after a reboot.
If SSH is unavailable because of a forgotten password, you need an alternative access channel:
After gaining console access, you can use Recovery Mode or another suitable recovery method.
Reinstalling the operating system is an extreme measure that is usually not needed just because of a forgotten password.
When reinstalling, you may lose:
If the problem is only a forgotten password, you should first use the standard access recovery tools.
The new administrator password should be unique and sufficiently complex. It is not recommended to use:
123456;For servers, it is recommended to use a long unique password and, where possible, switch to SSH keys instead of permanent password authentication.
After a successful login, it is recommended to check the server's state.
whoami
groups
sudo -v
cat /etc/os-release
sudo systemctl status ssh
If the server is used to host a website, application, or database, after regaining access it is also recommended to check the state of the main services.
If you need to quickly recover the password of an Ubuntu administrative user via Recovery Mode, the sequence is as follows:
passwd username.For convenience, here are the main commands in one place:
# Remount the root file system as writable
mount -o remount,rw /
# List users
ls /home
# Change a user's password
passwd username
# Change the root password
passwd root
# Reboot the server
reboot
Resetting a forgotten administrator password in Ubuntu Linux usually does not require
reinstalling the operating system. If you have another
administrative user, the password can be changed directly
with the sudo passwd username command.
If access to the administrative account is completely lost,
the most convenient option for Ubuntu Server is to boot
into Recovery Mode, get a root shell,
remount the file system as writable, and run the
passwd command.
For a VDS, it is especially important to have a backup access channel in advance — for example, a VNC console. This allows you to regain control of the server even if SSH is unavailable.
After regaining access, it is recommended not to limit yourself to simply replacing the password. Check the SSH settings, administrator privileges, the SSH keys in use, and the overall security of the server.
The user account is opened immediately upon application.
You can start using the service immediately after payment.