How to automatically mount network share upon connecting to my home wifi network?

I think it might involve systemd, but when I connect to my specified network, I want to mount my network drive (which is a usb drive plugged into my router)

I already have the mounting part working, I have it set up as a bash alias, but obviously having it mount automatically would be better.

Does anyone know how to do this? I saw some related articles but nobody was able to figure it out in the threads I saw.

1 Like

mount in fstab, read wiki for net options

how you mount ? protocol is smtp ?

sudo mount -t cifs -o username=admin //192.168.1.1/USB_Storage ~/readyshare

all I need is for this command to run every time I connect to my network, and I prefer a command line approach :slight_smile:

The easiest is probably a NM dispatcher script that starts after NM established a connection to your network. You can specify the network connection, but it needs to be unique for your home network.

https://wiki.archlinux.org/title/NetworkManager#Mounting_of_SMB_shares

2 Likes

Adding this line to your /etc/fstab file the share will be mounted on demand (when you access to it) that is the recommended option:
//192.168.1.1/USB_Storage ~/readyshare cifs _netdev,nofail,username=admin,x-systemd.automount 0 0

If you need that this share to be always mounted, simply remove the x-systemd.automount part:
//192.168.1.1/USB_Storage ~/readyshare cifs _netdev,nofail,username=admin 0 0

I just want to add that this method will always try to mount. It does not depend on the network your system is currently connected to.

I know this doesn’t exactly answer @WinterMadness’ question, but I think it might work.

I think using the mount on demand method will prevent the system from trying to mount it if the ~/readyshare directory is not browsed. Still, the “_netdev” and “nofail” options would prevent problems whether you are not connected to any network, or to foreign networks.

But I may be wrong.

Your solution will work and it is the recommend solution if your system is always connected to the same network.

But imagine the system is not connected to your home network. For example a school/university, work or cafĂ© network. In these cases you usually don’t want your system to try to connect to a IP you don’t know anything about. If your lucky the IP is not reachable in this network. But what if the IP is assigned to a system? Do you want your system to send the authentication information for your Home Network share to this system?
In these cases a system should not try to attempt to mount a share.

1 Like

my idea is that I should be able to check the SSID & whether or not I am currently connected, then decide to run the script

if the SSID doesnt match mine, then it simply doesnt do anything

It is not automatically, if you personally wish to check to SSID and then decide to run a script.

In NM, every connection has its own UUID. Use nmcli connection show . In a dispatcher script you can do a if statement to check the UUID and then run commands or not. Read the Arch Wiki article, it describes how a script can look like.

1 Like

I’ve marked this answer as the solution to your question as it is by far the best answer you’ll get.

However, if you disagree with my choice, please feel free to take any other answer as the solution to your question or even remove the solution altogether: You are in control! (If you disagree with my choice, just send me a personal message and explain why I shouldn’t have done this or :heart: or :+1: if you agree)

:innocent:
P.S. In the future, please don’t forget to come back to your question after your issue has been solved and click the 3 dots below the answer to mark a solution like this below the answer that helped you most:
Solution
so that the next person that has the exact same problem you just had will benefit from your post as well as your question will now be in the “solved” status.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.