Change users group in linux

How to change primary group

Now I want to remove the user again from group apache, but I get same error again. How can I delete pserver from the primary group apache ?

3 Answers 3

The usermod option -G adds supplementary groups. You want option -g to change the primary group. I.e. your command should have been:

# usermod -g pserver pserver 

Note, this will also change group ownership of files in the home directory, but not elsewhere.

More generally, the syntax for changing user ‘user’ to have primary group ‘group’ is:

Not clear in this command which is the user and which is the group, since in this unique circumstance they’re both identical, but it frequently won’t be that way.

Late, but a bit clear (at least for me);

The question as placed by the OP is misleading. The title reads «How to change primary group», however in the question body its clear the intent is to not just replace the primary group but also remove the previous primary.

In case anyone wants to just replace the primary but not remove it from the list, one simply has to add the previous primary back to list.

So, after following StarNamer’s post, one only needs to do a

adduser user previous-primary-group 

because the usermod command will have thrown out your previous primary group from the list of groups you belong to.

Источник

Safely change primary group of a user in Linux

Change User Group Linux

When it comes to user permission even a system administrator sometimes gets confused. No matter what linux training or administration training courses one has had one mistake and sever management can become a hell. Same holds true even for a Linux user at home. One has to be extremely careful while changing the primary group of any existing user. If you do some research, you will find that several people accidentally locked themselves out of being able to sudo. In order to avoid these unnecessary headaches it is essential that you change primary group of a user in linux, correctly.

Change Primary Group Of Linux User

Record the Current State

Before you change primary group of a Linux user, make a note of the user’s current status. Let us consider a user by the username «mackey». You can find this users current group memberships like this:

$ groups $ mackey,adm,cdrom,sudo,dip,plugdev,sambashare,lpadmin

The user mackey’s primary group is «mackey». This is user is also associated with a bunch of other groups: adm,cdrom,sudo,dip,plugdev,sambashare,lpadmin.

Читайте также:  Linux где файл подкачки

Note that adm, cdrom, sudo, dip, plugdev, sambashare, and lpadmin are all default secondary groups assigned to a user in Ubuntu 12.04 Precise Pangolin.

Change Primary Group of User

The command to use is «usermod». Below is the syntax of the command:

usermod - Modify a user account Syntax: usermod [-c comment] [-d home_dir [-m]] [-e expire_date] [-f inactive_days] [-g initial_group] [-G group [. ]] [-l login_name] [-p passwd] [-s shell] [-u uid [-o]] [-L|-U] user Options: -d home directory -s starting program (shell) -p password -g (primary group assigned to the users) -G (Other groups the user belongs to)

If you search the internet you may find that you can change the primary group using the following command:

sudo usermod -g NewPrimaryGroup mackey

As expected the above command would change mackey’s primary group from «mackey» to «NewPrimaryGroup». However, all of this user’s secondary group memberships will be gone, including «admin». This means that the user will henceforth be not able to use the «sudo» command to gain root access. This user will not be able to do any changes to the system (eg. install/remove softwares).

This, however, does not happen in all distributions. For example, in Ubuntu 12.04 Precise Pangolin «sudo usermod -g NewPrimaryGroup user» command assigns the new primary group to the user keeping all existing secondary groups. Yet, I like to change primary group the safe way, which puts control in my hand.

The safe way to change primary group of a Linux user is using this following command:

sudo usermod -g NewPrimaryGroup -G mackey,adm,cdrom,sudo,dip,plugdev,sambashare,lpadmin mackey

This command will first change the user mackey’s primary group from «mackey» to «NewPrimaryGroup». Then it will assign the user «mackey» to the following secondary groups «mackey,adm,cdrom,sudo,dip,plugdev,sambashare,lpadmin». This way mackey’s primary group will be changed without affecting his other group memberships or losing his membership to his previous primary group «mackey».

Recommended Guides on Linux Commands:

How to Fix In Case of a Screw Up

If you change a user’s primary group with just the «-g» option and all of his/her secondary group memberships are gone, then you can fix this by couple of different ways.

Method 1: If you had setup a «root» password previously (not normal in Ubuntu) you can login as «root» and then run the following command:

sudo usermod -g NewPrimaryGroup -G mackey,adm,cdrom,sudo,dip,plugdev,sambashare,lpadmin mackey

After running the above command, logout and login as the user (mackey in this case).

Method 2: If you did not setup a password for the user «root» then you cannot login as a root user. In this case, from grub menu boot into recovery and enter command line with root previleges. Then run the same command listed above:

sudo usermod -g NewPrimaryGroup -G mackey,adm,cdrom,sudo,dip,plugdev,sambashare,lpadmin mackey

Reboot and the user «mackey» should now have all the secondary groups restored. The primary group will be «NewPrimaryGroup».

If you simply want to add an existing user to a secondary group, retaining all his/her primary and secondary group memberships, then use the following command:

usermod -a -G NewSecondaryGroup user

The «-a» option means append to existing memberships.

Читайте также:  Системы linux для начинающих

There you go, now you can change primary group of users safely. Hope this helps.

Источник

How to Manage Users and Groups on Ubuntu 22.04

Linux is a multi-user and multi-tasking operating system. User and group management are the two most important tasks to be performed by Linux administrators.

In Linux, each user has their own login name and a home directory. Every user belongs to a primary group, and users can be added to multiple secondary groups. All users in the group will have the same group permission on files and folders. This makes it easier to provide permission for multiple users.

This tutorial will demonstrate how to manage users and groups in the Linux system.

A Quick Overview

The command-line tools to manage the users and groups in Linux are:

adduser / useradd : To add a user

addgroup / groupadd : To add a group

usermod : To modify a user account

deluser / userdel : To delete a user

delgroup / groupdel : To delete a group

passwd : To change the user’s password

We will cover the practical examples of all commands in this article. To follow the tutorial, you will need to switch to the root user or any user with sudo privileges.

1. Create a new user

You can add a new user to the system using the adduser command. The following command creates a new user henry in the system.

It will prompt you to enter the password for the new user and other user details.

adduser command to create a new user in linux

To verify the user, you can try to log in as a new user.

log in as another user in linux

2. Understanding the /etc/passwd file

The /etc/passwd is a plain text file that stores the user account information in Linux. You can use the cat command to view the content of /etc/passwd .

view the content of a etc passwd file in linux

Each user has one entry per line. The fields are separated by a colon : symbol and contains the following information.

username:password:UID:GID:GECOS:home_directory:shell

The new entries are saved at the end of a file. To find a user henry , you can see the last entries. Alternatively, you can use the grep command.

search for a specific user in a passwd file

3. Change the login name of a user

You can use the usermod command to change a user’s login name in Linux. This command renames the user henry to james .

$ sudo usermod -l james henry

usermod command to change the login name of a user

As you can see, the username is changed to james .

4. Change the user ID of a user

By default, the system automatically sets the next available UID when creating a user. The usermod command with -u flag can be used to change the UID of a user.

The following command changes the user ID of james to 4567 .

$ sudo usermod -u 4567 james

usermod command to change the uid of a user in linux

5. Change the group of a user

The -g option with usermod command changes the primary group of a user. For example, to change the primary group of a user james to linuxwizardry , you can run this command.

$ sudo usermod -g linuxwizardry james

The specified group must already exist in the system.

usermod command to change the group of a user

In Linux, a user can have only one primary group. But you can assign a user to multiple secondary groups. The -G flag allows you to specify the secondary group for a user.

Читайте также:  Создать свой образ линукс

The following command adds a user james to the group ubuntu .

$ sudo usermod -G ubuntu james

usermod command to add a user to the secondary group

6. Add a user to the sudoers group

You can add a user to the sudoers group and provide sudo privileges to that user. This command adds a user james to the sudo group.

$ sudo usermod -aG sudo james

The -a option adds a user to the group without removing the current group.

Next, log in as a user james and run the sudo command to confirm.

add a user to the sudoers group

7. Change the password of a user

The passwd command is used to change the user’s password in Linux. The following command changes the password of a user james .

passwd command to change user password in linux

8. Delete a user

When the user account is not needed, you might want to delete it from the system. The userdel command helps to remove a user in Linux.

The below command deletes a user james from the system.

userdel command to delete a user in linux

9. Delete a home directory of a user

The usedel command without any flags only removes a user. It does not delete the home directory of a user in the /home directory.

To delete a user along with its home directory, you can use:

10. Add a new group

You can add a new group to the system using the groupadd or addgroup command. The following example creates a new group computer on the system.

addgroup command to add a group in linux

11. Understanding /etc/group file

The /etc/group file stores the group details in a list. Each entry contains the following group information for each group.

group_name:group_pwd:group_id:group_list

You can display the entries in /etc/group file with the cat command.

display the entries in etc group file in linux

12. Create a system group

If you need to add a new system group, you can use the -r flag with the groupadd command. This command creates a new system group sysmin .

groupadd command to add a system group in linux

Output:

13. Add a new group with specific GID

When creating a new group, the system assigns the next available group ID by default. You can change this behavior and specify a GID for a new group with the -g flag.

The following command creates a new group bank with a custom group ID 644 .

add a group with a specific id in linux

Output:

14. Remove a user from the group

Sometimes you might need to remove a user from the secondary groups. You can do it by specifying the username and group to the deluser command.

The below command removes a user rohan from the group ubuntu .

$ sudo deluser rohan ubuntu

removing a user from the group in linux

Output:

15. Delete a group

You can remove a group from the system using the delgroup or groupdel command.

To delete a group ubuntu , run the following command.

delgroup command to delete a group in linux

Output:

If the specified group is the primary group of any user, it cannot be deleted. You must first change the primary group of a user.

Conclusion

Managing users and groups is one of the essential skills for every Linux administrator. You have learned the different examples of user and group management commands in Linux. Now you know how to perform the tasks like creating new users and groups, adding users to groups, changing the username and password, deleting users and groups, and much more.

We hope you found this article helpful. Please let us know if you have any confusion about any examples in the comment section below.

Источник

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