Samba is a free software re-implementation of the SMB networking protocol, and was originally developed by Andrew Tridgell.
This details how to mount a remote directory to our local machine using Samba.
-
Ensure that all necessary packages are installed, including
cifs-utils
,samba
, andsmbclient
.[!NOTE]
Installingsamba
will also install other required packages such ascifs-utils
andsmbclient
automatically. -
Create the directory that will be used as the mounting point (i.e.
/mnt/mynas
):sudo mkdir /mnt/mynas
-
Change ownership of the mounting point to our user:
sudo chown ${USER}: /mnt/mynas
-
Create our credentials file that will be used to access the remote directory (i.e.
.smbcreds
):mkdir -p ~/.config/smb
nano ~/.config/smb/.smbcreds
Content of the credentials file:
username=mynasuser password=mynaspassword
-
Add the IP address of our remote server to our
/etc/hosts
file:sudo nano /etc/hosts
Sample IP address entry:
# example.org 192.168.0.120 mynas
-
Add a line with our mounting options for the remote directory to our
fstab
file:-
Declare the mounting options for the remote directory:
fstab_line="//mynas/mydir /mnt/mynas cifs _netdev,nofail,mfsymlinks,users,x-systemd.automount,credentials=${HOME}/.config/smb/.smbcreds,vers=3.0,uid=$(id -u),gid=$(id -g),iocharset=utf8 0 0"
Meaning behind some of the included options:
//mynas/mydir
:mynas
denotes the sample hostname of the remote server, whilemydir
denotes the sample remote directory on the server./mnt/mynas
: The sample directory that will be used as the mounting point._netdev
: Indicates that the filesystem depends on network availability. It ensures that the mount attempt waits until the network is up.nofail
: Allows the boot process to continue even if this mount point fails. It prevents boot hang-ups in case the remote directory is unavailable.mfsymlinks
: Enables support for symbolic links in the CIFS/SMB share, allowing symbolic links on the remote system to be followed locally.users
: Allows non-root users to mount and unmount the filesystem.x-systemd.automount
: Automatically mounts the share when it is accessed, rather than at boot time. This helps avoid delays if the remote system isn't immediately available.
-
Write the line to the
/etc/fstab
file:echo "${fstab_line}" | sudo tee -a /etc/fstab
-
-
Reload the
daemon
for it to recognise the changes made to ourfstab
file:sudo systemctl daemon-reload
-
Mount the remote directory to our new mounting point:
sudo mount /mnt/mynas
This details how to mount a remote directory to our local machine using Samba.
-
Unmount the remote directory from our local machine:
sudo umount /mnt/mynas
[!IMPORTANT]
This example assumes that the remote directory is mounted at/mnt/mynas
. Update the path accordingly.If you receive an error message such as the target being busy, you can use the following command to force the unmount:
sudo umount -l /mnt/mynas
The
-l
flag stands forlazy
and will force the detachment of the mount point. -
If the previously mounted directory has been added as a mount entry in the
/etc/fstab
file, you can remove it by editing the file:sudo nano /etc/fstab
Remove the line that corresponds to the mount point you wish to remove by deleting or commenting it out. For example:
# example.org # //mynas/mydir /mnt/mynas cifs _netdev,nofail,mfsymlinks,credentials=/home/deck/.config/smb/.smbcreds,vers=3.0,uid=1000,gid=1000,iocharset=utf8 0 0
Save the file and exit the editor. This will prevent the mount point from being automatically mounted from the next boot.