0

我正在运行一个 bash 脚本,如果没有 sudo 在它们前面,脚本中的这些命令将无法工作。script.sh 位于 /jobs/script.sh 等文件夹中

我尝试在 script.sh 中运行的命令示例 -

  • mv /var/app/myapp /var/app/myapp.old

  • rm文件.tar.gz

  • tar -xzf /home/ubuntu/file.tar.gz -C /var/app/

如果我在它们前面添加 sudo,所有上述工作。

我试图弄清楚它们需要哪些权限才能在脚本中添加 sudo 才能工作。

我已尝试授予 script.sh rwx 权限并将所有者更改为 root。

我正在学习linux中的权限,所以我是新手。基本上 script.sh 应该有什么权限,这样我就不必在 bash 文件中使用 sudo 了?任何见解都会有很大帮助。

4

2 回答 2

0

From what you said it’s most likely you want read and write permissions you can achieve this with chmod

Chmod +[permission] filename

(+ is used to add permission you can also use - instead to remove it) Where permissions can be:

r  —> read 
w—> write 
x —>excecute 

... and more

FOR EXAMPLE: it seems you write permissions for the first file so :

chmod +w /var/app/myapp

Will fix problem

于 2020-11-16T22:20:06.920 回答
0

运行时sudo <some command><some command>则由 root 用户(超级用户执行)运行。您可能需要运行任何命令的原因sudo是因为命令读取/写入/执行的文件的权限使得只有“超级用户”(root)具有该权限。

执行命令mv fileA fileB时,执行用户需要:

  • 写入权限(fileB如果fileB已经存在)
  • 对包含目录的写权限fileB
于 2020-11-16T18:48:06.370 回答