Can I change permissions on a device with chmod?
Solution 1
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.
Solution 2
/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
.
Related videos on Youtube
![Raydel Miranda](https://i.stack.imgur.com/or6PX.jpg?s=256&g=1)
Raydel Miranda
Updated on September 18, 2022Comments
-
Raydel Miranda almost 2 years
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? -
Gilles 'SO- stop being evil' about 10 yearsIt'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
. -
Raydel Miranda about 10 years@countermode I make a mistake typing
chroot
insteadchmod
. Thanks all for comment and answers. -
Alec Teal over 4 yearsThanks for not answering the question posted.