In this article, we’ll delve into the process of setting up your Ubuntu system to automatically remount a CIFS (Common Internet File System) network share whenever your network reconnects. This is particularly useful in scenarios where your network connection is unstable or if you’re frequently switching between different networks.
To automatically remount a CIFS network share on network reconnect in Ubuntu, you can use either the systemd automount feature or a script triggered by network events. The systemd method involves modifying the fstab entry and reloading systemd, while the script method requires creating a script in the /etc/network/if-up.d/ directory. Both methods have their advantages and can be customized to suit your needs.
CIFS is a network file system protocol that allows systems to share files and resources, such as printers. It’s a version of SMB (Server Message Block) and is commonly used in Windows networks, but it can also be used on Linux systems like Ubuntu.
Before we proceed, ensure that you have:
- A working Ubuntu system
- Root or sudo access
- A configured CIFS network share
Method 1: Using Systemd Automount
Systemd, an init system used in Linux distributions to bootstrap the user space and manage all processes subsequently, offers an automount feature which can be utilized for our purpose.
Step 1: Modify the fstab Entry
/etc/fstab file with a text editor of your choice (we’ll use nano):
sudo nano /etc/fstab
Add the following options to your fstab entry for the network share:
noauto,x-systemd.automount,x-systemd.idle-timeout=30. Here’s what they do:
noauto: Prevents the share from being mounted at boot.
x-systemd.automount: Mounts the share when it’s accessed.
x-systemd.idle-timeout=30: Unmounts the share if there’s no activity for more than 30 seconds.
Step 2: Reload Systemd and Restart the Remote-fs.target
After modifying the fstab file, run the following commands to reload systemd and restart the remote-fs.target:
sudo systemctl daemon-reload sudo systemctl restart remote-fs.target
Method 2: Using a Script Triggered by Network Events
Another approach is to use a script that will be triggered upon network events.
Step 1: Create the Script
Create a script in
/etc/network/if-up.d/ (for instance,
/etc/network/if-up.d/mount-share) that will be executed upon connecting to the network.
Here’s a simple script that checks if the network is the desired one (by checking the SSID) and then runs the
mount -a command to mount all entries in
#!/bin/sh if [ "$IFACE" = "eth0" ]; then mount -a fi
This script checks if the interface (
eth0 (replace with your network interface if different) and if so, runs
Step 2: Make the Script Executable
After creating the script, make it executable using the following command:
sudo chmod +x /etc/network/if-up.d/mount-share
In this article, we’ve explored two methods to automatically remount a CIFS network share upon network reconnection in Ubuntu. Both methods have their own advantages: the systemd method is simpler and requires less scripting knowledge, while the script method offers more flexibility and can be customized to suit your specific needs.
Remember to test your setup thoroughly to ensure that the network share is remounted as expected after a network reconnection. If you encounter any issues, check your system logs and fstab file for potential errors.
A CIFS network share is a file system protocol that allows systems to share files and resources, such as printers, over a network. It is commonly used in Windows networks but can also be used on Linux systems like Ubuntu.
Automatically remounting a CIFS network share can be useful in scenarios where your network connection is unstable or if you frequently switch between different networks. It ensures that the network share is always accessible without manual intervention.
There are two methods described in the article. The systemd method is simpler and requires less scripting knowledge, while the script method offers more flexibility and customization options. Choose the method that best suits your needs and technical expertise.
/etc/fstab file with a text editor (such as nano) using the command
sudo nano /etc/fstab. Add the options
noauto,x-systemd.automount,x-systemd.idle-timeout=30 to the fstab entry for the network share. Save the file and exit the text editor.
Create a script in the
/etc/network/if-up.d/ directory (for example,
/etc/network/if-up.d/mount-share) using a text editor. Add the necessary commands to the script, such as checking the network interface and running the
mount -a command. Make the script executable using the command
sudo chmod +x /etc/network/if-up.d/mount-share.
After setting up the automatic remounting, you can test it by disconnecting from the network and then reconnecting. Check if the network share is automatically remounted. Additionally, you can check the system logs and the fstab file for any potential errors or issues.
Yes, the script can be customized to suit your specific needs. You can modify the script to check for specific network conditions or add additional commands before or after the
mount -a command. Be careful when customizing the script and ensure that it performs the necessary actions correctly.
Yes, root or sudo access is required to modify system files such as
/etc/fstab and to make scripts executable. Ensure that you have the necessary permissions before attempting these steps.