Recheck using below command:
[root@rhel1 ~]# id foo uid=511(foo) gid=511(foo) groups=511(foo),509(sales) [root@rhel1 ~]#
Above output confirms that sales is the secondary group whereas primary group is foo only.
- Linux add user to group while creating user with multiple secondary groups.
You can specify multiple secondary group using comma while creating the user itself in useradd command.
[root@rhel1 ~]# useradd -G sales,accounts,marketing bar
Confirm using below command:
[root@rhel1 ~]# id bar uid=512(bar) gid=513(bar) groups=513(bar),509(sales),510(accounts),512(marketing) [root@rhel1 ~]#
Also there is one more way for confirmation:
[root@rhel1 ~]# cat /etc/group|grep bar sales:x:509:foo,bar accounts:x:510:bar marketing:x:512:bar bar:x:513: [root@rhel1 ~]#
- Linux add user to group for existing user.
You can also change secondary group for existing users using “-G” option for usermod command. Let’s say you want to change the secondary group of the user foo from sales to accounts.
[root@rhel1 ~]# usermod -G accounts foo [root@rhel1 ~]# id foo uid=511(foo) gid=511(foo) groups=511(foo),510(accounts) [root@rhel1 ~]#
In case you want to add one more secondary group instead of changing it as above then you must use “-a” (append) option.
[root@rhel1 ~]# usermod -a -G marketing foo [root@rhel1 ~]# id foo uid=511(foo) gid=511(foo) groups=511(foo),510(accounts),512(marketing) [root@rhel1 ~]#
- Linux add user to group for existing user with multiple secondary groups.
You can specify multiple secondary groups using comma for existing user with command in usermod command.
[root@rhel1 ~]# usermod -a -G sales,hr foo [root@rhel1 ~]# id foo uid=511(foo) gid=511(foo) groups=511(foo),509(sales),510(accounts),512(marketing),514(hr) [root@rhel1 ~]#
In case you don’t specify -a option then current secondary groups will be flushed and replaced by specified group names.
[root@rhel1 ~]# id foo uid=511(foo) gid=511(foo) groups=511(foo),509(sales),510(accounts),512(marketing),514(hr) [root@rhel1 ~]# usermod -G sales,hr foo [root@rhel1 ~]# id foo uid=511(foo) gid=511(foo) groups=511(foo),509(sales),514(hr) [root@rhel1 ~]#
NOTE:So make sure you use -a option incase you want to append to existing group instead of changing the existing secondary groups.
Leave a Reply