Thursday, May 16, 2013

Add / Remove user to an existing Group in Linux

Users & Groups in Linux : ( Add & Remove )

All Groups of unix system are listed in the file /etc/group
To see the list of all groups of system
$ cut -d: -f1 /etc/group

See all Groups of a user :
$ id -nG username
Suppose I want to see all Groups of user Thomas whose username is thomas :
$ id -nG thomas
The output is name of groups of thomas.
user1 root admin sambashare vboxusers usbuser mysql apache2
Here user1 is the Primary Group (First Group) & others are secondary Groups.
You can use following command too to see groups of a user.
$ groups username
The first group after colon is Primary Group.



Add an existing Group to user (Other groups should not be removed) :
$ usermod -a -G groupname username
Suppose I want to add a new group to user thomas, other groups should not be removed.
$ usermod -a -G newgroup thomas
Now an existing group newgroup has been added to user thomas. To see the changes, Run
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup



Add an existing Group to user (other groups should be removed, only Primary Group and currently added Group should be available) :
$ usermod -G groupname username
Suppose I want to add new group to user thomas, other groups should be removed. Only Primary Group and currently added group should be assigned to user.
$ usermod -G grp thomas
Now an existing group grp has been added to user thomas. All other secondary groups have been removed from thomas
To find current groups of user thomas
$ id -nG thomas
The output is
user1 grp
All other secondary groups are replaced with grp. Now there is only one secondary group for thomas.




Remove Group of a user :
You can remove only secondary groups of a user. Primary Group can't be removed by this command. If you want to remove primary group, you have to assign a new primary group for user. A user can't be modified without primary group.
$ gpasswd -d username groupname
Suppose I want to remove a secondary group apache2 from user thomas.
Right now there are following Primary Group & Secondary Groups of user thomas.
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now I delete apache2
$ gpasswd -d thomas apache2
See the groups of user thomas
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql newgroup
apache2 has been removed for thomas.



Change Primary Group of a user :
$ usermod -g groupname username
It removes the current Primary Group of the user and replaces with new added primary group. Secondary groups are as it is. There is no change in Secondary Groups.
Suppose I want to change primary group of user thomas.
To see the groups of user thomas
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now I run following command to change Primary Group
$ usermod -g newgroup2 thomas
Now Primary Group user1 has been removed from thomas and it is replaced with newgroup2. Now newgroup2 is primary group of thomas.
To verify the changes, I see the groups list.
$ id -nG thomas
The output is
newgroup2 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now the Primary Group is newgroup2.


fatal: Queue report unavailable - mail system is down

mail postfix/postqueue[20397]: fatal: Queue report unavailable - mail system is down

If you are getting this error while sending mail via Postfix, Restart the postfix.
$ sudo service postfix restart
Now try to send a mail.

If sendmail and postfix both are installed on the system, stop sendmail and try again with postfix.

If you are still getting same error. Remove and install postfix again.

dpkg: unrecoverable fatal error, aborting: Cannot allocate memory

dpkg: unrecoverable fatal error, aborting: fork failed: Cannot allocate memory

It means you do not have enough memory to execute the operation.

The fix is for memory error, if you are getting following error
E: Sub-process /usr/bin/dpkg returned an error code (1)
You should check this post.

Since it is a memory error, try to configure dpkg first.
$ sudo dpkg --configure -a
But if you are still getting the error.

Check the free and used physical memory in megabytes by following command.
$ free -m
It displays the total amount of free and used physical memory and swap space in the system, as well as the buffers and cache consumed by the kernel.


Flush file system buffers by executing :
$ sync
Now try again.

If you are still facing the same problem, follow this.
To free page cache:
$ echo 1 > /proc/sys/vm/drop_caches

To free dentries and i-nodes :
$ echo 2 > /proc/sys/vm/drop_caches

To free page cache, dentries and i-nodes :
$ echo 3 > /proc/sys/vm/drop_caches
Now try again.

Still the error is not fixed, you should check the RAM and SWAP space for your system / server. Assigning a bigger RAM / SWAP will solve the error.