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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top