156

I would like to mount an SMB network share from the command line (terminal); how would I go about that?

I am using Mac OS X 10.6.4.

Eyvind
  • 1,725
  • 2
    I posted an answer here for a similar question, with an overview of the different methods to do that and their advantages and disadvantages. – mivk Aug 10 '20 at 17:45

9 Answers9

128

Use the open(1) command and a URL:

open 'smb://username:password@server/share'

Pros: Creates the mount point in /Volumes for you.

Cons: Requires the Finder to be running.

Jawa
  • 2,484
tlindner
  • 1,397
  • 4
    i prefer this version for quick connections in my local network, there all users are known, so this is a simpler command then using mount_smbfs - where i actually had problems concerning non existent paths - whereas if i would need to connect to some new server with special filesystem, or i would need to try another user or wanted different mount point - ok - but how often does this happen... so my thumb goes up for this simpler solution! Well Done! Ok i am a Terminal freak who still has Finder running, but that is like 90% of us, right? – hexerei software Sep 12 '15 at 14:00
  • Isn't Finder always opened, at least as a process? – Iulian Onofrei Feb 01 '16 at 08:40
  • 2
    @Iulian Onofrei: Not unless the user is logged in interactively. I've needed to use the direct mount command in cron jobs. Using the open command, these would fail if the interactive session was gone, e.g., after a power failure. – George Feb 09 '16 at 22:21
  • 1
    Also, note that open always exits as if it were successful if it passed the command along to Finder, even if the command later failed to connect to the network drive. – Jonathan Wren Jul 17 '16 at 02:15
  • Is it possible to have control the name of the mount point directory? E.g. /Volumes/mypc/Users instead of just /Volumes/Users. If I click on C drive, then it will actually mount to /Volumes/mypc...I wonder if this is just hard-coded for C drive? – vaughan Mar 14 '21 at 18:37
  • 1
    Note that if you're uncomfortable with typing your password in plaintext in a cli command you can omit the password and you'll be prompted for it by Finder – Ogier Schelvis Aug 31 '21 at 10:59
  • Both this way, and the normal Finder way (Go/Connect To Server...) will create the mount in /Volumes. In a multi-user environment where users don't log out, this causes mountpoints to have names like SHARE, SHARE-1, SHARE-2. So I prefer to mount in the users home directory (but this also has a disadvantage, you have to create a FOLDER for the share first, nothing is perfect.) – Paul Verschoor Oct 17 '22 at 10:45
124

You could easily achieve this using mount_smbfs (which is, actually, a wrapper for mount -t smbfs) :

mount_smbfs //user@SERVER/folder ./mntpoint

Optionally, add the workgroup :

mount_smbfs -W workgroup //user@SERVER/folder ./mntpoint

You could, of course, change the ./mntpoint (for something like /Volumes/smb).

After doing this, simply go to ./mntpoint to browse your data.

To unmount, using the following command :

umount ./mntpoint
jherran
  • 13,284
Studer
  • 8,779
  • 5
  • 38
  • 38
  • 2
    I get : `mount_smbfs: mount error: path: Input/output error', while it mounts find via Finder->Connect to Server? – Ian Vaughan Mar 02 '12 at 16:53
  • How do I go about it if my username has a @ char in it? For example when i am using a microsoft account like xyz@microsoft.com ? I tried giving the username in single and double quotes but dint work.. – Yashvit Sep 13 '14 at 14:00
  • I get this error: mount_smbfs: illegal option -- W – Iulian Onofrei Nov 04 '15 at 16:32
  • 9
    I get this error: mount_smbfs: could not find mount point /Volumes/Share: No such file or directory – Iulian Onofrei Feb 01 '16 at 08:35
  • 29
    Quote from man mount on OS X Yosemite: Note: You should always use the system mount command and never call mount_smbfs directly. – ssc Jun 30 '16 at 12:36
  • 5
    @Yashvit: Change the '@' to '%40' – WGroleau Aug 01 '16 at 04:11
  • Ran into some trouble when not doing this with sudo. So I suggest to: sudo mount_smbfs //some-srv/some/folder mntpoint && sudo cp $1 "mntpoint/some/further/folders/" && sudo umount mntpoint – t3az0r Aug 02 '19 at 12:45
  • there is no -W option – IceFire Mar 06 '20 at 09:36
  • 1
    @lulian Onofrei That's because you need to create a directory for that mount point first. E.g. mkdir /Volumes/Share – Joakim L. Christiansen May 22 '20 at 16:44
  • It is possible to pass a password: sudo mount -t smbfs //$user:$pass@server/share /mntpoint where pass=$( <"/opt/.pass.txt" ). If your password has special symbols, just point to this solution (and URL encoded table) – Rapekas May 31 '21 at 14:46
  • This only worker for me when SERVER was an IP address. – postrational Dec 02 '21 at 22:37
  • is there a way to specify the SMB version used? in linux you use -o vers=x.x to specify it. often if you use the wrong version of smb, it won't connect. – majorgear Apr 10 '22 at 16:42
34

You should take a look at mount’s help:

man mount

Upon closer inspection you’ll see that the filesystem’s type is:

mount -t smbfs //username:password@MACHINENAME/SHARENAME /SomeLocalFolderOfChoice

Password (and theoretically username) are optional.

The result of the above command will be no output (if all went ok), but a cd /SomeLocalFolderOfChoice, should produce the remote results. Please note that SomeLocalFolderofChoice must exist.

You can also use mount_smbfs to replace the mount -t smbfs.

jherran
  • 13,284
31

Using AppleScript is convenient because it stores your passwords in the Keychain. Bash function:

function mymount
{
    osascript <<EOF
mount volume "smb://user@fqdn1/volume1"
mount volume "smb://user@fqdn2/volume2"
EOF
}

Invoke ‘mymount’ from bash, enter passwords via the standard Keychain popup, and if all goes well the requested volumes will be mounted in /Volumes.

grg
  • 201,078
wytten
  • 421
  • 7
    That's perfect approach too. I don't like approved answer because it mounts as root user. In my opinion, these one-liners should be the best options: /usr/bin/osascript -e 'mount volume "smb://username:password@server/share"' or open /Volumes; open 'smb://username:password@server/share' – laimison May 08 '19 at 13:04
  • 1
    Additional improvement in case you want to use the current logged-in user name: replace "smb://user@fqdn1/volume1" with ""smb://$(id -un)@fqdn1/volume1". The bash command id -un resolves to the logged in user. – Rabarberski Sep 16 '20 at 11:11
  • 3
    Thank you for this answer. In addition to doing the "regular thing" that macOS does — i.e. putting it in /Volumes, making the directory for me, making it mount as the appropriate user, using the correct keychain entry automatically — it also seems to block until the drive is actually mounted, which is exactly what I want when I'm running scripts. – Glyph Feb 07 '22 at 05:05
  • Thanks! I made my script based on this. – Afriza N. Arief Sep 28 '22 at 04:00
  • "mount: You must specify a filesystem type with -t" – John Smith Oct 26 '23 at 17:25
8

What worked for me to make them mount during boot:

==> /etc/auto_master <==
#
# Automounter master map
#
+auto_master        # Use directory service
/net            -hosts      -nobrowse,hidefromfinder,nosuid
/home           auto_home   -nobrowse,hidefromfinder
/Network/Servers    -fstab
/-          -static
/-          auto_smb          # add this line <**********

==> /etc/auto_smb <==         # Create this if it doesn't exist <****
/(not Volumes)/Public   -fstype=smbfs,soft smb://(user):(password)@192.168.140.5/data/Public
/(not Volumes)/ WGroleau -fstype=smbfs,soft smb://(user):(password)@192.168.140.5/data/Students/wes_groleau

For some reason, neither the short name nor the FQDN of the server worked, so I used 'ping (name) to get the IP. In other words, DNS would resolve the name, but mount_smbfs could not.

And I could not put the mount point in /Volumes, because boot up would delete it.

One quirk: After this worked fine for a few days, for two or three days, LibreOffice, Adobe Reader, and Finder could not find ONE of the two shares, but the shell and TextEdit had no problem. After two or three days of that, it mysteriously started working again.

If you do these edits and don't want to reboot, you can mount them with 'auto mount -vc'

Update: More quirks. (1) There are two WiFi systems here, and one of them has no access to the Windows servers. A couple of times a week, one or both of the routers goes down. If the "good one" goes down and the MacBook automatically connects to the other one, instead of telling me the drive is off-line, the SMB drivers say "Too many users." (2) A couple of times a week, I get "permission denied" when I try to access my Windows files. This typically lasts about a half-hour, during which I can go to a windows bar and log in and see files with the same ID and password.

WGroleau
  • 4,869
  • 7
  • 44
  • 77
  • 1
    Since this has been upvoted twice this week, I should point out that it was written several versions of MacOS ago. – WGroleau Mar 30 '19 at 09:12
7

I would add that if you have a username of the form "workgroup\username", you should mount it like this :

mount -t smbfs "//WORKGROUP;username:password@MACHINENAME/SHARENAME" /SomeLocalFolderOfChoice

Source : adapting an example from here

matthieu
  • 171
  • 1
  • 3
3

I found the following commands work perfectly (meaning, just the same way as the CMD+K in Finder):

osascript <<EOF
mount volume "smb://user@server/share"
EOF

@wytten mentions the same above.

or

osascript -e 'mount volume "smb://user@server/share"'

Reference:

MaduKan
  • 261
-3

old post but what about mounting like this:

smb://no_username:password@MACHINENAME 

like the time machine drives?

  • 2
    "from the command line (terminal)"... this is not a valid bash command. I assume you mean with Finder + ⌘K to connect to server which isn't what the OP asked. – mattmc3 May 15 '19 at 16:30
-3

Important to allow port 445 (TCP) to smb communication. If you don't access it, your firewall block it!

You can connect to samba use with the following commands in mac os: mount_smbfs or mount or use GUI see: open Finder click Go and select the Connect to Server menu item, type smb://yourdomain/sharedfoldername and press connect button.

  • This is not an answer to the question. – Tetsujin Jan 27 '20 at 10:42
  • yes, all right it is an answer to a problem when somebody cant access it, and it is important. The command line and the GUI method will not work without it. – István Döbrentei Jan 27 '20 at 11:02
  • As helpful as this is, this should be a comment only. Although it is an important part of making sure SMB shares aren't blocked, it is not an answer to the original question! – IconDaemon Jan 27 '20 at 13:16
  • I added it because the letters in the comment is small but my opinion is that it is an important setting. I would like to help others to see better. Sorry, I can't see well the small letters on the screen. "Thank you" for your downvote, I hope your eyes work better. – István Döbrentei Jan 27 '20 at 20:26
  • @IstvánDöbrentei That's StackOverflow. Either go by their rules, or get downvoted. It's harsh, but it's a way to keep the content on SO sane. For more meandering discussion I suggest reddit. It's way more informal and you find a subreddit for nearly every topic. Or forums dedicated to topic. I for me personally appreciate your additional information, as it's very likely somebody not knowing how to mount will lack the knowledge about firewalls as well... – andreas Dec 16 '20 at 11:10