How to mount a Samba directory through Terminal, in the same way as CMD+K in Finder?

Hi,


I have a problem when mounting a directory

Apple recommend Samba for mounting a share from a NAS.


I want to mount the directory using terminal. A shell script.

So that I can use rsync to backup my work directories

selectively with least resource consumption.


The problem is that, If I do: "open 'smb://user@server/myshare'" in the terminal,

the mounts end up having "smbfs, nodev, nosuid, quarantine" attributes.

However if I use CMD+K to mount, the mount has only have

"smbfs, nodev, nosuid" attributes, and that way there is no "quarantine" attribute.


This happens with recent updates, perhaps since BigSur? Not sure to confirm though.

I need help today to find a way to mount the share in the same way as Finder's CMD+K, but through the terminal.

Posted on Aug 26, 2023 5:26 AM

Reply
Question marked as Best reply

Posted on Aug 26, 2023 5:36 AM

Historically, I've had better success using AppleScript to automate mounting of AFP and SMB shares. For example:


mount volume "smb://server/ShareName"


Or, from Terminal, you can use:


osascript -e 'mount volume "smb://server/ShareName"'


You can enhance the command with user@server syntax. Or you should be able to save the credentials in Keychain (assuming you require authentication to connect to the NAS).


However, when talking rsync and scripted automation, I generally will use SSH, not AFP or SMB. If your NAS supports SSH you might find this easier to automate. Using scripts to mount the volume will require the host system to be logged in while the use of SSH will still work if the rsync initiator is at the login window. Additionally, the Finder can get wacky every so often and generate duplicate mounts. In the Finder everything looks fine but in the Volumes directory you get the ghost mount and then indexed mounts (Mount 1, Mount 2, etc).


Hope this is helpful.


Reid





Similar questions

7 replies
Question marked as Best reply

Aug 26, 2023 5:36 AM in response to madu007

Historically, I've had better success using AppleScript to automate mounting of AFP and SMB shares. For example:


mount volume "smb://server/ShareName"


Or, from Terminal, you can use:


osascript -e 'mount volume "smb://server/ShareName"'


You can enhance the command with user@server syntax. Or you should be able to save the credentials in Keychain (assuming you require authentication to connect to the NAS).


However, when talking rsync and scripted automation, I generally will use SSH, not AFP or SMB. If your NAS supports SSH you might find this easier to automate. Using scripts to mount the volume will require the host system to be logged in while the use of SSH will still work if the rsync initiator is at the login window. Additionally, the Finder can get wacky every so often and generate duplicate mounts. In the Finder everything looks fine but in the Volumes directory you get the ghost mount and then indexed mounts (Mount 1, Mount 2, etc).


Hope this is helpful.


Reid





Aug 26, 2023 6:07 AM in response to Strontium90

Hi Reid, thank you for your response. When trying mount volume command, I get an error "invalid file system".


Here's what I did (obviously the paths/server are changed to accurate ones when running):



mount volume "smb://user@server/share"

mount: smb://user@server/share: invalid file system.


Any help is much appreciated to get this matter sorted.

Aug 26, 2023 7:50 PM in response to madu007

You could also use


open smb://user@server.address/share


from a Terminal session, or put that into a shell script.


The 'open' command is very useful. You can start a Screen Sharing (or VNC) session using:


open vnc://user@vnc.server.address


If the VNC server does not use the default 5900 port number you could specify it as:


open vnc://user@vnc.server.address:5905


assuming the VNC server is using port 5905. adjust accordingly.


You can open a web page in a default web browser


open https://web.page.address/any/additional/suffixes


You can start an ssh session with:


open ssh://remote.system.address


You can tell the Finder to open a folder


open /path/to/the/folder


You an open a file in the file's default application


open /path/to/the/file


You can open a file in a specific application


open -a application /path/to/the/file


The 'open' command can do so much more. See:


man open

How to mount a Samba directory through Terminal, in the same way as CMD+K in Finder?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.