74

What is the command line equivalent of the Nautilus feature called "Safely Remove Drive". Specifically, I am removing a USB flash drive.

Andrew Kozak
  • 103
  • 4
H2ONaCl
  • 9,693

5 Answers5

77

The udisks command is most likely what you are looking for.

While sudo unmount /dev/sdXY will work, udisks can do this without root level (sudo) permissions.

If you have a drive /dev/sdXY, mounted, where X is a letter representing your usb disk and Y is the partition number (usually 1), you can use the following commands to safely remove the drive:

udisks --unmount /dev/sdXY
udisks --detach /dev/sdX

For a practical example, if I have the partition /dev/sdb1 mounted, I would run this to unmount and detach it:

udisks --unmount /dev/sdb1
udisks --detach /dev/sdb

If your drive is not mounted, or was never mounted, simply use the second command:

udisks --detach /dev/sdb

I originally found this through this question: https://superuser.com/a/430470/176493.

Using udisks2:

In the newer ubuntu distributions (I'm unsure of when the switch occurred), udisks2 is installed instead of udisks.

Mirroring the commands above, to unmount and detach a disk with udisks2:

udisksctl unmount -b /dev/sdXY
udisksctl power-off -b /dev/sdX

Example if my drive is /dev/sdb1:

udisksctl unmount -b /dev/sdb1
udisksctl power-off -b /dev/sdb

Similarly to above, power-off can be used to detach the drive even if there are no partitions mounted, or no partition was ever mounted:

udisksctl power-off -b /dev/sdb
daboross
  • 2,153
  • I get Detach failed: Not Authorized when I try to run it without root privileges. (Not a serious issue, but a small correction.) – Jonathan Y. Oct 18 '15 at 09:59
  • What version of Ubuntu are you running? Also, how did you mount the drive? If you mounted using root permissions, or when logged in as another user (as opposed to Nautilus or udisks in the current user), your user won't have permission to unmount using udisks. – daboross Oct 18 '15 at 13:51
  • 2
    Yes, I mounted with sudo, but I also umounted before trying to --detach. It's a trusty release of ubuntu server. – Jonathan Y. Oct 18 '15 at 13:54
  • Ah, might be a difference with Ubuntu server then. Detach does work without root permission on my desktop. – daboross Oct 18 '15 at 22:19
  • @JonathanY. the difference lies that it was the root user that mounted the device, while udisk inherit your own user preferences. This has nothing to do with being server vs desktop, more like "why the heck user B is trying to mess with user A device?" – Braiam May 12 '16 at 22:29
  • @Braiam I would imagine the detach failing would be a server/desktop thing though. – daboross May 14 '16 at 03:14
  • I don't have udisks command on Ubuntu 16.04. Sadness unlimited – morhook Jul 08 '16 at 17:50
  • 2
    @morhook see updated answer with udisks2 support! – daboross Jul 13 '16 at 23:49
  • In my experience udisks2 power-off is a bit different from the eject icon for a usb device in Nautilus. For example power-off literally cuts the power to the device. Eject does not. If you want to keep power to the device, I would recommend the "eject" command itself in the answer by Ciro. – SpiRail Feb 14 '18 at 11:13
  • @SpiRail ah, cool. I didn't realize "power-off" turned off power to the drive since I always have some drive lights still on after running it, but that makes sense from the name. Could you elaborate as to why one would prefer eject over power-off, though? I mean both sync drive contents and ensure the OS isn't writing new stuff, or at least that's what I assume. – daboross Feb 14 '18 at 22:59
  • 1
    awesome! Thank you for this! I have been trying to move all of my activities to the terminal as much as possible, and this is one that I could not figure out how to do, since I write iso's to USB sticks all of the time, and need a way to eject them via the cli. Thanks again! – DPS Feb 16 '20 at 00:49
  • What if the USB is plugged in but not mounted? I can see its partitions in gparted, but none of them are used on disk – Addison Klinke Oct 13 '21 at 15:25
  • 1
    @AddisonKlinke In that case, using only the second command (--detach or power-off depending on your udisks version) should work. – daboross Oct 14 '21 at 06:28
28

The actual equivalent to Nautilus Mount/Unmount operation is gvfs-mount -m -d /dev/ice /some/directory and gvfs-mount -u /some/directory. This uses the same API that Nautilus uses, GIO virtual file system (gvfs), which provides different tools to use several services as mount points, such smb, NFS, FTP, block devices, etc.

To identify which device you need to unmount just use gvfs-mount -l which should be enough.

This solution has the peculiarity that it doesn't require for elevated permissions, since everything is managed by the umount/gvfsd/polkit services, which further resemblances the similarity with Nautilus behavior.

Braiam
  • 67,791
  • 32
  • 179
  • 269
  • 3
    This seems to be the most correct answer. Although many of us are used to thinking of udisks (suggested in other answers) as "the way" non-root users mount and unmount removable disks on the desktop, as stated here gvfs-mount may use other mechanisms. (On my Ubuntu MATE Utopic system, the udisks package isn't even installed.) – Eliah Kagan Oct 06 '14 at 01:52
  • 4
    Ubuntu 18.40 has gio which is the replacement for gvfs-mount. – studog Mar 06 '19 at 19:12
  • 1
    @studog yes; specific example: gio mount -e /media/$USER/MY_STICK – jez Aug 23 '22 at 16:26
16

Once you know the device, possibly using the df info as in @rcpao answer, the best way to "eject" the disk is, imho, using the same command that the graphical interface is using:

udisksctl unmount --block-device /dev/sdc1

I have a script to do a backup to a disk that I know will mount under /media/romano/movlin, and after the backup I do:

sync
udisksctl unmount -b $(mount | grep movlin   | cut -d" "  -f1)

Here, mount | grep movlin | cut -d" " -f1 will extract the device that is mounted under the label "movlin", (would be /dev/sdc1 in that case), and then it unmounts it.

Rmano
  • 31,947
  • Is it really necessary to manually run sync before udisksctl unmount? – Eliah Kagan Oct 06 '14 at 01:57
  • 1
    Probably not. But I am quite unsure of the correct behavior of the cache in general; I sometime feel that the USB drive is still flashing after the umount... during few seconds. Let it say it's a remnant of the old sync; sync; sync && poweroff... – Rmano Oct 06 '14 at 04:24
  • @Rmano I have seen the flashing on a supposedly dismounted flash drive (usually done through a gui under KDE) many times. I try to wait it out before unplugging it. This does look (to my untrained eye) like something is not quite right. It would be even worse with a flash drive with no activity indicator light. – Joe Oct 08 '14 at 20:32
  • 1
    Rmano, since udisks is no longer present by default in Ubuntu udisksctl may be a preferred solution. udisksctl power-off --block-device /dev/sdc turns off the light on an external USB Flash drive so it might be suitable as the next command to follow yours. – H2ONaCl Jan 03 '16 at 01:38
  • Other way to get the device findmnt -n -M /media/romano/movlin -o SOURCE => /dev/sdc1 – Santa Claus Apr 10 '21 at 20:14
10
  1. 'ls -l /dev/disk/by-id/' to find the device names of your flash drive and its partitions.

    lrwxrwxrwx 1 root root  9 Sep 16 10:49 usb-PNY_USB_2.0_FD_AAC3170000000954-0:0 -> ../../sdk
    lrwxrwxrwx 1 root root 10 Sep 16 10:49 usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part1 -> ../../sdk1
    lrwxrwxrwx 1 root root 10 Sep 16 10:49 usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part2 -> ../../sdk2
    

    /dev/disk/by-id/usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part1 is a symlink to /dev/sdk1

  2. If your distribution does not have /dev/disk/by-id/, you can use 'df' to find the mount point of your flash drive.

    myuser@myhost:~$ df
    Filesystem                   1K-blocks       Used  Available Use% Mounted on
    /dev/mapper/ubuntu--vg-root 1916153032  658404668 1160390336  37% /
    none                                 4          0          4   0% /sys/fs/cgroup
    udev                          16438692          4   16438688   1% /dev
    tmpfs                          3289976       2156    3287820   1% /run
    none                              5120          0       5120   0% /run/lock
    none                          16449860      18768   16431092   1% /run/shm
    none                            102400         48     102352   1% /run/user
    /dev/sda1                       240972      98990     129541  44% /boot
    /dev/sdc1                     60915712      20992   60894720   1% /media/myuser/SD024-64GB
    /dev/sdk2                         1004        876        128  88% /media/myuser/UEFI_NTFS
    /dev/sdk1                      7970172    5798804    2171368  73% /media/myuser/NTFS
    

Thus /media/myuser/UEFI_NTFS -> /dev/sdk2 <- usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part2

At this point, see daboross's answer for unmount, power off, and detach commands using 'udisksctl' and 'udisks' with your discovered mount points and disk device name.

  1. 'udisksctl unmount -b' or 'umount' using either /dev/sdc1 or /media/myuser/SD024-64GB.

    myuser@myhost:~$ udisksctl unmount -b /dev/disk/by-id/usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part1
    Unmounted /dev/sdk1.
    myuser@myhost:~$ udisksctl unmount -b /dev/disk/by-id/usb-PNY_USB_2.0_FD_AAC3170000000954-0:0-part2
    Unmounted /dev/sdk2.
    

    or

    myuser@myhost:~$ sudo umount /dev/sdc1
    [sudo] password for myuser: 
    myuser@myhost:~$
    

    or

    myuser@myhost:~$ sudo umount /media/myuser/SD024-64GB
    [sudo] password for myuser: 
    myuser@myhost:~$
    
  2. You should be able to see the flash drive's eject icon disappear in nautilus as soon as umount finishes.

  3. 'udisksctl power-off -b /dev/sd{letter}{partition_number}' to turn off the disk.

    myuser@myhost:~$ udisksctl power-off -b /dev/disk/by-id/usb-PNY_USB_2.0_FD_AAC3170000000954-0:0
    

Notes:

  1. The device is typically of the format "/dev/sd{letter}{partition_number}" where {letter} is the disk, and {partition_number} is the partition number within that disk. Your disk may have more than one partitions mounted, so you will need to unmount all of the mounted partitions before the disk itself.
rcpa0
  • 594
  • 2
    df looks very useful for identifying where the device is. – H2ONaCl Oct 05 '14 at 19:17
  • 2
    For a flash drive, this is reasonable. For a disk drive, "Safely Remove" also spins it down, which this solution does not do (see the answer by DaboRoss for that). – Nate Eldredge Oct 06 '14 at 00:47
  • 3
    Yeah, udisks --detach is helpful for detaching drives completely after unmounting. – daboross Oct 06 '14 at 00:50
  • 1
    df and umount are standard UNIX utilities. It is always good to know them, because these will always work, while udisks is dependent on dbus and a running udisksd. – Simon Richter Oct 06 '14 at 04:25
  • 2
    @NateEldredge: "For a flash drive, this is reasonable..." – actually, it's not and I'm down voting for this reason: last year I had to do an app qa deployment and naively used a quick command along the lines of cp /path/to /drive && umount drive. This rendered 2 flash drives (temporarily) unwritable until I realised it was the command that caused it. Running "Scan & repair" on a Windows machine fixed the drives. DaboRoss' answer below is much safer. – Andy E Oct 06 '14 at 12:32
  • 1
    OK, I'm not down voting because I don't have enough rep X-D. But I'm certainly not up voting :-P. – Andy E Oct 06 '14 at 12:33
5

eject from the eject package:

sudo eject /dev/sdX

appears to umount all partitions, and put the device in a state that you must remove and reattach it to remount.

  • For a few application this eject command is the best way to do it. Most of the other answers are "good enough" but technically the Eject Icon on Nautalis does not do things like power-off the device. In my experience eject is closer to the functionality of the eject usb button itself. If you want to avoid sudo you can add yourself to the disk group (requires log-out and log in). The command for this is "sudo adduser user disk" – SpiRail Feb 14 '18 at 10:56