There are two levels to the OP report:
1. sudo and brew are not good friends, and that's the brewer's policy.
$ brew --version
$ sudo brew --version
The second gives the error as reported and does not even supply the version number
2. this test did not cause me any hostname or other grief, i.e., pure invokation of brew does not cause the reported behaviour.
So there is an additional factor in the problem as reported by OP which is not obvious with the information given.
FWIW brew works just fine with: sudo su othuser provided the othuser is not $(id -u) -eq 0 (a.k.a. root).
Assuming this is a context specific problem possibly related to the less than graceful handling of the brew, do(/did) the host name changes and path problems persist after restart?