MacOS – How to escape password for smb mount

macosmountpasswordsmb

I want to mount a SMB share with different user credentials. Therefore I want to use

mount -o nodev,nosuid -t smbfs //user:pass=<word@host/share /mnt/share

However, this command fails:

-bash: word@host/share: No such file or directory

When trying to pass the password via a variable, I get:

$ export PWD="pass=<word"
$ mount -o nodev,nosuid -t smbfs //user:$PWD@host/share /mnt/share
mount_smbfs: URL parsing failed, please correct the URL and try again: Invalid argument

mount on other systems seems to accept username and password as mount options, like:

$ mount -o nodev,nosuid,domain=mydomain,username=user,password="pass=<word" -t smbfs //host/share /mnt/share 

or via a credentials file:

$ cat credentials.txt
username=user
password=pass=<word
domain=mydomain
$ mount -o nodev,nosuid,credentials=./credentials.txt -t smbfs //host/share /mnt/share 

However, both ways are not available with macOS' mount:

mount_smbfs: -o credentials: option not supported
mount_smbfs: -o domain: option not supported
mount_smbfs: -o username: option not supported
mount_smbfs: -o password: option not supported

So the question is: How to provide the password to macOS' mount_smbfs? I'm aware that changing the password would solve the problem, but for reasons this is not an option.

Best Answer

Slightly offtopic, but maybe useful if you want to stick to putting credentials with special chars in a Shell variable. Regarding your bash commands I will point you to several topics:

  1. PWD is a always present ENV variable showing the current working directory (Print Working Directory). Omit using that name for a PassWorD variable :-).
  2. There is a significant difference in using double quotes and single quotes. Double quotes embrace a string, which also can contain expandable variable names and is always subject to escape problems. Single quotes define a fix string and will be treated as such, e.g. no variable expansion will be done.

Example:

# fix string
tatooine-2257:~ mallert$ export P='<word'
tatooine-2257:~ mallert$ echo $P
<word
# no variable expansion
tatooine-2257:~ mallert$ export P='<word $PWD'
tatooine-2257:~ mallert$ echo $P
<word $PWD
tatooine-2257:tmp mallert$ echo $PWD
/tmp
  1. Finally - treat variable names in expansion distinguishable from usual text like referring to ${variablename}. (Bourne Shell style)
  2. Try open 'smb://username:password@server/share' - it creates the volume mount for you. If you omit the password, Finder will ask you for it.