Automount cifs Network Shares

Problem: I use a VPN to connect to my office. I have network shares that I use, but I don’t want to have to go through the routine of mounting the shares every time I login. If I forget, the software I use lags trying to find a directory that doesn’t exist. If I forget to unmount, my system will sometimes lag after I disconnect from the VPN.

I found the solution in systemd.

I have written this tutorial with Debian-based systems in mind. I’m pretty sure this will work with other systems that have systemd, but I have not tested.

↦ Edit: I use vim. If you don’t know, you can :wqto save and exit. If you don’t know how to use vim, use nano or whatever other garbage text editor you like.

↳Preamble, make sure you have cifs-utils installed:

Of course, none of this makes sense if you don’t already have cifs-utils installed:

$ sudo apt install cifs-utils

You should also do a quick check to make sure you have all the options you want in place. I mount with appropriate uid and gid in place. For example:

$ sudo mkdir /mnt/MyShare 
$ sudo mount.cifs //192.168.1.123/MyShare /mnt/MyShare -o uid=1000,gid=1000,user=myusername
Password for myusename@//192.168.1.123/MyShare: *******

If that works correctly, now comes the auto-magic part.

↳First, create credential files

Best practice, as far as I know, is to store credentials files in /root with 600 permissions:

$ sudo vim /root/.mycreds

username=myusername
password=mypassword

$ sudo chown root:root /root/.mycreds
$ sudo chmod 600 /root/.mycreds

You can test your credential file like so:

$ sudo mount.cifs //192.168.1.123/MyShare /mnt/MyShare -o uid=1000,gid=1000,credentials=/root/.mycreds

↳SystemD .mount and .automount files

The auto-magic part happens with these two files.  ↦ For reference, .mount and .automount.

Here are the steps I took to create these files:

$ sudo vim /etc/systemd/system/mnt-MyShare.mount

[Unit]
Description=Mount MyShare drive

[Mount]
Where=/mnt/MyShare
What=//192.168.1.123/MyShare
Options=uid=1000,gid=1000,credentials=/root/.mycreds
Type=cifs

A few things to note here. First, the filename must match where you intend to mount the share. So if you created a directory, say /myshares/MyShare, your filename would be myshares-MyShare.mount

Also, note the ‘Where’ and the ‘What’ options.

Now for the auto-magic bit:

$ sudo vim /etc/systemd/system/mnt-MyShare.automount

[Unit]
Description=MyShare automount

[Automount]
Where=/mnt/MyShare
TimeoutIdleSec=5

[Install]
WantedBy=multi-user.target

So the way this works is that the system will call the automount when the directory is accessed. (I recommend you read the docs referenced above. They explain it in much better detail than I do.)

↦Note that the filename matches that of the .mount file. (Again, read the docs for the ‘why’.)

I chose the option TimeoutIdleSec=5 so that the directory is unmounted after 5 seconds. I usually just need to save a file using software once or twice, then I’m done. So this is sufficient for my use-case.

↳Enable the automount with systemctl

All that’s left is to enable the automount file. ↦Note that we’re enabling the automount, not mount.

$ sudo systemctl enable mnt-MyShare.automount

To test, you can connect to your VPN (or if this is just within your LAN), just pop open a terminal and:

$ ls -lah /mnt/MyShare

You should see an output of what’s in your share. If you’re not connected to the correct network, you should see ls: cannot access '/mnt/MyShare/': No such device

I’m no Debian guru, but this works well for me. If you have any questions, please refer to the docs first. I can try to help, but I’m not -that- savvy.

If there is a better way of doing this, I’m all for suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *