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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0