Linux add to group command

Ubuntu Linux: Add a User To Group

Tutorial details
Difficulty level Intermediate
Root privileges Yes
Requirements useradd / usermod
Est. reading time 5 minutes

[a] useradd command – Create a new user or update default new user information or add a new user to secondary group. [b] usermod command – Modifies the system account and make changes to existing user accounts.

First, login as the root user

You must login as the root user. You can switch to the root user by typing ‘su –‘ and entering the root password, when prompted. However, sudo command is recommend under Ubuntu Linux for switching to root user:

Ubuntu Linux: add a new user to secondary group

Use the following syntax:

Create a group called foo and add user tom to a secondary group called foo:
$ sudo groupadd foo
$ sudo useradd -G foo tom
OR
# groupadd foo
# useradd -G foo tom
Verify new settings:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Finally, set the password for tom user, enter:
$ sudo passwd tom
OR
# passwd tom
You can add user tom to multiple groups – foo, bar, and ftp, enter:
# useradd -G foo,bar,ftp tom

Ubuntu Linux: add a new user to primary group

To add a user called tom to a group called www use the following command:

Ubuntu Linux: add a existing user to existing group

To add an existing user jerry to ftp supplementary/secondary group with usermod command using -a option

i.e. add the user to the supplemental group(s). Use only with -G option:

To change existing jerry’s primary group to www, enter:

For more information and options read the following man pages:
man 8 useradd man 8 usemod

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

For debian based system you can use adduser

#adduser tom group

Hi,
I want to create many users with same permission at Amazon EC2.
How can i do it?

im unable to login tothe new users created using “useradd” command.please help.

# useradd -G foo,bar,ftp top

should be
# useradd -G foo,bar,ftp tom
^
I

Hi Kiara,
You have to specify a password for the new user.

$passwd username hit on enter then try

Mis-spelled jerry in line:

usermod -g www jeryy

usermod -g www jerry

Thanks for the heads up!

It dos not work for me on debian Jessie. I can add the group but eaven if I added the group to the user the user cant acces to the folder …
usermod -a -G Group-name Username

permission on the folder are drwxrwsr-x and the user cant add annithing to this . So it fail It as been over 2h I’m playing with this with no succes .
more If I look for which group the user have I get withdifferent result:
in the Username shell the commande “groups” give me
Username
and the comande “groupes name1” give me
Username Group-name

Anny help will be welcom

I think you need to log out and log in again to gain the privileges that you have set via chmod.

Yup, logging out worked for me. (Actually I had to reset the VM anyway)

I don’t think this works; at least not any more. You have to use:
“usermod -a -G group username”

Источник

Linux Add User To Group Using Command-Line

  1. Primary user group. – It is the group that applied to you when login. Typically it is same as your login name. All of your process and files (including directories/folders) would have your primary group as the group membership. The primary group allows private group membership and security features. Your files or process cannot access by other group members or users on the Linux system.
  2. Secondary or supplementary user group – Users can be a member of other groups on the Linux system. It is useful for file sharing and other purposes. A sysadmin can fine-tune security too. For example, if you are a member of a secondary group called cdrom, you can mount and unmout cd-rom drive.
Tutorial details
Difficulty level Intermediate
Root privileges Yes
Requirements usermod/useradd
Est. reading time 5 minutes

How to add user to group in Linux

Please note that all user accounts related information are stored in the following files:

  1. /etc/passwd – Contains one line for each user account.
  2. /etc/shadow – Contains the password information in encrypted formatfor the system’s accounts and optional account aging information.
  3. /etc/group – Defines the groups on the system.
  4. /etc/default/useradd – This file contains a value for the default group, if none is specified by the useradd command.
  5. /etc/login.defs – This file defines the site-specific configuration for the shadow password suite stored in /etc/shadow file.

We do not modify these files by hand. Instead, we add a user to a group in Linux using various commands.

Linux command to add user to group

Open the terminal and then type:

  1. Add a new user called jerry to secondary group named cartoons on Linux:
    sudo useradd -G cartoons jerry
  2. Want to add a new user called tom to primary group called cartoons? Run:
    useradd -g cartoons tom
  3. We can add a existing user named spike to existing group named cartoons in Linux:
    useradd -g cartoons spike

How to become a root user

You must run all command as root user. To become a root user run:
su —
Alternatively use sudo command:
sudo -i

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Add a new user to secondary group using useradd

You need to the useradd command to add new users to existing group (or create a new group and then add user). If group does not exist, create it. The syntax is as follows:
useradd -G < group-name >username
In this example, create a new user called vivek and add it to group called developers. First, make sure group developers exists using grep command:
# grep «^developers» /etc/group
Sample outputs:

If you do not see any output then you need to add group developers using the groupadd command:
# sudo groupadd developers
Verify that user vivek does not exists:
# grep «^vivek» /etc/passwd
You should not see any outputs from above command. Finally, add a new user called vivek to group developers:
# useradd -G developers vivek
Setup password for user vivek:
# passwd vivek
Ensure that user added properly to group developers:
# id vivek
Sample outputs:

Please note that capital G ( -G ) option add user to a list of supplementary groups. Each group is separated from the next by a comma, with no intervening whitespace. For example, add user jerry to groups admins, ftp, www, and developers, enter:
# useradd -G admins,ftp,www,developers jerry

How to add a new user to primary group using useradd

How to add a existing user to existing group using usermod

Add existing user tony to ftp supplementary/secondary group with the usermod command using the -a option

i.e. add the user to the supplemental group(s). Use only with -G option:
# usermod -a -G ftp tony
In this example, change tony user’s primary group to www, enter:
# usermod -g www tony

usermod command options summary

Option Purpose
-a —append Add the user to the supplementary group(s). Use only with the -G option.
-g GROUP
—gid GROUP
Use this GROUP as the default group.
-G GRP1,GRP2 —groups GRP1,GRP2 Add the user to GRP1,GRP2 secondary group.

A note about security

If you add or delete user to existing group, you must change the owner of any crontab files or at jobs manually. You must make any changes involving NIS on the NIS server too.

A note about GUI tool

You will probably find the use of the GUI tool easy. KDE user can use KUser tool and the GNOME user can use users-admin:

users-admin is part of the GNOME system tools, a set of tools to easily access and manage system configuration

One can easily add users as follows from the gnome 3 settings on a Debian/Ubuntu Linux:

Fedora/RHEL/CentOS user can use system-config-users command as follows
# system-config-users
$ ssh -X -t vivek@server1.cyberciti.biz sudo system-config-users
Sample outputs:

Click on the Groups tab to add or view groups:

Summing up

You learned how to add a new or existing user to group on Linux operating systems. For more information type the following command at the shell prompt to read man pages:
$ man usermod
$ man useradd

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Linux: Add User to Group

This tutorial shows you step by step how to add a user to a group on Linux with several examples using the Linux command-line. It also explains how to add users and groups on Linux. The commands should work on any Linux Distribution and have been tested on CentOS, Debian, and Ubuntu.

Add a new Linux User to a Group

A Linux user can have one primary group and one or more secondary groups. The groups can be set as parameters of the adduser command when you create the user.

All commands have to be executed as the root user. On Ubuntu, please prepend the word «sudo » to all commands or run «sudo -s» to become root user.

Add Linux Group

As a first step, I will add a new group named «family» and a second group «friends»:

Add User to Group

Then I will add a new user «tom» to our group «family». The family group will be added as a secondary group by using the -G parameter.

Add User to two groups

Tom is now a member of the family group. The -G parameter allows it to list several groups separated by a comma. To add the user tom into the family and friends group, use this command:

Set User password

Please note that our new Linux user tom has no password yet, so he can’t login. To set a password for this user, run:

And enter the new password twice when the command requests it.

In the above example, we added the user tom to a secondary group, the adduser command has created a new primary group with the name of the user automatically and assigned this group as a primary group.

  • Username: tom
  • Primary Group: tom
  • Secondary Group: family (or family + friends if you followed the second example)

Set new primary group

Maybe you want that tom gets family as his primary group and friends as his secondary group? Then use this command instead:

to create the user tom. the -g switch tells the useradd command to use family as the primary group. There is no group tom in this case.

Use the man (manpage) command to get a detailed description of all command-line options for useradd:

Add an existing Linux User to a Group

For this task, we will use the usermod command. Usermod allows it to modify various options of the user including the group memberships of the User.

First I will add a third group with the name colleagues.

Usermod command example

I will add the colleagues group as the secondary group to the user tom.

The command explained: The -a switch stands for «append«, it is used in combination with the -G switch (that stands for the secondary group) only. The result is that we add tom to the group colleagues and this group is an additional or secondary group for the user.

The -G option allows it to add several groups at once by listing the group names separated by a comma. e.g.: «-G group1,group2,group3».

To change the primary group of the user tom to family, run:

Use man (manpage) command to get a detailed description of all command-line options for usermod:

Источник

Читайте также:  Canon l11121e драйвера astra linux
Оцените статью