How to create or delete user in Linux

  • To create a new user named joe:
$ sudo useradd joe

The above command does not create a home directory for the new user.

  • To create a new user named joe and create a home directory for him:
$ sudo useradd -m joe
  • To delete a user named joe:
$ sudo userdel joe

The above command does not remove the files owned by the user.

  • To delete a user named joe and remove all the files owned by him:
$ sudo userdel -r joe

Tried with: Ubuntu 16.04


How to set user of Docker container


When you run a Docker container using docker run, everything inside the container is executed by the root user and root group. Its UID is 0 and GID is 0. This can sometimes be a problem.

For example, I had mounted a directory from the host filesystem into the Docker container using the --volume option. When root creates a new file or directory in this mounted directory, it appears as owned by the user nobody and group nogroup. This was a problem since I wanted these new files and directories to be created with my username or at least my group.


You can set what username or group you want to run as inside a container by using the --user option.

  • To run as user joe: --user joe. It is highly unlikely that the username joe exists in the Docker container. So this will likely fail unless you have added Dockerfile commands to make this so.

  • Instead, set the UID: --user 1005. Docker will warn that the UID does not have a corresponding username inside the container, but it will work. Files created on the mounted directory will have the UID 1005. There is a problem: many programs inside the container may not run if root is not running it. For example, you may find that you cannot create files or directories inside the container without being root.

  • Another option is to set the group or GID: --user :1005. Notice the colon. Files created on mounted volumes will have the group GID you set. This solution worked to solve my problem.

  • You can set both UID and GID too: --user 1005:9999

Tried with: Ubuntu 16.04

How to add user to group in Linux

  • To add an existing user JoeUser to an existing group FooGroup in Linux:
$ sudo adduser JoeUser FooGroup
  • This command can be used, for example, to add an user to the sudo group and make it a sudoer:
$ sudo adduser JoeUser sudo
  • If the adduser program is not available, you can achieve the same with the classic usermod program:
$ sudo usermod -a -G FooGroup JoeUser

Tried with: Ubuntu 16.04