1

I would like to write to a Device File (of a printer) located at /dev/usb/lp0. The file is owned by lp user and group. This file is created whenever I connect my printer device to the system.

I tried adding myself to the lp group. However the lp0 file doesn't appear when the printer is connected. Removing myself from the group fixes the issue.


One solution to get write permission is to -

  1. Detect whenever the device is connected
  2. Trigger a shell script that runs sudo chmod 0666 /dev/usb/lp0

This led me to the answer at https://unix.stackexchange.com/a/28711


The shell script is successfully triggered but it doesn't run the sudo command*, since the shell script was not executed from the terminal. I have tried using sudo and gksudo, both have failed to prompt me to enter password i.e, I am unable to escalate permissions through a background shell script.

What I have tried?

  1. setuid from Unix & Linux @ StackExchange, but it seems to be a very bad idea.
  2. echo 'my_insecure_password' | sudo -S command, it didn't work*.

I did not try Polkit, which was suggested in other answers, due to the level of its complexity. But I am willing to go for it with proper directions.

  • 1
    Can you explain, better, the 2nd paragraph. My interpretation is that /dev/usb/lp0 is added when the printer is plugged in, but not if you are in group lp. Is this correct understanding? As it seems odd. – ctrl-alt-delor Jul 9 '16 at 21:15
  • Yes, that's correct. It does seem odd. Maybe I should try it once again. – Suraj Thapar Jul 9 '16 at 21:17
  • @steeldriver Please elaborate and post your comment (from askubuntu) as an answer here. – Suraj Thapar Jul 9 '16 at 21:30
  • 4
    AFAIK udev rules will be run as root - you shouldn't need sudo in your script. However, you shouldn't need a script at all: you can write a rule that sets the MODE directly - see the example Controlling permissions and ownership from this Writing udev rules tutorial – steeldriver Jul 9 '16 at 21:30
6

Adding yourself to the lp group is probably the best solution here. That would not cause the lp0 file not to appear. (It's theoretically possible that your system has been configured to cause lp0 not to appear if you're in the lp group, but 1. that would have to be a local configuration, not a default setup from a distribution; and 2. I don't see why someone would have set this up.)


What follows is for academic interest only. In your scenario, you don't need this.

If you needed to change the permissions on the device file, then How to run custom scripts upon USB device plug-in? is not exactly what you need — that's for more complex cases that require a script. To change the Unix permissions or the ownership on a device file, use OWNER, GROUP and MODE assignments directly in the udev rule. That is, do create a file under /etc/udev/rules.d, but the line in that file should have something like GROUP="mygroup" instead of RUN="/path/to/script".

If you want to do something more complex, such as setting an access control list, you'll need to run a script. You don't need to escalate permissions in that script: it's already running as root! Just call the program you need to run as root, e.g. setfacl.

2

I do not recommend this solution, for this problem.

You can edit /etc/sudoers to configure sudo to allow execution of a script without a password.

  • includedir /dev/usb/ is this a correct entry to /etc/sudoers? – Suraj Thapar Jul 9 '16 at 21:32
  • 2
    NOOOOOOOOO: That will read in the files in dev/usb and try to interpret them as rules for sudo. Also what I guess you are trying to do also looks like a bad idea: telling sudo to allow any operation on a directory, especially such a sensitive one, will not end well. – ctrl-alt-delor Jul 9 '16 at 21:57
  • 1
    I think I should go for steeldriver's solution for this. Thanks – Suraj Thapar Jul 9 '16 at 22:01

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.