douling6469 2013-05-08 11:00
浏览 94
已采纳

使用PHP脚本更改Linux主机名

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.

  • 写回答

2条回答 默认 最新

  • douba4824 2013-05-08 11:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch