Add user to group linux usermod

How to add existing user to an existing group?

I want to add the Apache user ( www-data ) to the audio group. I’ve read the man page for useradd , but I’m not having any luck. I’m running xubuntu 11.10. Here’s what I’m doing:

$ sudo useradd -G audio www-data useradd: user 'www-data' already exists 
$ sudo useradd audio www-data Usage: useradd [options] LOGIN Options: -b, --base-dir BASE_DIR base directory for the home directory. 

8 Answers 8

The useradd command will try to add a new user. Since your user already exists this is not what you want.

Instead: To modify an existing user, like adding that user to a new group, use the usermod command.

sudo usermod -a -G groupName userName 
  • The -a (append) switch is essential. Otherwise, the user will be removed from any groups, not in the list.
  • The -G switch takes a (comma-separated) list of additional groups to assign the user to.

In general (for the GUI, or for already running processes, etc.), the user will need to log out and log back in to see their new group added. For the current shell session, you can use newgrp :

sudo usermod -a -G [group-name] [user-name] : Just a quickie for those who only glance at the answer after reading the headline

I think the preferred way now is sudo adduser user group . It is simpler and cleaner syntax. See the response from @Bai.

Is there a way to get around the «logout and back in» part? Some type of update-groups command, maybe?

@con-f-use, if you can run sudo login -f YOURUSERNAME , it will start a new shell session. Use the id command to verify that the new session is in the correct set of groups. However, this isn’t «global» — it doesn’t apply to terminals that are already open. So, really, logging out is the best option, where possible

You can use the newgrp [group-name] command to add a new group to the user of the current session without logging out and in again. Only the current session will be affected though.

Читайте также:  Linux remote desktop control

Removing a user from a group:

@Tejendra’s comment seems to presuppose that using useradd is desirable/mandatory to answer OP’s question; Perhaps this answer is just missing words to indicate that adduser/deluser is a better alternative.

After adding to a existing user:

You may need to logout and login to get the groups permissions from /etc/group .

FYI: I think the id command should indicate you were added to the group without needing to exit. id myuser

sudo gpasswd -a myuser mygroup 

I’m posting this as an answer because I don’t have enough reputation to comment. As @dpendolino’s mentioned, for the effects of this command to persist:

sudo usermod -a -G groupName userName

. you need to logout/login again.

However, if you need a shortcut to start using your new group membership immediately (and you have the correct sudo privileges) I have found this work-around:

$ sudo su - # su [userName] $ groups 
  • sudo su — will give you a root shell
  • su [userName] returns you to a shell with your user
  • groups when run now will show the group you added with the usermod -aG command

In my case I was trying to add the ‘docker’ group to my user

$ groups userName adm cdrom sudo dip plugdev lpadmin sambashare wireshark lxd 
$ groups userName adm cdrom sudo dip plugdev lpadmin sambashare wireshark lxd docker 

Another option to apply the group membership is sudo login -f , as per Aaron McDaid’s comment on another answer.

sudo usermod -a -G groupName userName 

will work just fine, but I had to reboot entirely, just log out and log in again did not do the job.

On Ubuntu, since logging in as root is not enabled, users in the sudo group can elevate privileges for certain restricted commands. Any restricted command must be prepended with sudo to elevate privilege.

sudo usermod -a -G group user

will add the existing user user to a supplemental group named group . The user’s primary group will remain unchanged.

To permanantely add a user to a group, run this command:

Then log out and log in again (or reboot if necessary).

Explanation:

The usermod command will modify the /etc/group file to list the specified username in the line relevant to that group. If you run grep /etc/group , you should see your username in the output.

In Linux, every process is assigned to a user and groups, and any child processes inherit the user and groups of the parent process. So, for this change to take effect and propogate, you need to log out and log in again (at which point /etc/group is read and used). On Wayland, I’ve found that a full reboot is necessary.

Читайте также:  Ssh in script linux

If you run the command groups , you can see if the current process has the expected groups or not.

Источник

How to Add a User to a Linux Group

In Linux, a group is a unit in which you can manage privileges for several users simultaneously. Linux groups allow you to manage multiple user permissions quickly and easily.

In this tutorial learn how user groups work in Linux, and how to add users to specific groups.

Tutorial on how to add a user to a group on Linux.

  • A system running Linux
  • A user account with sudo or root privileges
  • Access to a terminal window/command line (Ctrl-Alt-T, Ctrl-Alt-F2)

What is a User Group in Linux

In Linux, different users have different roles or responsibilities. Some users might need the ability to execute applications, while others are restricted from accessing specific files and folders.

Groups let you create categories of users with pre-set permissions. Instead of managing permissions for each user account, you can simply add a user to a group to grant the appropriate permissions.

Primary Group

The primary group is set to the logged-in user. Any files the user creates are automatically added to that group. A user can only belong to one primary group at a time. A primary group with the same name as the user is created, and any files created by the user are included in that group.

Secondary Groups

A user can belong to any number of secondary groups (including none). Secondary groups are created to manage individual files and software applications. Members of the group inherit the read, write, and execute privileges for that group.

Note: Refer to our in-depth guide on how to create users in Linux for more info on user management.

How to Create a User Group

1. To create a new group, enter the following:

2. Replace new_group with the name you want for your new group.

create a user group in linux

How to Add User to Group

Add an Existing User to an Existing Group

1. Use the adduser command to add a user to a group:

sudo adduser user_name new_group

add existing user to an existing group

2. Use the useradd command to add a user:

sudo useradd –G new_group user_name

3. You can also use the usermod command to add a user to a group:

sudo usermod –a –G group_name user_name

The usermod command uses the –append and –group options to append the user to a particular group. Without using –append , the user could be dropped from other groups.

Add a User to Multiple Groups at Once

Use the usermod command to specify multiple groups to add to:

sudo usermod –a –G new_group,new_group2,new_group3 user_name

Create a User and Add to Group

1. This is useful for creating a new user on the fly for a specific software application. Enter the following:

sudo useradd –G new_group new_user

2. Next, assign a password to the new user:

Читайте также:  Linux checking file system failed

Change a Users Primary Group

All previous commands have been used to manage the secondary groups a user belongs to. In most cases, a user’s primary group is the same as their username.

To change a users primary group, enter the command:

sudo usermod –g new_group user_name

The lower-case –g specifies the primary group. (Upper-case –G refers to a secondary group.) A user can only have one primary group, so the old primary group user_name won’t be primary anymore for this user.

How to Remove a User From a Group

The gpasswd tool is used for managing groups. To remove a user from a group:

sudo gpasswd –d user_name new_group

Note: The gpasswd tool can also be used for other administrative tasks such as defining group administrators and setting a password for access to group resources. Use the Linux man command man gpasswd for details.

Delete a Group

To delete a group, use the command:

example of deleting a user group in linux

How to List Groups in Linux

Linux comes with several different groups by default. Some of these, like the sudo group, can be used to grant permissions. Others are hidden, used for system tasks.

1. To view a list of groups on your system by displaying the /etc/groups file:

2. To display the groups that a user belongs to with the groups command:

find groups of logged-in user

3. The image above shows the groups that the logged-in user ‘sofija’ belongs to. You can display groups for a different user by specifying the username:

4. Another method to display the groups a user belongs to, including user ID (uid) and group ID (gid), is to use the id command:

Other Common Groups

There are a several common group names you might encounter in Linux:

  • sudo – A member of this group can use the sudo command to elevate their privileges
  • wheel – This is an older method of granting sudo-like privileges
  • cdrom – Allows the user to mount the optical drive
  • adm – Allows the user to monitor Linux system logs
  • lpadmin – Allows the user to configure printers
  • plugdev – Allows the user to access external storage devices

You should now have a good understanding of Linux groups and how to add and remove members from those groups. For more information on specific commands, you can enter the man command to display a manual in your terminal window.

Источник

Оцените статью
Adblock
detector