Linux

授予特定使用者對設備的權限,而不授予其他使用者訪問權限

  • June 4, 2020

我有一個讀卡器/dev/sdb

我所做的是將所有權限授予所有者、組和世界其他地方,使用:

sudo chmod 777 /dev/sdb

我可以使用另一種組合,只允許所有者(我)使用讀卡器嗎?

只有一個使用者帳戶。

有多種方法可以實現這一點。

  1. 將您的使用者添加到擁有該設備的組中

通常在大多數發行版中,塊設備由特定組擁有。您需要做的就是將您的使用者添加到該組。

例如,在我的系統上:

# ls -l /dev/sdb
brw-rw---- 1 root disk 8, 16 2014/07/07-21:32:25 /dev/sdb

因此,我需要將我的使用者添加到disk組中。

# usermod -a -G disk patrick

 

2.更改設備的權限

這個想法是創建一個 udev 規則以在檢測到設備時執行命令。

首先,您需要找到一種方法來辨識設備。你用udevadm這個。例如:

# udevadm info -a -n /dev/sdb

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

 looking at device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host6/target6:0:0/6:0:0:0/block/sdb':
   KERNEL=="sdb"
   SUBSYSTEM=="block"
   DRIVER==""
   ATTR{ro}=="0"
   ATTR{size}=="31116288"
   ATTR{stat}=="     279      219     3984     1182        0        0        0        0        0      391     1182"
   ATTR{range}=="16"
   ATTR{discard_alignment}=="0"
   ATTR{events}=="media_change"
   ATTR{ext_range}=="256"
   ATTR{events_poll_msecs}=="-1"
   ATTR{alignment_offset}=="0"
   ATTR{inflight}=="       0        0"
   ATTR{removable}=="1"
   ATTR{capability}=="51"
   ATTR{events_async}==""

 looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host6/target6:0:0/6:0:0:0':
   KERNELS=="6:0:0:0"
   SUBSYSTEMS=="scsi"
   DRIVERS=="sd"
   ATTRS{rev}=="0207"
   ATTRS{type}=="0"
   ATTRS{scsi_level}=="0"
   ATTRS{model}=="STORAGE DEVICE  "
   ATTRS{state}=="running"
   ATTRS{queue_type}=="none"
   ATTRS{iodone_cnt}=="0x184"
   ATTRS{iorequest_cnt}=="0x184"
   ATTRS{device_busy}=="0"
   ATTRS{evt_capacity_change_reported}=="0"
   ATTRS{timeout}=="30"
   ATTRS{evt_media_change}=="0"
   ATTRS{max_sectors}=="240"
   ATTRS{ioerr_cnt}=="0x2"
   ATTRS{queue_depth}=="1"
   ATTRS{vendor}=="Generic "
   ATTRS{evt_soft_threshold_reached}=="0"
   ATTRS{device_blocked}=="0"
   ATTRS{evt_mode_parameter_change_reported}=="0"
   ATTRS{evt_lun_change_reported}=="0"
   ATTRS{evt_inquiry_change_reported}=="0"
   ATTRS{iocounterbits}=="32"
   ATTRS{eh_timeout}=="10"

 looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host6/target6:0:0':
   KERNELS=="target6:0:0"
   SUBSYSTEMS=="scsi"
   DRIVERS==""

 looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host6':
   KERNELS=="host6"
   SUBSYSTEMS=="scsi"
   DRIVERS==""

 looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0':
   KERNELS=="1-1.3:1.0"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb-storage"
   ATTRS{bInterfaceClass}=="08"
   ATTRS{bInterfaceSubClass}=="06"
   ATTRS{bInterfaceProtocol}=="50"
   ATTRS{bNumEndpoints}=="02"
   ATTRS{supports_autosuspend}=="1"
   ATTRS{bAlternateSetting}==" 0"
   ATTRS{bInterfaceNumber}=="00"

 looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3':
   KERNELS=="1-1.3"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ATTRS{bDeviceSubClass}=="00"
   ATTRS{bDeviceProtocol}=="00"
   ATTRS{devpath}=="1.3"
   ATTRS{idVendor}=="05e3"
   ATTRS{speed}=="480"
   ATTRS{bNumInterfaces}==" 1"
   ATTRS{bConfigurationValue}=="1"
   ATTRS{bMaxPacketSize0}=="64"
   ATTRS{busnum}=="1"
   ATTRS{devnum}=="5"
   ATTRS{configuration}==""
   ATTRS{bMaxPower}=="500mA"
   ATTRS{authorized}=="1"
   ATTRS{bmAttributes}=="80"
   ATTRS{bNumConfigurations}=="1"
   ATTRS{maxchild}=="0"
   ATTRS{bcdDevice}=="0207"
   ATTRS{avoid_reset_quirk}=="0"
   ATTRS{quirks}=="0x0"
   ATTRS{serial}=="000000000207"
   ATTRS{version}==" 2.00"
   ATTRS{urbnum}=="1115"
   ATTRS{ltm_capable}=="no"
   ATTRS{manufacturer}=="Generic"
   ATTRS{removable}=="unknown"
   ATTRS{idProduct}=="0727"
   ATTRS{bDeviceClass}=="00"
   ATTRS{product}=="USB Storage"

 looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1':
   KERNELS=="1-1"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ATTRS{bDeviceSubClass}=="00"
   ATTRS{bDeviceProtocol}=="01"
   ATTRS{devpath}=="1"
   ATTRS{idVendor}=="8087"
   ATTRS{speed}=="480"
   ATTRS{bNumInterfaces}==" 1"
   ATTRS{bConfigurationValue}=="1"
   ATTRS{bMaxPacketSize0}=="64"
   ATTRS{busnum}=="1"
   ATTRS{devnum}=="2"
   ATTRS{configuration}==""
   ATTRS{bMaxPower}=="0mA"
   ATTRS{authorized}=="1"
   ATTRS{bmAttributes}=="e0"
   ATTRS{bNumConfigurations}=="1"
   ATTRS{maxchild}=="6"
   ATTRS{bcdDevice}=="0000"
   ATTRS{avoid_reset_quirk}=="0"
   ATTRS{quirks}=="0x0"
   ATTRS{version}==" 2.00"
   ATTRS{urbnum}=="61"
   ATTRS{ltm_capable}=="no"
   ATTRS{removable}=="unknown"
   ATTRS{idProduct}=="0024"
   ATTRS{bDeviceClass}=="09"

 looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1':
   KERNELS=="usb1"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ATTRS{bDeviceSubClass}=="00"
   ATTRS{bDeviceProtocol}=="00"
   ATTRS{devpath}=="0"
   ATTRS{idVendor}=="1d6b"
   ATTRS{speed}=="480"
   ATTRS{bNumInterfaces}==" 1"
   ATTRS{bConfigurationValue}=="1"
   ATTRS{bMaxPacketSize0}=="64"
   ATTRS{authorized_default}=="1"
   ATTRS{busnum}=="1"
   ATTRS{devnum}=="1"
   ATTRS{configuration}==""
   ATTRS{bMaxPower}=="0mA"
   ATTRS{authorized}=="1"
   ATTRS{bmAttributes}=="e0"
   ATTRS{bNumConfigurations}=="1"
   ATTRS{maxchild}=="3"
   ATTRS{bcdDevice}=="0313"
   ATTRS{avoid_reset_quirk}=="0"
   ATTRS{quirks}=="0x0"
   ATTRS{serial}=="0000:00:1d.0"
   ATTRS{version}==" 2.00"
   ATTRS{urbnum}=="26"
   ATTRS{ltm_capable}=="no"
   ATTRS{manufacturer}=="Linux 3.13.6-gentoo ehci_hcd"
   ATTRS{removable}=="unknown"
   ATTRS{idProduct}=="0002"
   ATTRS{bDeviceClass}=="09"
   ATTRS{product}=="EHCI Host Controller"

 looking at parent device '/devices/pci0000:00/0000:00:1d.0':
   KERNELS=="0000:00:1d.0"
   SUBSYSTEMS=="pci"
   DRIVERS=="ehci-pci"
   ATTRS{irq}=="23"
   ATTRS{subsystem_vendor}=="0x144d"
   ATTRS{broken_parity_status}=="0"
   ATTRS{class}=="0x0c0320"
   ATTRS{companion}==""
   ATTRS{enabled}=="1"
   ATTRS{consistent_dma_mask_bits}=="32"
   ATTRS{dma_mask_bits}=="32"
   ATTRS{local_cpus}=="0f"
   ATTRS{device}=="0x1e26"
   ATTRS{uframe_periodic_max}=="100"
   ATTRS{msi_bus}==""
   ATTRS{local_cpulist}=="0-3"
   ATTRS{vendor}=="0x8086"
   ATTRS{subsystem_device}=="0xc0d3"
   ATTRS{numa_node}=="-1"
   ATTRS{d3cold_allowed}=="1"

 looking at parent device '/devices/pci0000:00':
   KERNELS=="pci0000:00"
   SUBSYSTEMS==""
   DRIVERS==""

然後在 中新建一個文件/etc/udev/rules.d,如99-cardreader.rules

SUBSYSTEM=="block", ATTRS{idProduct}=="0727", ATTRS{serial}=="000000000207", ACTION=="add", RUN+="/bin/chmod 777 /dev/$name"

在這裡,我使用udevadm info命令的輸出來查找設備的一些辨識資訊。我使用第SUBSYSTEM="block"一個條目的條目,然後是ATTRS第 6 個條目的值。這基本上會找到具有該產品和序列號的 USB 設備,然後找到從該 USB 設備產生的塊設備。

RUN命令會將設備上的權限更改為777. 但是我不認為這是一個很好的解決方案,因為這將設備向世界開放。相反,更好的解決方案可能是:

SUBSYSTEM=="block", ATTRS{idProduct}=="0727", ATTRS{serial}=="000000000207", ACTION=="add", RUN+="/bin/setfacl -m u:patrick:rw- /dev/$name"

這將授予使用者patrick對設備的讀/寫訪問權限。

*注意:*重要的是要記住,在編寫 udev 規則時,您只能使用來自頂級設備的參數,以及鏈中的另一個設備。因此我可以使用SUBSYSTEM="block"參數和ATTRS參數。但是我不能使用鏈中任何其他設備的任何參數,否則規則將無法匹配。

引用自:https://unix.stackexchange.com/questions/141255