1

note: I don't want to use a udev rule.

I need to change (programmatically) the permissions of some device. To understand what I have to do (in code) I want to do this using just chmod command.

So far, I've tried this:

root# ls -l /dev/sdb
brw-rw-rw- 1 root disk 8, 16 Apr 7 05:27 /dev/sdb

root# chmod 0600 /dev/sdb 

root# ls -l /dev/sdb
brw------- 1 root disk 8, 16 Apr 7 05:27 /dev/sdb

as you can see, /dev/sdb has read and write permissions only for the owner (root). But I'm still able to create new files and read files from my connected flash drive.

What am I missing? How can I use chmod to prevent users from writing to some device?

2 Answers 2

4

If I understand you right then there is some file system on /dev/sdb that you have mounted. What matters here are the permissions in the file system that resides on /dev/sdb, the permissions of /dev/sdb are completely irrelevant for your question. Except that with permissions 0666 anyone can bypass the access control mechanisms for that file system and access the content on the device arbitrarily, but this is a different issue.

If you want to restrict access to the files within the file system, then you have to assign appropriate ownership and permissions to the files (beginning with the root of that file system). For file systems like FAT mount(8) lets you set the ownership and access permissions for all files within the file system. If you want to expose the entire tree to certain users only and hide it from all others, then mount it somewhere where only those users have access to. But note that any user can see that something is being mounted (mount(8) or df(1) will show them).

chroot(1) is not going to help you at all.

3
  • 2
    It's enough to restrict the permissions on the mount point, or a directory above the mount point, e.g. chmod 700 /mnt/foo where the user that Raydel Miranda wants to restrict is does not own /mnt/foo. Apr 7, 2014 at 22:16
  • @countermode I make a mistake typing chroot instead chmod. Thanks all for comment and answers. Apr 9, 2014 at 12:06
  • Thanks for not answering the question posted.
    – Alec Teal
    Feb 9, 2020 at 12:42
1

/dev/sdb is the block device name. Changing the permission of /dev/sdb will not affect the filesystem on /dev/sdb.

Use mount to get the list of mounted mediums and mountpoints.

Use chmod to change permissions in mountpoint.

e.g. mount will show lines which looks something like

/dev/sdb1 on /run/mount/pioneeraxon/disk1 type ext4 (...)

In such case /run/mount/pioneeraxon/disk1 is the mountpoint for /dev/sdb1. You need to change the permissions in /run/mount/pioneeraxon/disk1.

1
  • thanks for not answering the question either.
    – Alec Teal
    Feb 9, 2020 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.