2

In Windows 10, through Windows Explorer is possible stablish a network connection for a remote directory and thanks to the Map network drive approach is possible keep that connection mapped with a drive, so if three machines has the same directory name - lets consider documents - so is possible get:

M:\documents
N:\documents
W:\documents

In Mac - El Capitan - through:

  • Finder -> Go -> Connect to Server

Is possible do the connection with smb://hostname/documents

Therefore the documents remote directory is mounted on /Volumes

/Volumes
 documents

The problem is, what happen if other two machines have the same documents directory name? - Consider it how a mandatory policy of the company - How is mounted in /Volumes? How recognize each mounted remote directory within each machine in the LAN? I want rename the mounted directory through Finder, but is not possible.

I want have in MacOS in /Volumes the following structure:

/Volumes
 192.168.1.55
    documents
 192.168.1.56
    documents
 192.168.1.57
    documents

It to have the isolated paths available to be used on some bash-shell scripts. How accomplish this goal?

3
  • 1
    You could have just mounted two of them and looked at what the OS created in /Volumes, you know. It suffixes a number. Feb 16, 2022 at 15:23
  • 2
    @MarcWilson However that depends on the order you attach them which might not be the same each time - asking to be able to add something that can be controlled in the path is a good question (although asking it twice is not good)
    – mmmmmm
    Feb 16, 2022 at 15:34
  • Yes. I don't want depend about XXX and XXX-1 - because there is no garanty about the sort/order and later can happen a disconnect and reconnection - so the automatic paths are useless - I want take explicit control about the names. The other question is more specific about command. Here is open to either Finder or Command. Or perhaps rename a mounted directory. The other is through the connection itself. Feb 16, 2022 at 16:35

2 Answers 2

3

Below is an example script. This script is modeled after the actions take by the Finder application. To simplify this answer, little error checking takes place.

#!/bin/bash
sudo mkdir -p "/Volumes/${1#*@}"
sudo chown $USER:staff "/Volumes/${1#*@}"
mount -t smbfs -o nodev,nosuid  "$1" "/Volumes/${1#*@}"
mount | grep -q "/Volumes/${1#*@}" && open "/Volumes/${1#*@}"

Note: Since you require folders to be created inside the /Volumes folder, the user's password may be requested by the script.

If the script is saved to the file connect, then the synopsis would be the following.

connect //user[:password]@server/share

An example is given below.

./connect //[email protected]/documents

In this example, the mount point would be the following.

/Volumes/192.168.1.55/documents

To unmount, the synopsis would be the following.

diskutil unmount /Volumes/server/share

An example of the command to unmount is given below.

diskutil unmount /Volumes/192.168.1.55/documents

Comment

While the above answer is designed to meet the requirements stated in the question, the answer is not very robust. If two or more users attempt to access the same share, then using the above answer will result in failure. Basically, the problem is more that one user can not use the same mount point at the same time. One way to avoid this problem would be to place the mount point in the $HOME/Volumes folder instead of the /Volumes folder. The new script given below is a modified version of the original script given above. This new script places the mount point in the $HOME/Volumes folder.

#!/bin/bash
mkdir -p "$HOME/Volumes/${1#*@}"
chmod 700 "$HOME/Volumes"
chmod +a "group:everyone deny delete" "$HOME/Volumes"
mount -t smbfs -o nodev,nosuid "$1" "$HOME/Volumes/${1#*@}"
mount | grep -q "$HOME/Volumes/${1#*@}" && open "$HOME/Volumes/${1#*@}"

Note: Since /Volumes folder is not used, the user's password will not be requested by the script.

If the script is saved to the file connect, then the synopsis would be the following.

connect //user[:password]@server/share

An example is given below.

./connect //[email protected]/documents

In this example, the mount point would be the following.

"$HOME/Volumes/192.168.1.55/documents"

To unmount, the synopsis would be the following.

diskutil unmount "$HOME/Volumes/server/share"

An example of the command to unmount is given below.

diskutil unmount "$HOME/Volumes/192.168.1.55/documents"
7
  • Does -pm work together? it is not correct, it only affects to the latest directory. In this case through $3 Feb 16, 2022 at 20:32
  • Well, -pm700 is short for -p -m 700. If this is a problem for you, then I will just change my answer. I do intend for the command to only change the the latest (last) directory. I am not sure the -m 700 is even needed. I only added this because apparently this is what the Finder and diskutil mount do. Feb 16, 2022 at 20:45
  • About the pm It does not work as expected - pls see this post and the two answers and the comments of them - stackoverflow.com/questions/5786326/… - So I replaced sudo mkdir -p -m 700 "/Volumes/$2/$3" by sudo mkdir -p "/Volumes/$2/$3" and sudo chmod -R 700 "/Volumes/$2/$3" Feb 16, 2022 at 20:49
  • After further investigation, I determined the default file modes should be use. Therefore, the chmod command and the -m option for the mkdir command are not needed. I updated my answer accordingly. This would also make the above comments irrelevant with respect to this answer. Feb 17, 2022 at 9:13
  • 1
    I was reading the man page for mount_smbfs, which states the following: "Note: You should always use the system mount command and never call mount_smbfs directly." Conversely, I was also reading the man page for umount, which states the following: "NOTES Due to the complex and interwoven nature of Mac OS X, umount may fail often. It is recommended that diskutil(1) (as in, ``diskutil unmount /mnt'') be used instead." Feb 17, 2022 at 13:52
1

The mount command lets you specify a mount_point:

sudo mount -t smbfs smb://hostname/documents /Volumes/documents-foo

Just change documents-foo to whatever you want it to be called, and note that that directory must already exist (create it with sudo mkdir /Volumes/documents-foo if it doesn't)

5
  • I tested this answer using El Capitan and the answer failed to work. What operating system did you use to test. Or, did you test? Feb 16, 2022 at 20:12
  • It does not work - it arises mount_nfs: can't resolve host: smb and mount_nfs: no usable addresses for host: smb Feb 16, 2022 at 20:31
  • 2
    I believe mount should have been mount -t smbfs or just mount_smbfs , but that is probably not the only problem. Feb 16, 2022 at 20:33
  • 1
    The sudo mount -t smbfs smb://hostname/documents /Volumes/documents-foo command mounts, but the user can not access the share without using sudo. By removing the sudo and entering the mount -t smbfs smb://hostname/documents /Volumes/documents-foo command, the share mounts, but also allows user access without using sudo. Feb 17, 2022 at 9:58
  • 1
    You're right, I left out -t smbfs. I meant to edit out the username and password from the command I tested with, and removed too much. I'll edit that in.
    – Cody
    Feb 18, 2022 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .