Why does reading a device require admin permissions?
Firstly there are couple of issues here
mount'ing the physical storage device and the partitions it contains.
- Accessing and manipulating the files on it.
If the filesystem is permissions based, e.g. ext2,3,4 then permissions are defined on a per file basis.
In terms of why you would require the user to have special admin privileges to mount a device there are a few reasons, which are more likely to apply to enterprise type situations and less likely to apply to personal computing - although it still can be relevant
- It prevents reading in abusive programs
Once a disk is mounted an entire collection of untrusted programs are now potentially available for execution which can abuse the operating system. If you were administering that system, you could be more confident that wouldn't happen if casual users couldn't upload their own programs off their own disks.
- Writing / saving sensitive secret data
If you had corporate secrets, or users passwords on a system, and someone could connect an unauthorised storage device they can make copies to it.
You can get around the sudo issue by either running the entire script as sudo, and then using
sudo to switch back to an ordinary user inside the script for the commands you dont want to run as root (yes you can do that) e.g.
# this dd command now works
dd if=<source> of=<target> bs=<byte size>
# normal cammand you want to run as "mathmaniac" user
sudo -u "MathManiac" bash -c "touch foo.bar"
So you then run this script with:
Another quick and dirty but generally not recommended way is simply to put sudo in front of the command inside the script, when the interpreter encounters the
sudo it will halt script execution and ask you for your sudo password before continuing.