I need to be able mount Samba (SMB) shares from a macOS terminal. How can I do that?
Macos – How to mount Samba / SMB shares under macOS from a terminal
macosshellsmb
Related Solutions
If you run the following:
sudo launchctl load -F /System/Library/LaunchDaemons/com.apple.smbd.plist
The service will launch, although the GUI won't update (as you force loading the service, even if disabled).
You can also remove the Disabled flag from the file and have it load regularly.
You can also run /usr/sbin/smbd
directly, as root.
I use autofs to mount my NFS shares on demand in my Kubuntu.
autofs
is a program for automatically mounting directories on an as-needed basis. Auto-mounts are mounted only as they are accessed, and are unmounted after a period of inactivity. Because of this, automounting NFS/Samba shares conserves bandwidth and offers better overall performance compared to static mounts viafstab
.
In your case this is useful:
When specifying a CIFS share in a map file, specify
-fstype=cifs
and precede the share location with a colon (:
).Example:
mntpoint -fstype=cifs ://example.com/shrname
Example: Mount read-write, specifying a user and group to own the files:
mntpoint -fstype=cifs,rw,uid=myuserid,gid=mygrpid ://example.com/shrname
Example: Mount read-write, specifying a username and password to use to connect to the share:
mntpoint -fstype=cifs,rw,username=myuser,password=mypass ://example.com/shrname
A map file is /etc/auto.master
or /etc/auto.*
or e.g. /etc/auto.master.d/*
. Read the documentation and learn how to configure the daemon.
At the first glance it looks like this is not the perfect solution for you because it seems you have to store the credentials in a file. This would be a map file (like in the example above) or a separate file e.g. credentials.txt
as you can see here. However there is this comment there:
I suppose you could make
credentials.txt
a named pipe and run a password prompting program to feed the pipe.
If this is right then I expect this "password prompting program" may be just a single echo
or printf
you invoke by hand.
EDIT:
What I'm asking for is an easy way to do this without having to manually invoke the command at a prompt, and without having to store (or fudge) the credentials in a file.
Linux can mount/unmount various filesystem via mount.*
and umount.*
executables. This credentials=/etc/credentials.txt
option in the linked example is in fact an option to mount.cifs
. I think if you use something like
mount -t foo …
or in autofs
configuration:
mntpoint -fstype=foo …
it will try to find and run mount.foo
, passing all other options to it.
So you should create mount.mycifs
as a wrapper over mount.cifs
. It should prompt you for your credentials somehow (straightforward xterm -e …
maybe, use read
or dialog
or something else; but read this please), add -o username=…,password=…
or -o credentials=…
to the rest of options and pass them to mount.cifs
(or mount -t cifs
) which does the actual mounting.
If you have umount.cifs
then make umount.mycifs
a symlink to it.
Then use -fstype=mycifs
in your map file without any options related to credentials.
The following /sbin/mount.mycifs
is a quick and dirty proof of concept. Understand what it does before you run it in your OS because it will be run as root
, I'm a random guy on the Internet and you shouldn't trust me.
#!/bin/bash
tmpf="`mktemp`"
DISPLAY=:0 XAUTHORITY=/home/ola/.Xauthority xterm -e /bin/bash -c '\
read -p "user:" u; \
read -sp "password:" p; \
printf "username=%s\npassword=%s" "$u" "$p" > "$0"; \
' "$tmpf"
mount -t cifs "$@" -o credentials="$tmpf"
rm "$tmpf"
It should be owned by root:root
or whatever is proper for mount.*
in your OS. Don't forget to make it executable (sudo chmod a+x /sbin/mount.mycifs
), it won't work otherwise. Notice there's a nasty hack with DISPLAY
and XAUTHORITY
that allows the autofs
daemon to display xterm
window on your(?) screen but in general it shouldn't do it. The hack is only for the daemon, mount -t mycifs …
should be able to display xterm
without the hack if invoked from within your desktop environment.
To make it less dirty you should write yet another program or script and run it with your local user's limited permissions before you access a directory where your CIFS would be automounted. This script should wait for a signal from mount.mycifs
, prompt you for the credentials (it can display windows etc. without nasty hacks) and pass them to mount.mycifs
which shouldn't display any windows nor prompts on its own.
Best Answer
Just like you would do in any common *nix system, you first have to create a directory which will serve as the mount point:
Then mount the SMB share:
If your server requires a username, password, and a domain:
Of course you can also leave out the password, and
mount_smbfs
will prompt you for it:Check out
man mount
for a list of common options to pass to the-o
switch. For storing authentication, seensmb.conf
.To unmount, just call: