Mount USB with exec flag by default [closed]

I have en ext4 file system with a program that I run off of it. However, every time I boot my computer I have to remount the drive with the exec flag in order to run the application. How would I go about editing /etc/fstab to mount the drive with the exec flag by default instead?


Common permissions with Microsoft file systems in Linux

When mounting Microsoft file systems (NTFS and FAT) you set the permissions of all files and directories.

Individual permissions with Linux file systems

But with linux file systems (you have an ext4 file system) you can set and should set the permissions of files and directories individually.

When you create files in a directory they will inherit the permissions from the directory. So I suggest that you modify a directory, where you have your program(s) and shellscripts,

sudo chmod ugo+x /path-to/directory-name

This time, you have already your program file, so modify its permissions

sudo chmod ugo+x /path-to/program-name

Edit 1: You may also want to change the ownership of some directories and files individually, which is also possible and recommended in linux file systems.

sudo chown user:group /path-to/directory-name

and

sudo chown user:group /path-to/file-name

where user and group should be replaced by the actual user-ID and group-ID, that you want to own the file (the group-ID can be skipped, or set to the same as the user-ID).

Edit 2:

A line with mount option exec in fstab for a USB drive with ext4

It works for me (in Ubuntu 16.04 LTS as well as in Artful to become 17.10) to run executable files in ext4 file systems, when automounted as well as when mounted via /etc/fstab without the mount option exec.

But it is not the case for you. So I tested to add a line into /etc/fstab, with mount option exec (in Artful to become 17.10), and it works for me. I hope this will solve your problem.

  • Create a mountpoint

    sudo mkdir -p /mnt/usb-ext4
    
  • Identify the UUID of the partition in the USB drive to be mounted

    sudo blkid
    

    Use the string without quotes.

  • Edit /etc/fstab

    sudo nano /etc/fstab
    

    I added the following lines in my test

    # external drive with ext4 partition
    UUID=984666a5-594c-4edc-93a9-8923e6f52c80 /mnt/usb-ext4   ext4    defaults,exec,errors=remount-ro 0 2
    

Edit 3:

Another line with mount options user,noauto,exec in fstab

When you add the line in the previous paragraph into fstab, the system wants the USB drive to be inserted. If you want to boot without it, you can get along if you add the mount options user,noauto to the option list in that line of fstab.

UUID=984666a5-594c-4edc-93a9-8923e6f52c80 /mnt/usb-ext4   ext4    user,noauto,exec,errors=remount-ro 0 2

But you have to initiate mounting afterwards, for example with

mount -L <label>

or

mount <mountpoint>

in my example

mount -L test-exec

or

mount /mnt/usb-ext4

The same user can unmount it

umount /mnt/usb-ext4

Test output

After rebooting I ran the following commands.

mtab:

$ grep /mnt/usb-ext4 ext4 /etc/mtab
/dev/sdb1 /mnt/usb-ext4 ext4 rw,relatime,errors=remount-ro,data=ordered 0 0

fstab:

$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=10880524-3839-4142-b7db-f65845d87825 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sda1 during installation
UUID=E556-B809  /boot/efi       vfat    umask=0077      0       1
/swapfile                                 none            swap    sw              0       0
# external drive with ext4 partition
UUID=984666a5-594c-4edc-93a9-8923e6f52c80 /mnt/usb-ext4   ext4    defaults,exec,errors=remount-ro 0 2

I created a directory and changed ownership:

cd /mnt/usb-ext4
sudo mkdir bin
sudo chown $USER:$USER bin

Then I created a small shellscript and made it executable:

cd bin
echo 'echo Hello World'>hello
chmod ugo+x hello

Long list to check the permissions and ownership:

$ ls -l
totalt 4
-rwxrwxr-x 1 tester tester 17 okt  6 07:51 hello

and it can be run

$ ./hello
Hello World

General commands identifying the system

lsb_release:

tester@tester-SATELLITE-PRO-C850-19W:/mnt/usb-ext4/bin$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu Artful Aardvark (development branch)
Release:    17.10
Codename:   artful

uname:

tester@tester-SATELLITE-PRO-C850-19W:/mnt/usb-ext4/bin$ uname -a
Linux tester-SATELLITE-PRO-C850-19W 4.13.0-12-generic #13-Ubuntu SMP Sat Sep 23 03:40:16 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

blkid:

tester@tester-SATELLITE-PRO-C850-19W:/mnt/usb-ext4/bin$ sudo blkid
/dev/sda1: UUID="E556-B809" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="b3276a58-ea15-4cea-8c74-095b13ea7aa6"
/dev/sda2: UUID="10880524-3839-4142-b7db-f65845d87825" TYPE="ext4" PARTUUID="d399063d-1c12-4a62-86d9-0112b15a3e40"
/dev/sdb1: LABEL="test-exec" UUID="984666a5-594c-4edc-93a9-8923e6f52c80" TYPE="ext4" PARTUUID="4b07dce4-4bde-4fe9-9b2f-2442a62b0b87"

lsblk:

tester@tester-SATELLITE-PRO-C850-19W:~$ sudo lsblk -fm
[sudo] lösenord för tester: 
NAME   FSTYPE LABEL     UUID                                 MOUNTPOINT     SIZE OWNER GROUP MODE
sda                                                                        55,9G root  disk  brw-rw----
├─sda1 vfat             E556-B809                            /boot/efi      480M root  disk  brw-rw----
└─sda2 ext4             10880524-3839-4142-b7db-f65845d87825 /             55,4G root  disk  brw-rw----
sdb                                                                        30,2G root  disk  brw-rw----
└─sdb1 ext4   test-exec 984666a5-594c-4edc-93a9-8923e6f52c80 /mnt/usb-ext4 30,2G root  disk  brw-rw----
sr0                                                                        1024M root  cdrom brw-rw----