Is there a similar command like "lid" on Ubuntu?
On RHEL, there is a command lid
, which lists group users, no matter primary group or secondary group.
[root@192 ~]# id user1
uid=1000(user1) gid=1000(user1) groups=1000(user1),1001(g1)
[root@192 ~]# id user2
uid=1001(user2) gid=1002(user2) groups=1002(user2),1001(g1)
[root@192 ~]# id user3
uid=1002(user3) gid=1001(g1) groups=1001(g1)
[root@192 ~]# lid -g g1
user3(uid=1002)
user1(uid=1000)
user2(uid=1001)
[root@192 ~]#
But it doesn’t exist on Ubuntu. Is there a similar one?
It does exist in Ubuntu, but it’s provided under a different name:
sudo libuser-lid -g g1
It’s part of the libuser
package, install that if necessary:
sudo apt install libuser
The reason it’s not named lid
is that lid
is provided in the id-utils
package and has a different purpose.
The described functionality can be achieved using standard utilities:
for u in $(getent group | grep '^g1:' | cut -d: -f4 | tr , 'n'); do
printf "%s(uid=%d)n" $u $(id -u "$u")
done
Update: the command:
getent passwd | grep -E '^([^:]+:){3}'$(getent group | grep '^g1:' | cut -d: -f3)':' | cut -d: -f1
will retrieve lines from /etc/passwd corresponding to users whose primary group is g1
. This can be combined with the previous command:
for u in $({ getent passwd | grep -E '^([^:]+:){3}'$(getent group |
grep '^g1:' | cut -d: -f3)':' | cut -d: -f1;
getent group | grep '^g1:' | cut -d: -f4 | tr , 'n'; }); do
printf "%s(uid=%d)n" $u $(id -u "$u")
done | sort | uniq
with the added sorting and removal of duplicates at the end.
This command can be made into a shell function for convenience, using the group name as a parameter:
lid_replacement()
{
for u in $({ getent passwd | grep -E '^([^:]+:){3}'$(getent group |
grep '^'$1':' | cut -d: -f3)':' | cut -d: -f1;
getent group | grep '^'$1':' | cut -d: -f4 | tr , 'n'; }); do
printf "%s(uid=%d)n" $u $(id -u "$u")
done | sort | uniq
}
# call as: `lid_replacement g1`
Edit: Updated regex to match the exact group name.
Edit 2: Updated to use getent(1) and added the function lid_replacement
.