【问题标题】:BASH : mount usb drive on a non root user does not give rights to write to the driveBASH:在非 root 用户上安装 USB 驱动器不授予写入驱动器的权限
【发布时间】:2012-03-14 08:33:47
【问题描述】:

我在脚本中使用 mount 命令在 Bash 中安装 USB 驱动器。我已经关闭了 haldameon 和 autofs,这样驱动器就不会自动挂载。

一切都在 Root 下运行,如果你在 root 下运行它,然后切换到非 root 帐户。但是,当从关机进入非 root 帐户然后运行脚本时,驱动器将挂载但不允许写入命令。

这是一些伪代码:

关闭 autofs,haldaemon 手动将驱动器插入计算机 运行安装 尝试使用目录 /mnt/ 写入

错误权限

这是关闭自动挂载的代码。

#stop automounter
/etc/init.d/autofs stop 
#stop hal daemon, this is the hardware abstraction layer
/etc/init.d/haldaemon stop 

这是挂载代码:

    #WHITE---------------------------------------

    if grep -qs '/mnt/WHITE' /proc/mounts; then 
        echo "WHITE Mounted re-mounting Unnecessary" 
    else
        #check if the directories are already there and remove if necessary
        if [ -d "/mnt/WHITE" ] ; then
            rmdir "/mnt/WHITE"
        fi

        #create directory and mount by label
        mkdir -p /mnt/WHITE
        mount -L WHITE /mnt/WHITE

        #check if the WHITE USB Drive is mounted to the correct directory
        if [ -d "/mnt/WHITE" ] ; then 
            #check if USB is mounted by location
            if grep -qs '/mnt/WHITE' /proc/mounts; then 
                echo "WHITE Mounted"
            else
                echo $errorstatus_white_mount
                exit 1
            fi
        else
            echo $errorstatus_white_mount
            exit 1
        fi
    fi      

这是有错误的复制代码:

echo "Copying Test Files to Drives"
#copy 
cp $copyfile "/mnt/WHITE"
cp $copyfile "/mnt/GREEN"
cp $copyfile "/mnt/RED"
sync
sleep 2

mount、/etc/init.d/autofs stop、/etc/init.d/haldaemon stop 的命令也在 sudoers 文件中。

感谢您对这个权限之谜的帮助。

【问题讨论】:

  • /mnt/WHITE 的 fstab 条目是什么样的?
  • 所有者:root,创建​​和删除;组:根,访问文件;其他:访问文件

标签: bash permissions usb mount


【解决方案1】:

可能是您没有在 mount/fstab 中传递一些参数。您可以尝试像这样在脚本中显式传递选项;

mount -L WHITE /mnt/WHITE -o rw

具体赋予它读/写权限,或者

mount -L WHITE /mnt/WHITE -o rw,uid=test,gid=test

以读/写方式挂载,并使用户可以访问设备:组test

【讨论】:

  • 这行得通。然后我还对它们使用了 chown 以确保我可以访问 /mnt/
【解决方案2】:

检查 mnt 驱动程序的权限

ls -l

它们可能归根用户所有,因此您不能写信给它们。

可能需要把它们扔掉:

chown domain:user /mnt/{WHITE|GREEN|RED}

【讨论】:

  • 我在上面结合使用了这个,但是它本身并不能工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-01
  • 2019-11-22
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多