Linux - List / Copy group membership for users

12. December 2012 08:00

 

A quick guide on how to copy group member ship in linux to another user. Which can be useful when setting up new users on a linux machine to make sure that users. It is also a way to find out what groups a user is a member of.

 

Part 1 - Get a list of groups

 

To get a list of groups as user is a member of can be done by reading the /etc/group file and doing a little bit of processing. This can be done using the following command.

 

grep -E "(:|,)<username>(:,|$)" /etc/group|cut -f1 -d:

 

The complex expression above is built to match specific username in each line of the file. Of which there is 3 different cases ":<username>"  ",<username>,"  ",<username>". So we search for the specific username beginning with a ":" or a "," and also ending in a "," or a "$" which is a newline. Then it cuts on the first field using ":" as a separator.

 

This will output  a list of group that "<username>" is a member of

 

Part 2 - Add another user to the same list of groups

 

Since we know that addgroup can be used to add a user to a group by doing "addgroup <username> <groupname>" then we can expand the above to add a username to each group in the list like this

 

for i in `grep -E "(:|,)<username>(:,|$)" /etc/group|cut -f1 -d:` ; do

  addgroup <newuser> $i

done

 

This will then make sure that the user <newuser> is a member of all the same groups that <username> is.

 

You should take care using the above as you may give somebody access well beyond what you thought you might have by giving them access to additional groups.

 

E-mail Kick it! DZone it! del.icio.us Permalink


Linux - Killing all processes for a specific user

13. July 2012 08:00

 

Here is a few methods for killing tasks for a specific user in linux which may be required during account deletion or because somebody has managed to be an idiot and locked himself out with a fork bomb or some such.

 

The simple method is to use the utility called 'slay' which for debian / unbuntu and most other distrobutions is avilable if its not install you can install it using 'apt-get install slay'

 

It is very simple to use. Just running the command slay <username> and it will kill all of that users processes.

 

 

The other method to use when slay is not avilable is a combination of ps and kill. You can use the following command

 

 

kill -TERM `ps h --User nobody -o pid`

 

 

Understanding the above can other advantages as well because you can switch out the --User for --Group and kill processes by group id as well as for a specific user.

E-mail Kick it! DZone it! del.icio.us Permalink


Getting sudo to work without a password

4. January 2012 21:26

 

I started working with linux for development work again after a break for a few years. I have ended up working on a project that requires a lot of mixed access from the normal user account to root or to other accounts. The simple way todo this without having to type a password a million times a day is to use sudo.

This is a quick guide to how I went about configuring it in such a way they you can also have support for multiple users on the same machine that may require root. You will need to be root of course for this to work.

 

First of all add a new group.

 

root@linux:~# addgroup sudoers
Adding group `sudoers' (GID 1001) ...
Done.

 

 

Then add your self to the group and repeat for each of the other users you need to have access.

 

 

root@linux:~# adduser james sudoers
Adding user `james' to group `sudoers' ...
Adding user james to group sudoers
Done.

 

Then make sure the following line exist in the /etc/sudoers config file.

 

 

%sudoers ALL=NOPASSWD: ALL

 

 

Then to test it you will need to logout and in again to allow the group change to be visible and then run something like "sudo bash" to get a root shell. Now you don't need to type a password a million times a day.

 

I should probably point out that there are security considerations on this and bear in mind that I use it on a development box which only has extremly limited access.

E-mail Kick it! DZone it! del.icio.us Permalink