【问题标题】:Can PrintWriter be used with sudo permissions?PrintWriter 可以与 sudo 权限一起使用吗?
【发布时间】:2020-09-29 21:36:06
【问题描述】:

我正在尝试自动化在 Linux 中创建虚拟主机的过程。我需要在 /etc/apache2/sites-available 中创建一个文件并写入 /etc/hosts 文件。我正在尝试使用 PrintWriter 执行此操作,但不知道如何获得许可。该程序仅供个人使用,但我仍然不想在源代码中保护我的用户密码。有没有办法请求身份验证,然后以 sudo 用户身份运行?

到目前为止的代码:

公共类 NewProjectController {

static String path = "/etc/apache2/sites-available/test";

public static void createVirtualHost(String vhost) throws FileNotFoundException, UnsupportedEncodingException {
    // TODO Auto-generated method stub

    PrintWriter writer = new PrintWriter(path, "UTF-8");
    writer.println("The first line");
    writer.println("The second line");
    writer.close();
}

}

【问题讨论】:

    标签: java authentication sudo printwriter


    【解决方案1】:

    当然可以在sudo权限下使用。
    您只需使用sudo java MainClassName 运行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2018-02-16
      • 2020-06-30
      相关资源
      最近更新 更多