Search code examples
linuxlinux-device-driverpermission-denied

Permission denied when trying to write to a block device


I am trying to write a block device driver that reads/writes off of the network. In essence, I have modified the example here for ram disks. And Here is my code. When I try to write to the block device using the command sudo cat > /dev/rb, I get premission denied.

$ sudo cat > /dev/rb
bash: /dev/rb: Permission denied

Can Anyone help me fix this??

I can see nothing suspicious in the kernel log.

Thanks in advance!


Solution

  • Your redirection is not run as root, only cat does.

    Try the following:

    sudo sh
    # cat > /dev/rb
    

    The redirection (>) is a special syntax that is handled by the shell. This means that either you need to handle writing in a different way, or by elevating the executed shell.

    An example of the former would be to run for instance cat | sudo dd of=/dev/rb (where dd itself handles opening and writing to the device, and dd is being run as root)

    An example of the latter would be running an entire shell as root such as: sudo sh -c "cat > /dev/rb"