1

I have a shared folder on windows and I usually mount it like this

mkdir /media/somedir
sudo mount -t cifs //servername/sharename /media/somedir

The problem is that once I restart the machine the mounted shared folder is no longer their and I have to run the above commands again. I wonder if there is a way to automount it when trying to access this directory? I've checked with @Gilles commenting to his answer found here and as shown in the below screenshot, he said it can be done by editing the file but it's not recommended and he said may be there is a way to automount it when trying to access the directory and he suggested me to ask a question as someone may be able help. Any advice?

enter image description here

Tak
  • 529

3 Answers3

1

You can add an entry to your fstab like this:

//192.168.1.2/SambaPi    /home/pi/shares/pi    cifs    defaults,noauto,nofail,username=pi,passwd=raspberry,x-systemd.automount,x-systemd.requires=network-online.target    0    0

(Copied without testing from here). The nofail argument will make sure that even if mounting fails booting won't be interrupted. The noauto is optional and will prevent mounting the drive until it is accessed (see here for more details). The x-systemd parameters will tell the system to wait until the network is set up before mounting (Source).

You can also let it mount by a shell script you run on startup. The better version here is to create a systemd-module that does this for you. You can use systemd modules to unmount it safely on shutdown too. There are a loot of good tutorials for this out there.

piegames
  • 907
  • Disclaimer: I just googled this answer together linking the source of my knowledge. I am not responsible for anything. – piegames Mar 15 '18 at 17:10
  • 1
    There's a few things to note in all the answers here: When working with Windows shares on Win Server 2016, you need to push vers=3.0 in your options, because by default CIFS doesn't always try the latest SMB protocol versions. This resulted in some headaches at one of the places that employs me, until I dug into this a bit. – Thomas Ward Mar 15 '18 at 17:23
  • /etc/fstab is world-readable. using username= and password= works but puts the login & password in plain text in a file readable by anyone. use credentials=/path/to/file instead, as in @AyushGoyal's answer, to put the login & password in a separate file (that can be owned by root:root, and readable only by root...i.e. mode 600). See man mount.cifs for details. – cas Mar 16 '18 at 00:57
  • @cas but why Gilles https://unix.stackexchange.com/a/25494/85865 told me that updating the fstab is usually a bad idea, because it means your computer will get stuck while booting if the server is down. Any advice? – Tak Mar 16 '18 at 07:49
  • @Tak normally the nofail should take care of this. I had the same problem with an external HDD on my Pi and adding this stopped from failing to boot when it was umplugged. Just try it out. If it fails, you can comment out that line from the fstab in the emergency console and reboot normally again. – piegames Mar 16 '18 at 07:53
  • noauto and nofail both solve that particular problem. they can be used by themselves or together. noauto prevents the fs from being auto-mounted at boot (can only be manually mounted from the command line - e.g. with mount /media/somedir (the rest of the details come from the /etc/fsstab file). nofail stops systemd from caring if the automount fails. – cas Mar 16 '18 at 07:53
1

Let's say you want to share a file from one machine to another using Samba, since you have used cifs which is a newer file system.

Assuming that you have all the required packages and that you have made all the necessary configurations in the smb.conf file, I will limit this answer to client side configuration.

To permanently mount the shared directory,open the fstab file, which has the path /etc/fstab, and make the following entries:

//<IP>/<share-name-of-directory> /mount/point cifs credentials=/root/creds.txt,sec=ntlmssp,multiuser,defaults 0 0

After making the entry in this file, run the command mount -a. Credentials will take the path of the file which has username and password of the user which is allowed to mount the directory on the system. After that user logs in, the user will be required to get the access from the server by using the following command:

cifscreds add <ip-of-server>

After that user will be able to access the mounted directory. The directory need not be mounted again and again since the entry has been made in the fstab file. The fstab file is read during the boot process, and the credentials are provided so that the system itself is able to mount the directory from server with these credentials.

The mount command itself mounts directories only temporarily and hence the directories get unmounted when the system reboots.

  • There's a few things to note in all the answers here: When working with Windows shares on Win Server 2016, you need to push vers=3.0 in your options, because by default CIFS doesn't always try the latest SMB protocol versions. This resulted in some headaches at one of the places that employs me, until I dug into this a bit. – Thomas Ward Mar 15 '18 at 17:23
  • but why Gilles https://unix.stackexchange.com/a/25494/85865 told me that updating the fstab is usually a bad idea, because it means your computer will get stuck while booting if the server is down. Any advice? – Tak Mar 16 '18 at 07:50
  • He probably suggested this because if the entries in fstab file are erroneous, the system will definitely get stuck while booting because it will not be able to understand the instruction that has been written. To be on the safer side, we run mount -a immediately after making the changes in fstab file. The command mount -a refreshes the fstab and if there are any errors, it shows them on the terminal itself so that those can be corrected to prevent the unfortunate circumstance of system being stuck at boot up – Ayush Goyal Mar 16 '18 at 08:13
0

Assuming that you have Samba & CIFS Utilities installed, I found that adding 2 entries to /etc/fstab did the trick.

/dev/sdb1 /mnt/sdb1   ntfs-3g defaults 0 0
/dev/sdb1 /mnt/sdb1   cifs    defaults 0 0

When you reboot Linux you will find that the drive is mounted and shared.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232