Quick Tip: Add users to a group with augeas

05 May 2010

When you use Augeas to add users to groups, you might do that by first adding a new user node to the corresponding group (we’ll use kvm for the sake of this example), and then set its value to the name of the user:

augtool> ins user after /files/etc/group/kvm/user[last()]
augtool> set /files/etc/group/kvm/user[last()] joe

That works great for groups that already contain at least one user. If there are no users in the group yet, the ins will fail since there is no match for /files/etc/group/kvm/user[last()].

Instead of saying ‘add a new user after the last user’, you need to say ‘add a new user either after the gid field or after the last user’, which turns the above ins command to

augtool> ins user after /files/etc/group/kvm/*[self::gid or self::user][last()]
augtool> set /files/etc/group/kvm/user[last()] joe

Creative Commons License Watzmann.Blog by David Lutterkort is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

Generated with Jekyll