Connecting to Windows Share on Linux (samba)
I had to backup some files to the raid at my church this weekend. In trying to do so I had to have a bit of a refresh on how to do simple operations to connect to the Windows File Shares. Here are some tips on working with them on Linux. We use samba-client to interact with them.
Installing Required Tools
- smbclient - this allows us to have an interactive session with the samba share
- cifs-utils - this allows us to mount a share as a filesystem which makes working with them incredibly easy
smbclient
smbclient is the more manual was to interact with these kinds of shares but allow us to test connections and look for info on these shares before setting up a more elegant solution later on. Sometimes we only needs to short and quick method and this will help you accomplish that.
Listing Available Shares
Listing available shares will help you find what you are looking to access. Use the following:
bash-5.1$ smbclient -L //smb-server -N
Anonymous login successful
Sharename Type Comment
--------- ---- -------
lucyshare Disk Lucys Stuff
public Disk Publically accessible share
IPC$ IPC IPC Service (Samba Server)
SMB1 disabled -- no workgroup available
Note: For using a username and password
bash-5.1$ smbclient -L //smb-server -U lucy
Enter MYGROUP\lucy's password:
Sharename Type Comment
--------- ---- -------
lucyshare Disk Lucys Stuff
public Disk Publically accessible share
IPC$ IPC IPC Service (Samba Server)
SMB1 disabled -- no workgroup available
Connection to a Share Interactively
Connecting to a samba share interactively will allow you to interact with files in the share and put files there from your computer.
bash-5.1$ smbclient //smb-server/lucyshare -U lucy
Enter MYGROUP\lucy's password:
Try "help" to get a list of possible commands.
smb: \>
If allowed you can connect anonymously as well.
bash-5.1$ smbclient //smb-server/public -N
Anonymous login successful
Try "help" to get a list of possible commands.
smb: \>
Downloading a File
You can download a file using get/mget commands. mget allows for patterns to download multiple files.
bash-5.1$ smbclient //smb-server/lucyshare -U lucy
Enter MYGROUP\lucy's password:
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Mon Sep 20 01:52:43 2021
.. D 0 Mon Sep 20 01:52:43 2021
lucy.txt N 11 Mon Sep 20 01:49:25 2021
science D 0 Mon Sep 20 01:50:48 2021
959863856 blocks of size 1024. 405795168 blocks available
smb: \> get lucy.txt
getting file \lucy.txt of size 11 as lucy.txt (1.1 KiloBytes/sec) (average 1.1 KiloBytes/sec)
smb: \> mget *.txt
Get file lucy.txt? y
getting file \lucy.txt of size 11 as lucy.txt (5.4 KiloBytes/sec) (average 1.8 KiloBytes/sec)
Get file lucy.txt? smb: \>
<p class="has-text-align-left">
You can upload using put.
</p>
smb: \> put /etc/hosts hosts
putting file /etc/hosts as \hosts (85.0 kb/s) (average 85.0 kb/s)
smb: \>
cifs-utils
Using the cifs-utils package we can mount a samba share as a filesystem to use our normal commands with it.
Use the following to mount a samba share.
sudo mount -t cifs -o username=lucy //smb-server/lucyshare /mnt
This will prompt for a password and then mount the share as lucy.
Examples
To run through all of these examples you can check out my repository.
It will work for every example except for cifs-utils. The container won’t be able to mount filesystems in that manner.