Programatically discover "main" domain?

I have users setup for different domains. So the domain example.com is the user “example”. I can find the username in a script easily with whoami but the domain name is trickier. Is there an easy way when logged in as a user to find what the “primary” domain is?

Howdy,

You may be able to figure that out from the command line tools… two example commands that could help are:

virtualmin list-users --domain-user YOUR_USERNAME --include-owner

virtualmin list-users --domain-user YOUR_USERNAME --include-owner --multiline

That needs to run as root.

Are you using home a directory naming scheme that includes the domain name, like /home/example.com? If so, this might help:

cd ~; pwd | egrep "\/\w+\/(.*)"

Unfortunately, I’m using the “default” setup, so the domain “example.com” has the user “example”. But the domain “exampledomain.org” would have the user “exampledomain” - so I can’t just “guess” and add .com as it wouldn’t always be right. If I had made the username the entire domain whoami would be perfect.

So far I’ve found that the domain name is in the file that ~/logs/access-log is symlinked to.

ls -l ~/logs/access_log | awk '{ print $10 }' | awk -F / '{ print $5 }' | awk -F _ '{ print $1 }'

returns the domain, but it’s pretty ugly. I wish I knew awk better.