How to use SMB (Windows) shares on Linux system?

We can browse and mount Windows shared folders from Linux machines. We can also use same methods to access SMB shares from Linux servers.

Most of Linux distros include smbfs package as default. This package contains smb related commands.

To get a list of shares on a server, run,

$ /usr/bin/smbclient -L <server>

‘server’ parameter will be IP address or hostname of server. This command will return a list of services which contains shared drives and printers. If smb shares have security configured, that command will ask for a password.

To use a share on client machine, run,

$ /usr/bin/smbclient <service> <password>

‘service’ will be hostname and share name. For example, if you want to access ‘images’ directory on ‘johndoe’ server, service would be called \johndoe\images. But, due to shell restrictions, you need to escape backslashes, so you should use that command,

$ /usr/bin/smbclient \\\\johndoe\\\\images yourpassword

When you run that command, you will access to smbclient prompt. If you type ‘h’, you can see all available commands.

smb: \> h
ls             dir            lcd            cd             pwd            
get            mget           put            mput           rename         
more           mask           del            rm             mkdir          
md             rmdir          rd             prompt         recurse        
translate      lowercase      print          printmode      queue          
cancel         stat           quit           q              exit           
newer          archive        tar            blocksize      tarmode        
setmode        help           ?              !

If you are familiar with ftp command, you can use smbclient prompt easily.

smbclient prompt is good for testing but it isn’t enough for real work. Probably, you will want to mount smb shares to your local and use them like a standard folder. So you should use smbmount and smbumount commands for that. These commands work like mount and umount commands.

To mount a smb share to your system, run,

$ smbmount "\\\\johndoe\\\\images" -U <username> -c 'mount /target_folder -u 500 -g 100'

We use same ‘service’ parameter in that command. And we also specify target folder name to mount share to that folder. After you run that command, you will see smb share as mounted in mount command output like a NFS share.

[root@myhost]# mount
/dev/hda1 on / type ext3 (rw)
none on /proc type proc (rw)
none on /dev/pts type devpts (rw,mode=622)
//JOHNDOE/IMAGES on /target_folder type smbfs (0)