Question
How do I list all domains for a user account on my server?
Answer
There are several methods that can be used to identify domains for a user account.
The information can be parsed from the /etc/userdomains file on the server using the account username with grep or perl commands.
Syntax for grep
grep ': $user' /etc/userdomains
Example grep usage replacing $user with the actual username
# grep ': mydomain' /etc/userdomains
mydomain.com: mydomain
myotherdomain.com: mydomain
myotherdomain.mydomain.com: mydomain
mysubdomain.mydomain.com: mydomain
Syntax for perl
perl -F: -lane 'if (@F[1] =~ /$user/) {print @F[0]}' /etc/userdomains
Example perl usage replacing $user with the actual username
# perl -F: -lane 'if (@F[1] =~ /mydomain/) {print @F[0]}' /etc/userdomains
mydomain.com
myotherdomain.com
myotherdomain.mydomain.com
mysubdomain.mydomain.com
Alternatively, you can use uapi to list the domains for a user with the list_domains function. Further information about uapi syntax can be found on the documentation below.
https://api.docs.cpanel.net/openapi/cpanel/operation/list_domains/
Syntax with uapi list_domains
uapi --output=jsonpretty --user=$user DomainInfo list_domains
Example uapi command replacing $user with the actual username
# uapi --output=jsonpretty --user=mydomain DomainInfo list_domains
{
"apiversion" : 3,
"func" : "list_domains",
"result" : {
"status" : 1,
"warnings" : null,
"metadata" : {},
"data" : {
"addon_domains" : [
"myotherdomain.com"
],
"sub_domains" : [
"mysubdomain.mydomain.com"
],
"parked_domains" : [],
"main_domain" : "mydomain.com"
},
"errors" : null,
"messages" : null
},
"module" : "DomainInfo"
}