13

I'm trying to automatically mount a network drive at startup by editing /etc/fstab but doesn't work.

If I execute this líne,

sudo mount.cifs //192.168.0.67/test /home/pi/test -o username=myname,password=123

it works great. But I don't know how to properly write the same in /etc/fstab.

15

Each line in the /etc/fstab file contains the following fields separated by spaces or tabs:

file_system    dir    type    options    dump    pass

A typical mount point added in /etc/fstab would look like the following:

# <file system>        <dir>         <type>    <options>             <dump> <pass>
/dev/sda1              /             ext4      defaults,noatime      0      1

You can't simply add a mount statement in the file.

Add this line to the end of your /etc/fstab file:

 //192.168.0.67/test  /home/pi/test  cifs  username=myname,password=123,iocharset=utf8,sec=ntlm  0  0

After the /etc/fstab is edited you can test by mounting the filesystem with mount -a which will check fstab and attempt to mount everything that is present.

| improve this answer | |
  • 1
    what is the remote folder is only accessible with a public key instead of a password? – m4l490n Apr 25 at 2:01
9

In addition to 에이바's answer, you may want to place the credentials in a specific file called .smbcredentials in your home directory for a little more security. This is a good practice especially for multiuser systems. This way you will be protecting your cifs password. Create a file: /home/myname/.smbcredentials and include just two lines:

username=myname
password=123

Set your permissions: $ chmod 600 .smbcredentials

Then in /etc/fstab include the following line:

//192.168.0.67/test  /home/pi/test  cifs  credentials=/home/myname/.smbcredentials,iocharset=utf8,sec=ntlm  0  0

Be sure to test with a reboot.

| improve this answer | |
  • 1
    Also, don't forget to sudo chown root /home/myname/.smbcredentials if you created it by your normal user. – bytebuster Feb 24 '18 at 13:45
  • You can also test without reboot by running mount -a – 에이바 Dec 11 '18 at 17:45
0

Sorry if this appears to highjack your thread, it is related and I spent hours trying to get the advice posted here to work with my Raspberry Pi before I gave up and came up with this alternative for the Raspberry Pi users out there....note that this should also work on Ubuntu 18.04.

I tried to get the above advice to work with Raspberry Pi Raspbian buster desktop on my Raspberry Pi 3+, but all I could get to work reliably was the command line version posted by user6354 at the beginning of this thread. However, I was able to place that line with a little editing into a file in the /home/user/.config/autostart folder on my pi and got it to work.

The contents of the file should look like the following (make changes for your situation - see below):

[Desktop Entry] Encoding=UTF-8 Name=OurCloud_share Name[en_GB]=OurCloud_share GenericName=OurCloud_share Comment=Script to mount OurCloud_share TryExec=lxterminal Exec=lxterminal -e "sudo mount.cifs //192.168.1.xxx/sourcefolder /home/pi/targetfolder -o username=loginID,password=userpassword" Icon=lxterminal Type=Application StartupNotify=true Categories=GTK;Utility;TerminalEmulator; Name[en_US]=mount_OurCloud.desktop

Where:

OurCloud_share = whatever you want to call your NAS drive access

192.168.1.xxx = replace this with your NAS TCP/IP address on the network

sourcefolder = shared folder on your NAS drive

targetfolder = the folder where you want the mounted NAS folder contents to appear (Create this folder and ensure you make it read/write if you want two way access to it)

loginID = the user ID for logging in to your NAS via network

userpassword = the password to use with the loginID

Name the file something meaningful (here I use "mount_OurCloud.desktop". The ".desktop" file type is required.)

Save this and then use root authority to set the file permission for executable.

Reboot and your NAS folder should be visible in the target folder.

| improve this answer | |

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.