Advertisement
Tritonio

How To Mount A USB Flash Disk On The Raspberry Pi

Mar 15th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1.  
  2.  
  3. Whether you call them USB flash disks, memory sticks, keys or drives they are a useful accessory in the world of computers. By now many people will have a collection of various devices of varying capacities. Using them with the Pi is a great way of getting some use out of them rather than letting them gather dust in a drawer.
  4.  
  5. SanDisk Cruzer Micro USB Flash DriveI tend to transfer files from my Pi using FTP but occasionally it is useful to get stuff onto a USB drive. There are lots of guides already out there but I had a few issues using most of them, mainly due to permission problems. My main goal was to clarify the process by which I could mount standard FAT32 drives and allow the default Pi user read/write permissions.
  6.  
  7. So here is my procedure for using USB flash drives with the Raspberry Pi where the Pi user has permissions to use it without needing “sudo”. It works great with the shiney new Kingston Digital 32GB DataTraveler Micro I’ve just bought myself.
  8. PiHub by PimoroniStep 1 – Plug In The Device
  9.  
  10. The first step is to plug in your USB stick. If you are using a mouse and keyboard you will need a decent USB hub at this point. (e.g. the PiHub by Pimoroni).
  11. Step 2 – Identify The Devices Unique ID
  12.  
  13. In order to find the unique reference (UUID) for your drive run the following command in the terminal :
  14.  
  15. ls -l /dev/disk/by-uuid/
  16.  
  17. This will give you an output that should list your drive :
  18.  
  19. Mount USB Stick
  20.  
  21. The line will usually refer to “/sda” and in this example it is “sda1”. My ID is “18A9-9943”. Note down yours.
  22.  
  23. You would need to repeat this step if you wanted to use a different device as the UUID would be different.
  24. Step 3 – Create a Mount Point
  25.  
  26. A mount point is a directory that will point to the contents of your flash drive. Create a suitable folder :
  27.  
  28. sudo mkdir /media/usb
  29.  
  30. I’m using “usb” but you can give it whatever name you like. Keep it short as it saves typing later on. Now we need to make sure the Pi user owns this folder :
  31.  
  32. sudo chown -R pi:pi /media/usb
  33.  
  34. You will only need to do this step once.
  35. Step 4 – Manually Mount The Drive
  36.  
  37. To manually mount the drive use the following command :
  38.  
  39. sudo mount /dev/sda1 /media/usb -o uid=pi,gid=pi
  40.  
  41. This will mount the drive so that the ordinary Pi user can write to it. Omitting the “-o uid=pi,gid=pi” would mean you could only write to it using “sudo”.
  42.  
  43. Now you can read, write and delete files using “/media/usb” as a destination or source without needing to use sudo.
  44. Step 5 – Auto Mount
  45.  
  46. When you restart your Pi your mounts will be lost and you will need to repeat Step 4. If you want your USB drive to be mounted when the system starts you can edit the fstab file :
  47.  
  48. sudo nano /etc/fstab
  49.  
  50. Then add the following line at the end :
  51.  
  52. UUID=18A9-9943 /media/usb auto,users,rw,uid=pi,gid=pi 0 0
  53.  
  54. My fstab file looks like this :
  55.  
  56. USB Stick Automount Fstab File
  57.  
  58. Make sure you set the correct UUID. Use CTRL-X followed by Y to save and exit the nano editor.
  59.  
  60. Now reboot :
  61.  
  62. sudo reboot
  63.  
  64. Your USB drive should be auto-mounted and available as “/media/usb”.
  65. Step 6 – Un-mounting The Drive
  66.  
  67. You don’t need to manually un-mount if you shutdown your Pi but if you need to remove the drive at any other time you should un-mount it first. Only the user that mounted the drive can un-mount it.
  68.  
  69. umount /media/usb
  70.  
  71. If you used the fstab file to auto-mount it you will need to use :
  72.  
  73. sudo umount /media/usb
  74.  
  75. If you are paying attention you will notice the command is “umount” NOT “unmount”!
  76. An Extra Note About File Systems
  77.  
  78. In the examples above I let the mount command automatically determine the file system of the device. This seems to work ok for me. If you need to you can manually specify the file system by adding the “-t” parameter. For example to specify a FAT32 device you would use :
  79.  
  80. sudo mount /dev/sda1 /media/usb -o uid=pi,gid=pi -t vfat
  81.  
  82. If your drive uses NTFS, EXT3 or EXT4 you will need to replace references to “vfat” with “ntfs-3g”, “ext3” or “ext4”.
  83.  
  84. If you are using NTFS you will also need to install the following package :
  85.  
  86. sudo apt-get install ntfs-3g
  87.  
  88. Final Thoughts
  89.  
  90. This technique suits my applications but the main disadvantage is that it is specific to a known USB device given we are using the device ID. However if you created a few mount points in advance you could manually mount a new device to a spare mount point without worrying about updating the fstab file.
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement