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:
$ 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
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
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
Adding user 'user3' to group 'netdev' ...
Adding user user3 to group netdev