【发布时间】: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