Root password inside a Docker container

前端 未结 16 704
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 03:24

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

16条回答
  •  独厮守ぢ
    2020-12-02 04:08

    You can SSH in to docker container as root by using

    docker exec -it --user root  /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.

提交回复
热议问题