I'm trying to change the Linux hostname with PHP script. I could do it directly on the Linux machine using the command hostname
But when I use the below PHP Statement .. it didn't work.
system('sudo hostname '.trim($Host));
Please suggest a solution.
Assuming your Apache user can run sudo
commands without entering a password (which, by the way, would be a huge security vulnerability, so please don't do it) you could do this:
exec("sudo hostname " . $hostname);
In order to make sure your www-user
can do this, edit /etc/sudoers
with the following line:
www-user ALL=NOPASSWD: /bin/hostname
Whatever you're trying to accomplish by changing the hostname, you'll probably find that you can do it another, safer, way. To anyone reading this, I thoroughly do not recommend you follow these instructions, you are opening your server up to potential harm by escalating the priveleges of the www-user
account.
try this command with the function exec(). http://php.net/manual/de/function.exec.php