keepcalculator

06 Numbers and Powers

Access and Mount an SMB Share via Command Line

Card No. 06·72936 · Updated · Standard formula, shown below

To access and mount an SMB share via the command line, you use a client tool such as smbclient for quick interaction or mount.cifs (Linux) / mount_smbfs (macOS) to attach the share to your local filesystem. The exact command depends on your operating system and whether you need a temporary session or a persistent mount.

What Is SMB and Why Use the Command Line?

SMB (Server Message Block) is a network protocol for sharing files, printers, and other resources between computers. It is the native file-sharing protocol on Windows and is widely supported on Linux, macOS, and network-attached storage (NAS) devices. While graphical file managers can connect to SMB shares with a few clicks, using the command line offers several advantages:

  • Scripting and automation: Commands can be embedded in shell scripts for repeated or scheduled tasks.
  • Remote administration: When managing a server over SSH, you often have no graphical interface.
  • Precise control: You can specify exact mount options, credentials, and permissions.
  • Debugging: Command-line tools provide verbose output that helps diagnose connection issues.

Prerequisites: Tools and Permissions

Before you can mount an SMB share, you need the appropriate client software installed. On most Linux distributions, the package is called cifs-utils (which provides mount.cifs) and smbclient (part of the samba-client package). On macOS, the built-in mount_smbfs is available, and on Windows, you can use net use in a command prompt. You also need the following:

  • The server’s hostname or IP address (e.g., 192.168.1.10 or fileserver).
  • The share name (e.g., public or documents).
  • A valid username and password (or a guest account if the share allows it).
  • Local permissions to create a mount point (usually requires sudo on Linux).

Quick Access with smbclient (No Mounting)

If you only need to browse or transfer a few files without permanently attaching the share, smbclient is the fastest method. It works like an FTP client. To connect to a share, use:

smbclient //server/share -U username

You will be prompted for the password. Once connected, you can use commands like ls, cd, get (to download a file), and put (to upload a file). For example, to download a file named report.pdf:

smbclient //192.168.1.10/documents -U alice
smb: > get report.pdf

To list all shares on a server without mounting any, use:

smbclient -L //server -U username

This is useful for discovering what shares are available before you decide which one to mount.

Mounting an SMB Share on Linux

To make the share appear as a directory in your local filesystem, you use the mount.cifs command (often called via mount -t cifs). The basic syntax is:

sudo mount -t cifs //server/share /mnt/mountpoint -o username=alice

You will be prompted for the password. After mounting, you can access the files under /mnt/mountpoint as if they were local. To unmount, use sudo umount /mnt/mountpoint.

If you want to avoid the password prompt (for scripting), you can include it in the options, though this is less secure:

sudo mount -t cifs //server/share /mnt/mountpoint -o username=alice,password=secret

For a share that allows guest access, use guest instead of a username:

sudo mount -t cifs //server/share /mnt/mountpoint -o guest

Common Mount Options

  • uid=1000 and gid=1000 – Set the local user and group ownership of the mounted files (replace 1000 with your user ID).
  • file_mode=0644 and dir_mode=0755 – Set file and directory permissions.
  • iocharset=utf8 – Ensures proper character encoding for non-ASCII filenames.
  • vers=3.0 – Specify the SMB protocol version (e.g., 1.0, 2.0, 3.0) if the server requires a specific version.
  • _netdev – Tells the system to wait for network availability before mounting (useful in /etc/fstab).

Mounting on macOS and Windows

macOS uses mount_smbfs, which is similar but with slightly different syntax. The command is:

mount_smbfs //username@server/share /mount/point

You will be prompted for the password. To unmount, use umount /mount/point. On Windows, you can map a network drive using net use in a command prompt:

net use Z: \\server\share /user:username

This maps the share to drive letter Z:. To disconnect, use net use Z: /delete. Windows also supports pushd \\server\share to temporarily map a drive and change to it.

Persistent Mounts via /etc/fstab

If you want the share to be mounted automatically at boot, add an entry to /etc/fstab on Linux. A typical line looks like:

//server/share /mnt/mountpoint cifs username=alice,password=secret,uid=1000,gid=1000,iocharset=utf8,_netdev 0 0

Storing passwords in /etc/fstab is a security risk because the file is readable by root only, but still visible to anyone with sudo access. A better approach is to use a credentials file:

//server/share /mnt/mountpoint cifs credentials=/etc/samba/creds,uid=1000,gid=1000,_netdev 0 0

The credentials file should contain:

username=alice
password=secret

Set its permissions to 600 (readable only by root) with sudo chmod 600 /etc/samba/creds. After editing /etc/fstab, test the mount with sudo mount -a.

Frequently Asked Questions

How do I mount an SMB share without a password?

If the server allows guest access, use the guest option instead of a username. For example: sudo mount -t cifs //server/share /mnt/mountpoint -o guest. On macOS, use mount_smbfs //guest@server/share /mount/point. Note that many servers disable guest access for security reasons.

Why do I get “Permission denied” when trying to mount?

This usually means the server rejected your credentials or the share does not allow your user. Check the username and password, and verify that the share name is correct. On Linux, also ensure you have permission to write to the mount point (try sudo). If the server uses SMB2 or SMB3, you may need to specify vers=2.0 or vers=3.0 in the options.

How can I mount an SMB share as a non-root user on Linux?

Mounting requires root privileges unless you set the user option in /etc/fstab. Add user to the options field, and then any user can mount with mount /mnt/mountpoint. For example: //server/share /mnt/mountpoint cifs user,username=alice,noauto 0 0. The noauto option prevents automatic mounting at boot.

What is the difference between smbclient and mount.cifs?

smbclient is an interactive client that lets you browse and transfer files using commands similar to FTP. It does not integrate the share into your local filesystem. mount.cifs (or mount -t cifs) attaches the share to a directory, so you can use any normal file operation (like cp, mv, or opening files in applications) directly on the mounted path.

How do I unmount an SMB share that is “busy”?

If you get a “target is busy” error, it means a process is using files inside the mount. Close any open files or change the current directory out of the mount point. You can use lsof +f -- /mnt/mountpoint to see which processes are using it. If necessary, use sudo umount -l /mnt/mountpoint to perform a lazy unmount (detaches the filesystem immediately, but cleans up when no longer busy).

Can I mount an SMB share on Linux using an IP address instead of a hostname?

Yes, you can use the server’s IP address directly in place of the hostname. For example: sudo mount -t cifs //192.168.1.100/shared /mnt/shared -o username=alice. This is often necessary if DNS is not resolving the hostname correctly.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →