Linux smbclient Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux smbclient command and its practical applications. smbclient is a command-line tool used to access and interact with Windows file shares. We will start by installing smbclient on an Ubuntu 22.04 Docker container, and then demonstrate how to connect to a Windows share, as well as list the files and directories within it.

The smbclient package is not installed by default on Ubuntu 22.04, so we will need to install it first. Once installed, we can use smbclient to connect to a Windows share and perform various operations, such as listing the contents of the share.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") subgraph Lab Skills linux/apt -.-> lab-422922{{"`Linux smbclient Command with Practical Examples`"}} linux/cd -.-> lab-422922{{"`Linux smbclient Command with Practical Examples`"}} linux/mkdir -.-> lab-422922{{"`Linux smbclient Command with Practical Examples`"}} linux/ls -.-> lab-422922{{"`Linux smbclient Command with Practical Examples`"}} linux/sudo -.-> lab-422922{{"`Linux smbclient Command with Practical Examples`"}} end

Install smbclient on Ubuntu 22.04

In this step, we will install the smbclient package on the Ubuntu 22.04 Docker container. smbclient is a command-line tool used to access and interact with Windows file shares.

First, let's update the package index:

sudo apt-get update

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done

Next, install the smbclient package:

sudo apt-get install -y smbclient

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libsmbclient
Suggested packages:
  samba-common
The following NEW packages will be installed:
  libsmbclient smbclient
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 479 kB of archives.
After this operation, 1,711 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 libsmbclient amd64 2:4.15.5+dfsg-1ubuntu2 [222 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/main amd64 smbclient amd64 2:4.15.5+dfsg-1ubuntu2 [257 kB]
Fetched 479 kB in 0s (1,030 kB/s)
Selecting previously unselected package libsmbclient.
(Reading database ... 26536 files and directories currently installed.)
Preparing to unpack .../libsmbclient_2%3a4.15.5+dfsg-1ubuntu2_amd64.deb ...
Unpacking libsmbclient (2:4.15.5+dfsg-1ubuntu2) ...
Selecting previously unselected package smbclient.
Preparing to unpack .../smbclient_2%3a4.15.5+dfsg-1ubuntu2_amd64.deb ...
Unpacking smbclient (2:4.15.5+dfsg-1ubuntu2) ...
Setting up libsmbclient (2:4.15.5+dfsg-1ubuntu2) ...
Setting up smbclient (2:4.15.5+dfsg-1ubuntu2) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3) ...

The smbclient package is now installed on the Ubuntu 22.04 Docker container.

Connect to a Windows Share Using smbclient

In this step, we will use the smbclient command to connect to a Windows file share.

First, let's create a directory where we will store the files from the Windows share:

mkdir ~/project/windows-share

Now, let's connect to a Windows share using the smbclient command. Replace the following placeholders with the appropriate values for your Windows share:

  • //windows-server/share: The UNC path to the Windows share
  • username: The username to access the Windows share
  • password: The password for the username
smbclient //windows-server/share -U username%password

Example output:

Enter WORKGROUP\username's password:
Try "help" to get a list of possible commands.
smb: \>

You are now connected to the Windows share. You can use the following smbclient commands to interact with the share:

  • ls: List the files and directories in the current directory
  • cd directory: Change the current directory
  • get file: Download a file from the share
  • put file: Upload a file to the share
  • quit: Exit the smbclient session

For example, to list the files in the current directory on the Windows share:

smb: \> ls
  .                                   D        0  Wed Nov 30 11:59:39 2022
  ..                                  D        0  Wed Nov 30 11:59:39 2022
  example.txt                         A        12  Wed Nov 30 11:59:39 2022

                8467839 blocks of size 4096. 3524491 blocks available
smb: \>

When you're done, type quit to exit the smbclient session.

List Files and Directories on a Windows Share

In this step, we will use the smbclient command to list the files and directories on a Windows share.

First, let's connect to the Windows share using the smbclient command. Replace the following placeholders with the appropriate values for your Windows share:

  • //windows-server/share: The UNC path to the Windows share
  • username: The username to access the Windows share
  • password: The password for the username
smbclient //windows-server/share -U username%password

Example output:

Enter WORKGROUP\username's password:
Try "help" to get a list of possible commands.
smb: \>

Now, let's list the files and directories in the current directory on the Windows share:

smb: \> ls
  .                                   D        0  Wed Nov 30 11:59:39 2022
  ..                                  D        0  Wed Nov 30 11:59:39 2022
  example.txt                         A        12  Wed Nov 30 11:59:39 2022

                8467839 blocks of size 4096. 3524491 blocks available
smb: \>

The output shows the files and directories in the current directory on the Windows share. You can use the following smbclient commands to navigate and interact with the share:

  • cd directory: Change the current directory
  • get file: Download a file from the share
  • put file: Upload a file to the share
  • quit: Exit the smbclient session

When you're done, type quit to exit the smbclient session.

Summary

In this lab, we learned how to install the smbclient package on an Ubuntu 22.04 Docker container. smbclient is a command-line tool used to access and interact with Windows file shares. We first updated the package index and then installed the smbclient package using the apt-get command. The installation process also included the installation of the libsmbclient package, which is a dependency for smbclient.

After installing smbclient, we will learn how to connect to a Windows share using the smbclient command and list the files and directories on the share.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like