I can connect to my EC2 instance running Linux through SSH using my private key, and I can run privileged commands using sudo, but my instance doesn't have a root password. How do I get one?

For security purposes, it's a best practice to avoid root passwords, but if you need one (for example, a software installer requires the root password), you can temporarily create one by granting a user root permissions and manually creating a password for the root user.

When you launch an EC2 instance running Linux, you associate an SSH key pair with it, and you use the private key to authenticate and log in to the instance by using SSH. The default user name for the instance depends on the OS (for example, “centos” for CentOS, “ubuntu” for Ubuntu Linux, and "ec2-user” for most other Linux distributions).

After you log in using the user ID and password for your instance, you can execute privileged commands using sudo as a prefix before each command, as in the following example:

$ sudo mount /dev/sdf1/localfolder

You can also assume root user permissions by running the sudo su command.

By default, the sshd daemon is configured to refuse direct connections by the root user, so you won't be able to log in over SSH as a root user with this password. For security reasons, avoid enabling direct SSH access for the root user. Instead, connect by using the user ID associated with your operating system (for example, "ec2-user" for many Linux distributions) and a key pair.

If you need to add a root user password temporarily:

1.    Connect to your EC2 instance running Linux by using SSH.

2.    Assume root user permissions by running the following command:

$ sudo su

3.    Next, run the following command to create a password for the root user:

# passwd root

4.    When prompted, enter your temporary root password, and then enter it again to confirm it.
Note: This command must be run as the root user.

After you finish the task that requires a temporary root password, delete the root password by running the following command:

# passwd –l root

EC2, Linux, root, password

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2016-1-27