我可以在终端
中运行此命令./ rename_hostname.sh my_new_hostname
rename_hostname.sh包含:
#!/bin/bash
#Assign existing hostname to $hostn
hostn=$(cat /etc/hostname)
newhost=$1
#change hostname in /etc/hosts & /etc/hostname
sudo sed -i "s/$hostn/$newhost/g" /etc/hosts
sudo sed -i "s/$hostn/$newhost/g" /etc/hostname
#display new hostname
sudo /etc/init.d/hostname.sh
echo "Successful"
一切正常。
但是当我尝试通过PHP和Apache www-data
运行时$exec = "/path/to/sh/rename_hostname.sh " . $new_hostname
$successful = shell_exec($exec);
它返回成功,但主机名没有被更改。
我在文件末尾添加了sudo visudo。
www-data ALL = NOPASSWD: /etc/init.d/hostname.sh
www-data ALL = /etc/hosts
www-data ALL = /etc/hostname
还试过
www-data ALL = NOPASSWD: /etc/hosts
www-data ALL = NOPASSWD: /etc/hostname
似乎都没有帮助我。
我还将rename_hosname.sh修改为www-data用户
非常感谢任何帮助