I have trying to get a script to run at startup to mount a flash drive and change ownership on dynamically created files. I have not been successful in getting the script to run. It only runs when I log in a run it under my username.

I am running Stretch version 9.

Basically, I need to run the following in order for my mounted flash drive to work.

cd /media
sudo umount /media/usb
sudo chmod 777 usb -R
sudo mount -t ntfs-3g -o uid=myusername,gid=root /dev/sda /media/usb/

Does anyone have any suggestions for how to get a script to run at reboot on this version? Or where I could go to learn more about it?

Crontab -e does not work. I have tried putting the script into /etc/init.d/ to no avail.

Thank you all so much in advance!

  • 1
    When you say, "crontab -e does not work", do you mean that your crontab is not opened in your text editor (e.g. nano) when you enter this command, or do you mean that the action you set up in your crontab file to run @ reboot doesn't do what you want? If the latter, could you show us your crontab entry? – Seamus Oct 25 '19 at 13:12

A simple solution.

  • Append your commands to rc.local execute this command in the console

    sudo nano /etc/rc.local

and add your command to the file.

  • further reading here

Note: Usage of /etc/rc.local has been depreciated due to compatibIlity with SysV as pointed out by Ingo.

  • Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. – Ingo Oct 24 '19 at 16:22
  • ok i will note that.. should I add that to the answer? – WizzyGeek Oct 24 '19 at 16:25
  • Yes I would do that. The site you have linked seems to be an older one or they don't know that rc.local isn't supported in the future as noted in the last sentence of the Compatibillity notes: "Note that there are some areas where systemd currently provides a certain amount of compatibility where we expect this compatibility to be removed eventually." – Ingo Oct 24 '19 at 16:37

A quick observation: Are you sure using /dev/sda works? It is much more likely to be /dev/sda1, which I'll use here. If you are positive the stick was formatted without partitions, then it would be /dev/sda, but this is a very unusual thing to do, particularly since it's implicit this was formatted on Windows.

You can use /etc/fstab for this; see also man fstab. Add a line

/dev/sda1  /media/usb   ntfs-3g   uid=[N],gid=0   0  0 

The drive will be automatically mounted at boot if present. [N] should be the user's numerical ID (get this with id [username]). I am not sure that gid will work in this context. You can test this without rebooting by using just sudo mount /dev/sda1.

Note that if you have multiple drives on the USB bus, which drive will be /dev/sda is not predictable. In this case you may prefer to use UUIDs as mentioned in the man page (or many places if you search online). On the other hand, if it's always just one (ntfs formatted) stick but not always the same one, the dev node is preferable.


Thank you all for posting!

I found that the easy solution was to edit the /etc/rc.local file. I added the script to this file and it runs it at startup. I am reading that this method is obsolete! So it might not work in the future. It does for now and that is good enough for me. Thank you for your answers!

sudo nano /etc/rc.local

  • No need to make an answer. Better you accept the answer from Teen_boom with a tick on the tick on its left side and append your "answer" as comment. – Ingo Oct 26 '19 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.