1. Overview
We might face a situation where we’re supposed to send files over a Samba server. This situation can be any, such as joining a business or organization that uses a centralized Samba file server, requiring us to send all our work files to the server daily. In such a scenario, we use Samba clients to access that server and transfer files over them.
In this tutorial, we’ll learn how to send files over Samba via the command line (CLI) using smbclient and mount with CIFS commands.
Specifically, we’ll use Debian-based systems to test the commands.
2. Setting up Samba
Before diving deeper, let’s first understand Samba and set up a Samba server.
2.1. Understanding Samba
Samba is a popular, open-source application suite that allows us to share files, printers, and other computing resources between machines over the network. It uses the SMB/CIFS (Server Message Block/Common Internet File System) protocol. Moreover, it supports Windows, Linux, MacOS, and many other Unix-like systems.
2.2. Installing and Configuring Samba
Now, we need to install and configure the Samba server on our systems. In a Debian-based system, we can install Samba from its official repository. After the installation, we have to configure the Samba share in the /etc/samba/smb.conf file according to our needs.
In our case, we’ve added the following lines to our /etc/samba/smb.conf file to define a Samba share baeldungSharedFolder, at the path /home/ubuntu/baeldung:
[baeldungSharedFolder]
comment = Baeldung Samba Share Folder
path = /home/ubuntu/baeldung
browseable = yes
read only = no
guest ok = no
After configuring the Samba share, we can set a strong password for the Samba server and restart its services to implement the changes.
Once we configure the Samba server, we can access it from any client machine that supports the SMB/CIFS protocol to send files over it.
3. Using smbclient
We can use the smbclient utility on the client machine to retrieve and transfer files from the Samba server. Moreover, we can use this tool to get information about directories available on the server.
We ensure that we have smbclient installed on our system and then navigate to the directory containing the files to be sent over the server:
$ cd baeldungClientFolder/
$ ls
aFile1 aFile2 aFile3 mFile1 mFile2 singleFile
Now, let’s use smbclient to transfer these files.
3.1. Sending a Single File
To send a single file over the server, we can use this smbclient command syntax:
$ smbclient //<Server_IP>/<shared_folder> -U <username>%<password> -c "put /<path>/to/<local_filename> <remote_filename>"
Here, the -U flag is used to specify the username and password of Samba, whereas the -c flag allows us to specify a command string to be executed by smbclient.
Moreover, in this syntax, we’re required to replace the command variables with the following:
- <Server_IP>: the IP address of our Samba server, which can be obtained by executing the hostname -I command on the server.
- <shared_folder>: the name we configured for the Samba share directory.
-
: the username of the Samba server -
: the password of the Samba server -
/to/<local_filename> : the complete path and name of the file we want to send to the server. - <remote_filename>: the new name for the shared file
In our case, we’ll run the following smbclient command to send the singleFile file to the server:
$ smbclient //192.168.168.149/baeldungSharedFolder -U ubuntu%Pw123456 -c "put singleFile singleFileShared"
putting file singleFile as \singleFileShared (0.0 kb/s) (average 0.0 kb/s)
Thus, we’ve successfully transferred a file to Samba.
3.2. Sending Multiple Files
Let’s run this smbclient command with multiple put commands as a command string to connect to our Samba server and transfer the mFile1 and mFile2 files to it:
$ smbclient //192.168.168.149/baeldungSharedFolder -U ubuntu%Pw123456 -c "put mFile1 mFile1Shared; put mFile2 mFile2Shared"
putting file mFile1 as \mFile1Shared (0.0 kb/s) (average 0.0 kb/s)
putting file mFile2 as \mFile2Shared (0.0 kb/s) (average 0.0 kb/s)
Hence, these files will be shared and renamed to mFile1Shared and mFile2Shared, respectively.
3.3. Sending All Files in the Directory
Using the smbclient tool’s mput command, we can transfer all files in a directory on the client machine to the Samba server.
For instance, we can run the smbclient command to send all files located in the current directory and subdirectories to the shared folder on the Samba server:
$ smbclient //192.168.168.149/baeldungSharedFolder -U ubuntu%Pw123456 -c "prompt; recurse; mput *"
putting file aFile2 as \aFile2 (0.0 kb/s) (average 0.0 kb/s)
putting file aFile3 as \aFile3 (0.0 kb/s) (average 0.0 kb/s)
...
In the above command, the command string following the -c flag specifies multiple operations. These include prompting for permission before transfer, recursing into subdirectories, and transferring all files.
Notably, this command replaces files with the same name present on the server. To prevent this, we can add the -n flag after the command string in the command.
4. Using mount With CIFS
Apart from smbclient, we can transfer our files from our client machines to the Samba server by mounting a shared folder from the server to a local directory on the client machine using the CIFS protocol.
To share files, we first need to mount the shared folder onto the local directory.
In our case, the shared folder is located at //192.168.168.149/baeldungSharedFolder, and the local directory’s path is /mnt/clientFolder. So let’s run this mount command:
$ sudo mount -t cifs -o username=ubuntu,password=Pw123456 //192.168.168.149/baeldungSharedFolder /mnt/clientFolder
Afterward, we can run the mount command with a grep filter to verify that the shared folder is successfully mounted:
$ mount | grep "/mnt/clientFolder"
//192.168.168.149/baeldungSharedFolder on /mnt/clientFolder type cifs (...username=ubuntu…addr=192.168.168.149…)
Here, we can see the output, indicating that our directory is mounted. Now, we can treat this mounted directory as a regular part of our client system. Therefore, we can retrieve and transfer files to this mounted directory using the cp and mv commands.
4.1. Sending Multiple or Single Files
For instance, to send a single file located in the baeldungClientFolder directory on our client machine, we can run this cp command:
$ sudo cp ~/baeldungClientFolder/singleFile /mnt/clientFolder/singleFileShared
Moreover, we can also use the cp command to transfer multiple files from the local directory to our mounted directory:
$ sudo cp baeldungClientFolder/multiFile1 baeldungClientFolder/multiFile2 /mnt/clientFolder/
Finally, we’ve sent our files to Samba.
4.2. Sending All Files in the Directory
As mentioned earlier, we can also use the mv command to transfer files over Samba.
To demonstrate, let’s move all the files located in the baeldungClientFolder directory to our mounted shared directory using this mv command:
$ sudo mv baeldungClientFolder/* /mnt/clientFolder/
The above command empties the local directory and shifts all its files to the shared directory on the Samba server.
4.3. unmount Shared Folder
Lastly, we can use the unmount command to disconnect the shared folder when we no longer need it:
$ sudo umount /mnt/clientFolder/
5. Conclusion
In this article, we discussed smbclient commands for sending single, multiple, and all files located in a local directory over Samba. Additionally, we learned how to use the mount command with the CIFS protocol to mount a shared folder onto a local directory for transferring local files to the server.