I have a server running where I use php to run a bash script to verify certain information of a user. For example, I have a webhosting server set up, and in order to be able to add another domain to their account I want to verify if the user is actually a member of the 'customers' group. What would be the best way to do this?
I have searched google, but all it comes up with is ways to check whether a user or a group exists, so google is not being a big help right now.
Explanation: id -nG $USER shows the group names a user belongs to. grep -qw $GROUP checks silently if $GROUP as a whole word is present in the input.
To display the members of a group, or the groups to which a user belongs, use the pts membership command. To display the groups that a user or group owns, use the pts listowned command. To display general information about a user or group, including its name, AFS ID, creator, and owner, use the pts examine command.
user infomation is stored in /etc/passwd, so you can use "grep 'usename' /etc/passwd" to check if the username exist. meanwhile you can use "id" shell command, it will print the user id and group id, if the user does not exist, it will print "no such user" message.
Try doing this :
username=ANY_USERNAME if getent group customers | grep -q "\b${username}\b"; then echo true else echo false fi
or
username=ANY_USERNAME if groups $username | grep -q '\bcustomers\b'; then echo true else echo false fi
if id -nG "$USER" | grep -qw "$GROUP"; then echo $USER belongs to $GROUP else echo $USER does not belong to $GROUP fi
Explanation:
id -nG $USER
shows the group names a user belongs to.grep -qw $GROUP
checks silently if $GROUP as a whole word is present in the input.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With