Mount remote file shares on a wireless network.

Working with my Raspberry Pi, one issue I always had was mounting my remote Samba server after the wireless was up. There are a couple ways this can be completed, but I find a simple script running after the wifi is up to be the easiest. This works for both Redhat and Debian based Linux distros. Here are the steps I used.

1. Determine the SSID

dave@dai-pi-2:~$ iwconfig | grep ESSID
lo no wireless extensions.

eth0 no wireless extensions.

wlan0 IEEE 802.11bgn ESSID:"Daves-Network"

So my SSID is “Daves-Network”, with the interface of “wlan0”.

2. Add the following mount script under /etc/network/if-up.d/050mountsmb. In this case, I used 050mountsmb, with the header number being the order in which the script is run compared to other scripts in this directory.

#!/bin/bash
if [ "$IFACE" = "wlan0" ]
then
    if iwconfig|grep -c "Daves-Network"
    then
        sleep 10s
        sudo mount -t cifs //192.168.1.100/smb_mount /mnt/backup -o username=dave,password="MySuperSecrectPassword",uid=1000,gid=1000
    fi
fi

Breaking down the mount command:

  • We confirm the interface is “wlan0”
  • If we can then confirm the SSID “Daves-Network”, we then perform the mount.
  • 192.168.1.100 is the Samba share IP
  • smb_mount will be the Samba share’s name.
  • /mnt/backup will be where the file share will be mounted.
  • The options will identify the user/password in order to access the share, and the user/group permissions for the share on the local device.
  • If the mount fails, try again in 10 seconds.

 

3. Secure the script

Since this script uses a password, you need to make sure only root has read/write access.

sudo chown root:root /etc/network/if-up.d/050mountsmb
sudo chmod 600 /etc/network/if-up.d/050mountsmb

That’s it! Now when the host is rebooted or the network reloaded, the share will be mounted after wifi is confirmed as up. If not, the script will sleep for 10 seconds and try again.

Leave a Reply

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

*