The xhost command needs an active X server to run, it can run at the login screen
e.g. when lightdm loads.
You can enable it by editing /etc/lightdm/lightdm.conf and adding the line :
example /etc/lightdm/lightdm.conf file :
example /home/user/bin/xhost.sh file :
it works on Ubuntu 12.04.1 LTS, and it was needed to enable a java application that needed X and was run by tomcat6