Ever wondered why you need sudo or root privileges to mount an external drive or partition as a regular user from the terminal, but not from the file manager on Linux? As you already know, you need to be a sudo user in order to display, mount and access removable media (such as external hard drives, USB drives, optical drives and cameras) from the command line. But how do you mount these removable storage devices without root or sudo using a graphical file manager (like Nautilus)? In this quick tutorial, I will explain how a file manager mounts external drives without sudo or root privileges in Linux.
How can a file manager mount an external drive without sudo or root privileges in Linux?
File managers use UDisks2 to mount external drives without administrator rights. UDisks2 is the second version of UDisks. UDisks is deprecated. GNOME, KDE and various other desktop environments now use UDisks2 to enable regular users to mount removable media.
$ sudo systemctl status udisks2
● udisks2.service - Disk Manager Loaded: loaded (/lib/systemd/system/udisks2.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2020-09-09 12:09:21 IST; 3h 9min ago Docs: man:udisks(8) Main PID: 978 (udisksd) Tasks: 5 (limit: 9336) Memory: 9.3M CGroup: /system.slice/udisks2.service └─978 /usr/lib/udisks2/udisksd Sep 09 12:08:51 ostechnix systemd: Starting Disk Manager... Sep 09 12:09:00 ostechnix udisksd: udisks daemon version 2.8.4 starting Sep 09 12:09:21 ostechnix udisksd: Acquired the name org.freedesktop.UDisks2 on the sy> Sep 09 12:09:21 ostechnix systemd: Started Disk Manager.
The udisksctl CLI utility is used to query and use the daemon.
The actions a user can take with udisk are limited with Polkit.
Polkit is an application-level toolkit for defining and handling authorization.
It allows non-privileged processes to communicate with privileged processes.
Since UDisks is already running as root, it allows unprivileged programs (like file managers) to mount or unmount storage devices without sudo or root privileges.
This is how file managers mount an external drive without administrator rights.
You can test this yourself by plugging in a USB stick as a normal user using the udisksctl command as shown below:
$ udisksctl mount -b /dev/sdc1
$ udisksctl mount --block-device /dev/sdc1
Replace / dev / sdc1 with your device name. Conclusions:
Mounted /dev/sdc1 at /media/sk/ventoy.
Similarly, you can unmount your USB drive with the command:
$ udisksctl unmount -b /dev/sdc1
$ udisksctl unmount --block-device /dev/sdc1
If you’ve ever looked for a way to allow regular users to securely mount a filesystem on Linux without superuser privileges, Udisks2 is recommended!