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.

  • You're passing a redirection operator in the middle of the URI, and aren't escaping it and aren't quoting the URI. Thus you're instructing the shell to run a binary named 'share' from a directory named 'word@host' with '/mnt/share' as an argument and pass that into a butchered mount invocation. – Marc Wilson Jul 8 at 3:49
  • @MarcWilson Exactly that is the problem. Hence the question how to escape it. – PVitt Jul 8 at 10:18
  • Can we assume your shell is bash and the version is GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18) – bmike Jul 9 at 13:38
  • @bmike Yes, that is correct, although it is darwin17, not darwin18. – PVitt Jul 10 at 6:48

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.
  • Option 4 is the pragmatic way to get this working without relying on the mature and secure manner of purring the required password into the keychain and retrieving it as needed. That last item will have a lot of learning (shaving the yak indeed), but will be very powerful once anyone has a set of scripts and tools they understand to write to the keychain from bash and then read from it from bash. – bmike Jul 10 at 10:58
  • But option 4 wont work as the share should be mounted during boot time and the password is not to known by the user. However, storing it in the keychain would be the preferred way. – PVitt Jul 10 at 12:37
  • However, thanks for the hints. In the heat of the battle I used PWD without thinking of it's common usage. Also the the other stuff is quite interesting. Didn't know about it yet. – PVitt Jul 10 at 12:42
  • You are welcome - always glad to help out. :-) – Garex Jul 10 at 14:08

My gut feeling is you don't want to complicate things by setting a variable and you want to escape each punctuation with the \ character.

The < is going to be super problematic for bash

The next thing you might try is quoting the problematic argument string with single quotes but I've had better luck with the back slash escaping command characters in bash individually.

So if you want bash to see this:

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

you need to type this:

mount -o nodev,nosuid -t smbfs //user:pass=\<word@host/share /mnt/share
  • Is there a command to auto escape strings? – JBis Jul 9 at 13:57
  • @JBis Not sure I get what auto escape would be? The < character is defined as a high precedence operator in bash so it redirects the command mount -o nodev,nosuid -t smbfs //user:pass= to a file named word@host/share with some other stuff tacked on. Are you asking if there's a command to point out any and all characters that are operators in bash? – bmike Jul 9 at 14:07
  • Well putting a "\" in front of < should make it read as a character not an operator. – JBis Jul 9 at 14:11
  • Thanks for the answer. I'm also interested in the complicated way with variables, not only, but also because \< only helps to avoid the bash error. Now it is mount_smbfs: URL parsing failed, please correct the URL and try again: Invalid argument, which is the same error as when passing the password via a variable. – PVitt Jul 10 at 7:07
  • Hi @PVitt You might have better luck posting an entirely new question showing the one command that has the new error. It might be hard to edit the current question and repeat getting new answers or editing the existing answers if you've moved past the initial set of errors with a \ escaping the one < – bmike Jul 10 at 10:55
up vote 0 down vote accepted

Even when escaping the bash relevant characters (< in the example) via \ or passing the password via a variable like

$ export PASSWORD="pass=<word"
$ mount -o nodev,nosuid -t smbfs //user:${PASSWORD}@host/share /mnt/share

mount fails to parse the url due to the fact that < is also a special character to URLs:

mount_smbfs: URL parsing failed, please correct the URL and try again: Invalid argument

So we also need to escape the < character for URLs, which is %3C, so that pass=\<word becomes pass=%3Cword.

See Stackoverflow for additional information, and a list of url encoded characters.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.