Adding all users to a group

Recently I found that in I needed to add all the interactive users on my system to the same user group, in this case to allow them access to the network manager applet. If you only have a couple of user accounts doing this by hand isn’t too difficult, but I have quite a few different accounts on my system so I wanted to see if I could come up with a ‘one liner’ to do the job…

In order to add a user to a group you need to be running as root.

$ su
Password: 

OR

$ sudo -i
Password:


First you need work out how to get a list of user accounts on the system, in this case I’m doing this by extracting all the username fields from the password file.

# cat /etc/passwd | cut -d ':' -f1
root
daemon
:
:
:
nobody
user1
user2
user3
:
:
:
sshd

Since you only want a list of interactive users I’ll filter out those that don’t have a home directory (assuming that all home directories are in the /home directory) using grep.

# cat /etc/passwd | grep /home | cut -d ':' -f1
user1
user2
user3

By combining this with a for loop we can iterate through all the interactive users and add each one to the netdev group so that they can use the network manager applet.

# for ID in $(cat /etc/passwd | grep /home | cut -d ':' -f1); \
> do (adduser $ID netdev);done
The user `user1' is already a member of `netdev'.
Adding user 'user2' to group 'netdev' ...
Adding user user2 to group netdev
Done.
Adding user 'user3' to group 'netdev' ...
Adding user user3 to group netdev
Done.

This entry was posted in Linux and tagged . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s