Restricting Users from Running chmod 777

为 chmod 创建自定义脚本包装可以帮助您管理和限制某些 chmod 命令的使用,例如防止设置 777 权限。下面是如何在 linux 的系统中创建这样一个脚本的基本示例。

Step 1: Create the Script

打开终端,并使用文本编辑器创建脚本。

sudo nano /usr/local/bin/safe_chmod

将以下内容添加到文件中:

#!/bin/bash
# Custom script wrapper for chmod to prevent setting 777 permissions

# Check if any of the arguments is '777'
for arg in "$@"; do
    if [ "$arg" == "777" ]; then
        echo "Error: Setting 777 permissions is not allowed for security reasons."
        exit 1
    fi
done

# If 777 is not found, execute the original chmod command with all arguments
/bin/chmod "$@"

Step 2: Make the Script Executable

保存并关闭该文件,然后使脚本可执行。

sudo chmod +x /usr/local/bin/safe_chmod

Step 3: Aliasing the chmod Command

为了使脚本有效替换 chmod 命令,您可以将 chmod 的别名设置为 safe_chmod。用户级别设置可以通过编辑 .bashrc.bash_profile 实现, 全局设置可以通过编辑 /etc/etc/bash.bashrc 实现。

echo "alias chmod='/usr/local/bin/safe_chmod'" >> ~/.bashrc 
source ~/.bashrc

我的开源项目

酷瓜云课堂-开源知识付费解决方案


鸠摩智首席音效师
475 声望9 粉丝

身强体健,龙精虎猛的活着。