500

I'm using a Docker image which was built using the USER command to use a non-root user called dev. Inside a container, I'm "dev", but I want to edit the /etc/hosts file.

So I need to be root. I'm trying the su command, but I'm asked to enter the root password.

What's the default root user's password inside a Docker container?

1
  • 49
    just exec as a root: docker exec -u 0 -it mycontainer bash. (see H6's answer) Feb 28, 2019 at 19:07

21 Answers 21

876

You can log into the Docker container using the root user (ID = 0) instead of the provided default user when you use the -u option. E.g.

docker exec -u 0 -it mycontainer bash

root (id = 0) is the default user within a container. The image developer can create additional users. Those users are accessible by name. When passing a numeric ID, the user does not have to exist in the container.

from Docker documentation

Update: Of course you can also use the Docker management command for containers to run this:

docker container exec -u 0 -it mycontainer bash

10
  • 8
    It would be nice to specify that you need mycontainer up and running at the moment when you type the above commad. it works using 2 different terminals: one for mycontainer and the other for this command. Otherwise It requires that mycontainer is running as detached.
    – nicolimo86
    Nov 5, 2016 at 23:59
  • 20
    for images, use docker run -u 0 -it mycontainer bash Apr 27, 2017 at 15:42
  • 4
    this is the answer that should be on top
    – D Pinto
    Feb 23, 2018 at 16:26
  • 2
    @High6, when you say "You can log into the Docker Image...", I think you mean "You can log into the Docker Container". Jun 25, 2018 at 1:05
  • 1
    Great answer! Thank you. This should be top.
    – sprut
    Mar 19 at 20:55
151

Eventually, I decided to rebuild my Docker images, so that I change the root password by something I will know.

RUN echo 'root:Docker!' | chpasswd

or

RUN echo 'Docker!' | passwd --stdin root 
6
  • 7
    I tried this but it does not work on my CentOS 6 based docker. Does this command work on CentOS based docker? Mar 8, 2016 at 7:59
  • Why doesn't it ask for the password twice?
    – Gulzar
    Nov 25, 2021 at 13:51
  • 1
    can you explain what your commands do? Sep 15, 2022 at 17:58
  • @CharlieParker both the commands are intended to pass a password for root user to the change password command using a pipe. Although both the options didnt work for me while trying to gain root access to my solr docker container.
    – Gautam
    Jun 8, 2023 at 4:55
  • It looks like this solution is no longer valid. Tested and failed with Ubuntu 23.04 as base image. In practice my container OS default shell requires root login. At the prompt I enter root password twice from stdin and get thrown when I provide the correct password, i.e. the password as specified above in the corresponding image's dockerfile.
    – Cbhihe
    Jul 14, 2023 at 17:28
70

There are a couple of ways to do it.

  1. To run the Docker overriding the USER setting

     docker exec -u 0 -it containerName bash
    

or

docker exec -u root -it --workdir / <containerName> bash
  1. Make necessary file permissions, etc., during the image build in the Docker file

  2. If all the packages are available in your Linux image, chpasswdin the dockerfile before the USER utility.

For complete reference: http://muralitechblog.com/root-password-of-a-docker-container/

0
65

To create/change a root password in a running container

docker exec -itu 0 {container} passwd
4
  • 5
    You save my bacon Apr 19, 2021 at 22:11
  • 2
    This one should be an accepted answer!
    – zer0hedge
    Oct 19, 2021 at 12:09
  • The others are close, but this one actually has the meat.
    – Spokes
    Jun 6, 2022 at 23:55
  • 3
    Note: passwd is not your password. Literally, you have to type passwd, then it asks for your password. Apr 13, 2023 at 12:50
28

I am able to get it working with the below command.

root@gitnew:# docker exec -it --user $(username) $(containername) /bin/bash
25
docker exec -u 0 -it containername bash
25

Get a shell of your running container and change the root pass.

docker exec -u 0 -it <MyContainer> bash

root@MyContainer:/# passwd
Enter new UNIX password: 
Retype new UNIX password: 
2
  • How can the OP change the password when they don't know what it is?
    – MEMark
    Nov 15, 2021 at 15:03
  • 3
    @MEMark if you are root you are not asked about current password...
    – DimiDak
    Nov 15, 2021 at 17:36
20

I had exactly this problem of not being able to su to root because I was running in the container as an unprivileged user.

But I didn't want to rebuild a new image as the previous answers suggest.

Instead I have found that I could access the container as root using 'nsenter', see: https://github.com/jpetazzo/nsenter

First determine the PID of your container on the host:

docker inspect --format {{.State.Pid}} <container_name_or_ID>

Then use nsenter to enter the container as root

nsenter --target <PID> --mount --uts --ipc --net --pid
4
  • 1
    Using boot2docker, I had to use sudo nsenter --target <PID> --mount --uts --ipc --net --pid
    – peater
    Jun 29, 2015 at 20:57
  • Yes, good point. You generally need root permissions to execute docker commands and I guess nsenter is the same. I didn't make this totally clear in my answer. Jun 30, 2015 at 10:27
  • 5
    Since this answer was written, docker has added the command exec to do basically the same thing as nsenter, but easier and cleaner. Just a data point for those who now find this question through search. The command is "docker exec -it <containername> <command>" (command is usually /bin/bash, but you can of course do whatever you want). Nov 21, 2015 at 18:08
  • Also, docker has special treatment for hosts (and resolv.conf). You should not manually edit them; docker recreates /etc/hosts on every start to reflect linked containers and the like. Nov 21, 2015 at 18:11
11

You can SSH in to docker container as root by using

docker exec -it --user root <container_id> /bin/bash

Then change root password using this

passwd root

Make sure sudo is installed check by entering

sudo

if it is not installed install it

apt-get install sudo

If you want to give sudo permissions for user dev you can add user dev to sudo group

usermod -aG sudo dev

Now you'll be able to run sudo level commands from your dev user while inside the container or else you can switch to root inside the container by using the password you set earlier.

To test it login as user dev and list the contents of root directory which is normally only accessible to the root user.

sudo ls -la /root

Enter password for dev

If your user is in the proper group and you entered the password correctly, the command that you issued with sudo should run with root privileges.

10

To avoid having to rebuild your container do the following steps:

  1. docker exec -u 0 -it containerName bash
  2. sudo visudo
  3. add the following to the end of the file:
your_username ALL=(ALL:ALL) ALL

In my case I use ubuntu as my username so it would like like this:

ubuntu ALL=(ALL) NOPASSWD: ALL

save the file, re-attach to your docker container, and sudo to your hearts continent.

Bonus: If you want to update the docker image you can then just do the following:

docker commit containerName NewImage
9

The password is 'ubuntu' for the 'ubuntu' user (at least in docker for ubuntu :14.04.03).

NB: 'ubuntu' is created after the startup of the container so, if you just do this:

 docker run -i -t --entrypoint /bin/bash  ubuntu     

You'll get the root prompt directly. From there you can force the password change of root, commit the container and optionally tag it (with -f) to ubuntu:latest like this:

root@ec384466fbbb:~# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@ec384466fbbb:~# exit

% docker commit ec3844
5d3c03e7d6d861ce519fe33b184cd477b8ad03247ffe19b2a57d3f0992d71bca

docker tag -f 5d3c ubuntu:latest

You must rebuild your eventual dependencies on ubuntu:latest.

5

I'd suggest a better solution is to give the --add-host NAME:IP argument to docker run when starting the container. That will update the /etc/hosts/ file without any need to become root.

Otherwise, you can override the the USER setting by giving the -u USER flag to docker run. I would advise against this however, as you shouldn't really be changing things in a running container. Instead, make your changes in a Dockerfile and build a new image.

3
  • I need to add entries in the hosts file while the container is running.
    – guillaume
    Feb 26, 2015 at 9:21
  • I also need to install new package, but I can't as I'm not root.
    – guillaume
    Feb 26, 2015 at 9:32
  • 1
    You can use the -u flag to change user. I don't think you can do it from inside the container. Feb 26, 2015 at 9:45
3

You can use the USER root command in your Dockerfile.

3

When you start the container, you will be root but you won't know what root's pw is. To set it to something you know simply use "passwd root". Snapshot/commit the container to save your actions.

3

In some cases you need to be able to do things like that under a user with sudo (e.g. the application running in the container provides a shell to users). Simply add this into you Dockerfile:

RUN apt-get update         # If necessary
RUN apt-get install sudo   # If your base image does not contain sudo.
RUN useradd -m -N -s /bin/bash -u 1000 -p '$1$miTOHCYy$K.c4Yw.edukWJ7z9rbpTZ0' user && \
    usermod -aG sudo user  # Grant sudo to the user
USER user

Now under the default image user user you will be able to sudo with the password set on line 3.

See how to generate password hash for useradd here or here.

3

Some of the answers above were good, especially those like:

docker exec -u root -it CONTAINERID /bin/bash

where you get your CONTAINERID from the first column of the answer to:

docker ps

This makes you root, and you can do anything you want. But only if the command exists in your container. In order to do something as simple as changing the root password (as many people above have suggested), I had to turn off my VPN and do:

yum install -y passwd

While I was there, I installed vim and sudo in case I needed it in the future.

Just a note: passwd won't let you get away with easy passwords.

2

try the following command to get the root access

$ sudo -i 
3
  • This is unlikely to work unless you have the account in the container setup with sudo access. This would be an unusual situation and break the point of having a non-root user running the container.
    – Josiah
    Jan 17, 2020 at 14:17
  • Anyone know the DEFAULT su password for Docker install? I tried to run elevation and there IS one set. May 7, 2020 at 20:52
  • This worked in WSL2 after exporting the docker image and importing into WSL2. From there, just do passwd to set the password for root. Exit back to non-root user and try sudo su or su. It will now work.
    – WSLUser
    May 29, 2020 at 18:38
2

By default docker containers run as the root user.

If you are still using the container you can use exit command to get back to root (default user) user instead of running the container again.

Example -

[dev@6c4c86bccf93 ~]$ ls
[dev@6c4c86bccf93 ~]$ other-commands..
[dev@6c4c86bccf93 ~]$ exit
[root@6c4c86bccf93 /]# ls
1

You may also use it like this (if you need to confirm the password)

(echo 'myrootpassword'; echo 'myrootpassword') | passwd root
1

There are two different scenarios here:

First

  1. I created a user "rohit"
  2. Now I executed su - rohit
  3. Now I am currently logged-in as rohit
  4. I just want to become root again.
  5. Solution: type exit and enter

To go back to root user inside docker container from any other user.

  • type exit and enter.

enter image description here

Second:

  1. I created docker container without root password
  2. now I need password for root
  3. Solution: open container bash, execute passwd command and set password for root

enter image description here

Now I can use this password for root that I just set anywhere in the container but it won't work for ssh directly.

-2

Setting a fixed root password in a docker container can compromise systems, and so shouldn't be used. Instead you might use:

 docker exec -itu 0 CONTAINER_ID bash

whenever you want root access to the container, while the container is up and running.

The above command assumes you want to run bash as your shell. I don't use MS Windows, but I'd guess that you might try CMD or CMD.EXE instead of bash if you're on a MS Windows machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.