欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

同时显示 所有的用户和用户组(用户所属组) How to show all registred users and their groups with one command? 有大用 有大大用

awk -F':' '{ print $1}' /etc/passwd | while read -r line; do id "$line"; done


或者把下面的代码存为一个文件 ,再 /bin/bash 此文件


awk -F":" 'NR==FNR {   

    m[$1] = "";

    next;

}

{

    for (i in m) {

        if ($4 ~ i || $1 == i) {

            m[i] = m[i] $1 " ";

        }

    }

}

END {

    for (i in m) {

        print i ":", m[i];

    }

}' /etc/shadow /etc/group






I was writing a test on basic Unix commands etc, and there was a question show all registered users and their groups with one command. At the same time I can't use getent command.

shareimprove this questionasked Apr 23 '15 at 10:37Alexander835

A possible solution is for example with awk and reading /etc/shadow and /etc/group (I assume you don't need system users and I am trying to exclude them and locked users):

awk -F":" 'NR==FNR {
    if ($2 !~ /\!/ && $2 !~ /\*/) {
        m[$1] = "";
    }
    next;
}
{
    for (i in m) {
        if ($4 ~ i || $1 == i) {
            m[i] = m[i] $1 " ";
        }
    }
}
END {
    for (i in m) {
        print i ":", m[i];
    }
}' /etc/shadow /etc/group

You can remove if ($2 !~ /\!/ && $2 !~ /\*/) condition to list all user accounts, and also note that existence of ! or * in /etc/shadow means the user is not be able to use a unix password to log in (but the user may log in the system by other means e.g. key based login).

shareimprove this answeredited Apr 24 '15 at 7:18Gilles470k1019001425answered Apr 23 '15 at 12:00taliezin6,18411525

cat /etc/passwd (filter the contents with grep as per your requirements)

Here you go: awk -F':' '{ print $1}' /etc/passwd | while read -r line; do id "$line"; done

shareimprove this answeredited Apr 23 '15 at 11:18answered Apr 23 '15 at 10:54rajaganesh877122725


来自  https://unix.stackexchange.com/questions/198122/how-to-show-all-registred-users-and-their-groups-wit...

普通分类: