smbclient is a command-line tool in Samba that allows users to interact with Windows network shares and printers. It provides an FTP-like interface to access and manage files on SMB/CIFS servers, including Windows machines and other Samba servers. This tool is useful for transferring files, accessing shared directories, and testing SMB connections.
General Usage
smbclient [OPTION] [SERVICE][COMMAND]
Common Options
-L host: List shares available on a host.
-U user: Specify the username for authentication.
-W workgroup: Specify the workgroup or domain.
-I ip_address: Specify the IP address of the host.
-c command: Execute a command immediately after connecting.
Basic Usage Examples
List Shares on a Server
smbclient-L//hostname-Uusername
This command lists all available shares on the specified server.
Example:
smbclient-L//server-Umyuser
Output:
Enter MYUSER's password:
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
public Disk Public Share
IPC$ IPC IPC Service (server)
Connecting to a Share
smbclient//hostname/sharename-Uusername
This command connects to a specific share on the server.
Example:
smbclient//server/public-Umyuser
Output:
Enter MYUSER's password:
smb: \>
Uploading a File
Once connected to a share, you can upload files using the put command.
smb:\>putlocalfileremotefile
Example:
smb:\>put/path/to/localfile.txtremotefile.txt
Output:
putting file /path/to/localfile.txt as \remotefile.txt (0.0 kb/s) (average 0.0 kb/s)
Downloading a File
Similarly, you can download files using the get command.
smb:\>getremotefilelocalfile
Example:
smb:\>getremotefile.txt/path/to/localfile.txt
Output:
getting file \remotefile.txt of size 12345 as /path/to/localfile.txt (0.0 kb/s) (average 0.0 kb/s)
Listing Files in a Directory
smb:\>ls
Example:
smb:\>ls
Output:
. D 0 Mon Jul 19 12:34:56 2021
.. D 0 Mon Jul 19 12:34:56 2021
file.txt A 12345 Mon Jul 19 12:34:56 2021
12345678 blocks of size 1024. 567890 blocks available
Creating a Directory
smb:\>mkdirdirname
Example:
smb:\>mkdirnewfolder
Output:
smb: \>
Deleting a File
smb:\>delfilename
Example:
smb:\>deloldfile.txt
Output:
smb: \>
Executing Multiple Commands
You can execute multiple commands by using the -c option.
smbclient//server/public-Umyuser-c'ls; get remotefile.txt /path/to/localfile.txt'
Output:
Enter MYUSER's password:
. D 0 Mon Jul 19 12:34:56 2021
.. D 0 Mon Jul 19 12:34:56 2021
remotefile.txt A 12345 Mon Jul 19 12:34:56 2021
12345678 blocks of size 1024. 567890 blocks available
getting file \remotefile.txt of size 12345 as /path/to/localfile.txt (0.0 kb/s) (average 0.0 kb/s)
Advanced Usage
Authenticating with a Password File
Instead of entering a password interactively, you can use a credentials file.
Create a Credentials File
Use the Credentials File
Example:
Using Kerberos Authentication
If your network uses Kerberos for authentication, you can connect using your Kerberos ticket.
Obtain a Kerberos Ticket
Connect with Kerberos Authentication
Example:
Summary
Command
Purpose
Example Command
smbclient -L //hostname -U username
List available shares on a server
smbclient -L //server -U myuser
smbclient //hostname/sharename -U username
Connect to a specific share
smbclient //server/public -U myuser
smb: \> put localfile remotefile
Upload a file to the connected share
put /path/to/localfile.txt remotefile.txt
smb: \> get remotefile localfile
Download a file from the connected share
get remotefile.txt /path/to/localfile.txt
smb: \> ls
List files in the current directory
ls
smb: \> mkdir dirname
Create a new directory
mkdir newfolder
smb: \> del filename
Delete a file
del oldfile.txt
smbclient //hostname/sharename -A /path/to/credentialsfile
Use a credentials file for authentication
smbclient //server/public -A /path/to/credentials.txt
smbclient //hostname/sharename -k
Use Kerberos authentication
smbclient //server/public -k
Conclusion
smbclient is a powerful tool for interacting with SMB/CIFS shares from the command line. It provides a wide range of options and commands for accessing, managing, and transferring files on network shares. Whether you need to list available shares, connect to a specific share, or perform file operations, smbclient offers the functionality needed for effective network file management in a Samba environment. Understanding its options and commands is essential for system administrators and users working with network shares.