文件权限仅执行


12

我如何将文件设置为仅对其他用户可执行,而对其他用户不可读/可写,这是因为我正在使用用户名执行某些操作,但又不想透露密码。我试过了 :

chmod 777 testfile
chmod a=x
chmod ugo+x

当以其他用户身份执行时,我仍然获得拒绝权限。


不以其他用户身份执行该怎么办?这是脚本还是二进制程序?脚本必须由解释器读取。
psusi 2011年

文件顶部是否有shebang?#!/ bin / sh
J男爵

Answers:


10

您需要对脚本具有读取和执行权限才能执行该脚本。如果您无法阅读脚本的内容,则也无法执行该脚本。

tony@matrix:~$ ./hello.world
hello world
tony@matrix:~$ ls -l hello.world
-rwxr-xr-x 1 tony tony 17 Jul 13 22:22 hello.world
tony@matrix:~$ chmod 100 hello.world
tony@matrix:~$ ls -l hello.world
---x------ 1 tony tony 17 Jul 13 22:22 hello.world
tony@matrix:~$ ./hello.world
bash: ./hello.world: Permission denied

确实,经过更新可以反映出对于考虑到原始问题的脚本来说确实如此,我怀疑情况就是如此。
SevenBitTony11年

13

前面的陈述有一半的道理。您可以设置一个脚本,使该脚本不被用户读取,但仍可执行。这个过程有些麻烦,但是可以通过在/ etc / sudoer中创建一个异常来实现,以便用户可以临时以自己的身份运行脚本而无需提示输入密码。下面的例子:

我想与用户共享的一些脚本:

me@OB1:~/Desktop/script/$ chmod 700 somescript.pl
me@OB1:~/Desktop/script/$ ls -l somescript.pl
-rwx------ 1 me me 4519 May 16 10:25 somescript.pl

制作一个调用'somescript.pl'的shell脚本并将其保存在/ bin /中:

me@OB1:/bin$ sudo cat somescript.sh
[sudo] password for me: 
#!/bin/bash
sudo -u me /home/me/Desktop/script/somescript.pl $@

可选步骤在/ bin /中建立到somescript.sh的符号链接:

sudo ln -s /bin/somescript.sh /bin/somescript

确保shell脚本对用户可读/可执行(没有写访问权限):

sudo chmod 755 /bin/somescript.sh
me@OB1:/bin$ ls -l somescript*
lrwxrwxrwx 1 root root  14 May 28 16:11 somescript -> /bin/somescript.sh
-rwxr-xr-x 1 root root 184 May 28 18:45 somescript.sh

通过添加以下行,在/ etc / sudoer中设置异常:

# User alias specification
User_Alias  SCRIPTUSER = me, someusername, anotheruser

# Run script as the user 'me' without asking for password
SCRIPTUSER ALL = (me) NOPASSWD: /home/me/Desktop/script/somescript.pl

证明:

someuser@OB1:~$ somescript
***You can run me, but can't see my private parts!***

someuser@OB1:~$ cat /home/me/Desktop/script/somescript.pl
cat: /home/me/Desktop/script/somescript.pl: Permission denied

这个方法应该比试图与混淆更好Filter::CryptoPAR::Filter::CryptoAcme::Bleach可逆转确定的用户设计的。将脚本编译为二进制文件也是如此。如果您发现此方法有问题,请告诉我。对于更高级的用户,您可能需要完全删除User_Alias部分,并用'%groupname'替换SCRIPTUSER。这样,您可以使用usermod命令管理脚本用户。


6

如果让其他用户执行程序,则无论程序文件是否可读,他们都可以知道该程序在做什么。他们所需要做的就是指向调试器(或类似调试器的程序,例如strace)。如果二进制可执行文件是可执行文件且不可读(该脚本不能运行脚本,因为解释器需要能够读取该脚本),则可执行文件可以运行,但这并不能为您提供任何安全性。

如果希望其他人能够像黑盒子一样执行程序,而又不让他们确切地看到程序在做什么,则需要为脚本赋予提升的特权:将setuid赋予用户。只有root可以在setuid程序上使用调试工具。请注意,编写安全的setuid程序并不容易,并且大多数语言都不适用。有关更多说明,请参见在shell脚本上允许setuid。如果要编写setuid程序,我强烈建议Perl,它具有一种模式(异味模式),该模式明确旨在使安全的setuid脚本成为可能。


-1

您可以使用chmod命令设置文件权限。root用户和文件所有者都可以设置文件权限。chmod有两种模式,符号模式和数字模式。

首先,您决定是否为用户(u),组(g),其他(o)或所有三个(a)设置权限。然后,您添加一个权限(+),将其删除(-),或清除以前的权限并添加一个新的权限(=)。接下来,决定是否设置读取权限(r),写入权限(w)或执行权限(x)。最后,您将告诉chmod您想更改哪个文件的权限。

这里有一些例子。

清除所有权限,但为所有人添加读取权限:

$ chmod a=r filename

命令后,文件的权限将为-r--r--r--

为组添加执行权限:

$ chmod g+x filename

现在,文件的权限为-r--r-xr--

为文件所有者添加写和执行权限。请注意如何同时设置多个权限:

$ chmod u + wx文件名

之后,文件权限将为-rwxr-xr--

从文件的所有者和组中删除执行权限。再次注意,如何同时设置它们:

$ chmod ug-x文件名

现在,权限为-rw-r--r--

这是在符号模式下设置文件权限的快速参考:

Which user?
u   user/owner
g   group
o   other
a   all
What to do?
+   add this permission
-   remove this permission
=   set exactly this permission
Which permissions?
r   read
w   write
x   execute

从这个问题显而易见,OP知道如何使用chmod。这不能回答问题。
斯科特,
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.