smbclient

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

  1. List Shares on a Server

    smbclient -L //hostname -U username

    This command lists all available shares on the specified server.

    Example:

    smbclient -L //server -U myuser

    Output:

    Enter MYUSER's password:
    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    public          Disk      Public Share
    IPC$            IPC       IPC Service (server)
  2. Connecting to a Share

    smbclient //hostname/sharename -U username

    This command connects to a specific share on the server.

    Example:

    smbclient //server/public -U myuser

    Output:

    Enter MYUSER's password:
    smb: \>
  3. Uploading a File

    Once connected to a share, you can upload files using the put command.

    smb: \> put localfile remotefile

    Example:

    smb: \> put /path/to/localfile.txt remotefile.txt

    Output:

    putting file /path/to/localfile.txt as \remotefile.txt (0.0 kb/s) (average 0.0 kb/s)
  4. Downloading a File

    Similarly, you can download files using the get command.

    smb: \> get remotefile localfile

    Example:

    smb: \> get remotefile.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)
  5. 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
  6. Creating a Directory

    smb: \> mkdir dirname

    Example:

    smb: \> mkdir newfolder

    Output:

    smb: \>
  7. Deleting a File

    smb: \> del filename

    Example:

    smb: \> del oldfile.txt

    Output:

    smb: \>
  8. Executing Multiple Commands

    You can execute multiple commands by using the -c option.

    smbclient //hostname/sharename -U username -c 'command1; command2'

    Example:

    smbclient //server/public -U myuser -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.

  1. Create a Credentials File

    username=myuser
    password=mypassword
    domain=mydomain
  2. Use the Credentials File

    smbclient //hostname/sharename -A /path/to/credentialsfile

    Example:

    smbclient //server/public -A /path/to/credentials.txt

Using Kerberos Authentication

If your network uses Kerberos for authentication, you can connect using your Kerberos ticket.

  1. Obtain a Kerberos Ticket

    kinit myuser@MYDOMAIN
  2. Connect with Kerberos Authentication

    smbclient //hostname/sharename -k

    Example:

    smbclient //server/public -k

Summary

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.

Last updated