10

I currently connect to a Windows (smb) share via the Finder -> Go -> Connect To Server. I have to type in the IP (although I have saved the credentials in my keychain). If I reboot or logout, I have to do this again.

I would like the SMB share I am connecting to always be available as soon as I login. How can I accomplish this?

Chealion
  • 26,327

7 Answers7

12

Make the share point a login item, and it'll be automatically "opened" (i.e. connected) every time you log on. Go to System Preferences -> Users & Groups -> select your account in the sidebar -> Login Items tab, then drag the mounted share point (you can get it from the Finder's computer view, available under its Go menu).

(Historical note: the preference pane was named "Accounts" in OS X 10.6, but changed to "Users & Groups" in 10.7 and later.)

4

Overview:

Create a launchd agent (a bash script in this case) to mount us whatever shares we like every couple of minutes. It is not perfect but it works. This is for Samba shares, but you can modify it to do other types.

Made with help from Dave Nicoll's about sharing windows/x iTunes libraries.

I'm using my laptop's wireless card to determine if I am connected to my home SSID. You can of course use anything you'd like for conditional execution.

If you're going to use SSID as a condition for mounting as I have you may find it benificial to alias the Airport utility that ships with OS X to your /usr/bin/. If not skip ahead to the next section.

Open Terminal and run:

sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport
Remember the output of this:
airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'

The Script:

My conditionals check for

  1. The share is not already mounted (line 1-4)
  2. The SSID of our Airport card network is not null (line 5-8)
  3. That our SSID must be equal to a pre-defined SSID, in my case "2600leagues" (line 9)

Open your favorite text editor and start a new file, I called mine MountShares.sh

if [ -d '/Users/kyle/Music/iTunes/Podcasts/' ]; then
        #echo Nothing to do, share is mounted
        exit
else 
    if [ -z `airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'` ]; then
       #echo SSID is Null, we're not connected with the Airport to any Network.
        exit
    else 
        if [ `airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'` == "2600leagues" ]; then
            #echo SSID is 2600leagues!
            /sbin/mount -o nodev,nosuid -t smbfs '//Kyle:NotmyPassword@192.168.1.5/media/Music' '/Users/kyle/Music/'
        fi
    fi
fi

Next we need to edit line 11 to represent your specific needs. You only need to edit the bold parts:

/sbin/mount -o nodev,nosuid -t smbfs '//User:Password@ServerIP_or_Name/Share' '/Where/You/Want/it_to_mount/'

Example:

/sbin/mount -o nodev,nosuid -t smbfs '//Kyle:NotmyPassword@192.168.1.5/media/Music' '/Users/kyle/Music/'

Now that we have our edited mount command, try running it in Terminal. If it is successful, your share should be available at the mount location. (in my example /Users/kyle/Music/)

Edit the line 1 of the bash script to reflect a file or directory inside the mounted share. If you are going to use the SSID as a condition as I have, you'll want to change line 5 by replacing 2600leagues with your SSID.

Save the bash script in a convenient location. (I use ~/Library/Scripts/NetworkMounts/MountShares.sh)

You now need to make the bash script executable, we use chmod to do this.

In Terminal:

chmod 777 ~/Library/Scripts/NetworkMounts/MountShares.sh

If the network share is still mounted, make sure you unmount/eject it now. (Finder works)

Try executing the script via Terminal:

~/Library/Scripts/NetworkMounts/MountShares.sh

If all goes well your share should be mounted. Now all that is left to do is make it so the script runs every so often. Normally you would use crontab for this sort of thing, but 10.6 has deprecated it. Apple would rather you use their launchd service.

Making the launchd Agent

To help you make a launchd agent get Lingon from sourceforge.

  1. Open Lingon
  2. Create a new User Agent (My Agent)
  3. Give it a name such as com.kyle.MountShares
  4. Choose or type the path to the bash script ~/Library/Scripts/NetworkMounts/MountShares.sh
  5. Specify when you would like it to run. (I have Run when it is loaded by the system and *Run it every 10 minutes)
  6. Save it
  7. Exit Lingon

That's it

Let me know if this helps; I typed all this out as fast as possible.

k-f-
  • 136
  • 3
3

With auto_fs, auto_master, etc, try having a read of Autofs: Automatically Mounting Network File Shares in Mac OS X (PDF). It's a little dated now (2009) but, using the examples in the doc, I got all my NFS & SMB shares organised and auto mounting easily.

Mark
  • 31
1

Have you looked at auto_master(5)? At a glance it looks possible, but it looks like it could be a lot of work to get set up.

Spiff
  • 110,156
1

There are a couple of solutions here, the simplest of which (IMHO) I will repost below:

From a terminal:

sudo vifs

If not familiar with vi: Hit 'i' for insert (edit mode), then paste this on a new line:

smbserver:/share /Volumes/mount_point url automounted,soft,url==cifs://user:pass@smbserver/share

Hit Esc and then zz to save and exit.

Finally:

sudo automount -vc

Now, when hitting /Volumes/mount_point in the console/finder/whatnot, the share will be automatically mapped.

0

System Preferences > Sharing > File Sharing > Option > Share Files and Folders Using SMB . I think it can recognize sharing settings. If not, you can add smb share to your favorite server too.

If you want to add it as a favorite server, hit Apple+K while in Finder to bring up the Connect to Server dialog box. Type in the address and then click the + next to the address. It will then be added to the below area Favorite Servers.

Josh K
  • 12,990
Ye Lin Aung
  • 5,700
0

My solution is to use Automator and create a small app that automatically connects, then set it to start at login.

Start Automator and select Application, and under the Files and Folders Library you should see Get Specified Server and Connect to Servers. Add those in that order, and add your ip address and share name (if applicable).

At this point you could save it as an application (~/Applications is a good place). Then add it to your login items through Systems Preferences > Users.

However, I added in Automator a 30 second Pause (from Utilities library), and four Loops of 1000 iterations. That should keep it running until I logout or reboot.

Note: it will pop-up a somewhat useful dialog if it cannot connect:

Connection failed

The server "x.x.x.x" may not exist or it is unavailable at this time. Check the server name or IP address, check your network connection, and then try again.

Note: it does not check to see if it is already connected. There may be a way to check the output of Connect to Server

Grue
  • 21