mount usb drive as root

function mountdrive(){
  disk=$(fdisk -l | grep "Disk /dev/sd" | tail -n 1 | grep -oP "/dev/sd\S")

  if [ ${#disk} -eq 0 ]; then
    echo "no usb drive found"
    return
  fi
  target="/root/mount"

  read -p "Mount $disk to $target? (y/n): " res

  if [ "$res" != "y" ]; then
    return
  fi

  mount $disk $target
  echo "mounted"
}

you might want to disable automatic mounting of drives

1
2
3
4
systemctl stop udisks2.service
systemctl disable udisks2.service
systemctl status udisks2.service # should be stopped and disabled
systemctl mask udisks2.service # this makes sure it cannot be restarted by another process
Edit

Pub: 10 Nov 2023 18:56 UTC

Edit: 10 Nov 2023 19:06 UTC

Views: 59