pkexec 用户提权操作

108 阅读1分钟

使用场景

本操作针对用户linux用户,如果对于某个文件没有权限,但是仍想进行写操作,则可以在代码中如下脚本如下

const shell = require("shelljs");

const shellComm = `echo "test have auth to write" > yourMkdirPath/test.sh`;
const shellCommSuper = `/usr/bin/pkexec su --command "chmod -R 777 yourMkdirPath"`;
/**
 先判断是否具有写权限,如果没有则提升权限,将此文件夹分享给所有人
*/
function startShell(checkAuth = true) {
  let startExec = shell.exec(checkAuth ? shellComm : shellCommSuper, {
    async: true,
    cwd: yourMkdirPath,
    encoding: "binary",
  });
  startExec.stdout.on("data", (data) => {});
  startExec.stderr.on("data", (data) => {
    if (checkAuth) {
      startShell(false);
    }
  });
}

startShell()