Samba is a free software re-implementation of the SMB networking protocol, and was originally developed by Andrew Tridgell.
This details how to install Samba on a Linux system.
- Install the
sambapackage using your package manager (i.e.yay) - this will automatically install other dependencies such assmbclientandcifs-utils.
This details how to mount a remote Samba share to our local machine.
- Automatic mounting: As mount entry
- Use of the x-systemd.automount mount option in /etc/fstab
- systemd.mount
- Mounting SMB Shares with Systemd
-
Ensure that all necessary packages are installed.
-
Create the directory that will be used as the mountpoint for the remote storage (i.e.
/mnt/mynas):sudo mkdir -p <mountpoint>
For example:
sudo mkdir -p /mnt/mynas
-
Create our credentials file that will be used to access the remote directory (i.e.
.smbcreds):-
Pre-create the directory for the credentials file:
mkdir -p ~/.config/smb -
Write and save the credentials file:
nano ~/.config/smb/.smbcredsContent of the credentials file:
username=<nas-user> password=<nas-password>
Replace the values with the credentials of the Samba share user accordingly.
-
Update the permissions of the credentials file to secure it:
chmod 600 ~/.config/smb/.smbcreds
-
-
(Optional) Add the IP address of our remote server to our
/etc/hostsfile:sudo nano /etc/hosts
Sample IP address entry:
# example.org 192.168.0.120 mynasReplace the IP address and the hostname of the remote Samba server accordingly.
-
Add our mount configuration for the remote directory using the system's
fstabfile:-
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:mynasdenotes the sample hostname of the remote server, whilemydirdenotes 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.
Feel free to omit or add to these sample options depending on your needs.
-
Write the line to the
/etc/fstabfile:echo "${fstab_line}" | sudo tee -a /etc/fstab
-
-
Alternatively, if using
fstabis not a lasting option such as on immutable systems like SteamOS or Bazzite, we could add our mount configuration usingsystemdmount units instead:-
Get the numerical value of our user ID (UID):
id -u
Sample UID output:
1000
-
Get the numerical value of our group ID (GID):
id -g
Sample GID output:
1000
-
The name of the mount unit file to create, which requires corresponding to the intended mountpoint (i.e.
/mnt/mynas):systemd-escape --path "<mountpoint>" --suffix=mountFor example:
systemd-escape --path "/mnt/mynas" --suffix=mountSample output:
mnt-mynas.mount -
Add our intended mount options to the mount unit file:
-
Create the mount unit file (i.e.
mnt-mynas.mount):sudo nano /etc/systemd/system/'<mount-unit-file>'For example:
sudo nano /etc/systemd/system/'mnt-mynas.mount' -
Add the following configuration to the file:
[Unit] Description=Mount Samba share After=network-online.target Wants=network-online.target [Mount] What=//<nas-host>/<nas-share> Where=<mountpoint> Type=cifs Options=_netdev,nofail,mfsymlinks,credentials=/home/<user>/.config/smb/.smbcreds,vers=3.0,uid=<uid>,gid=<gid>,iocharset=utf8 [Install] WantedBy=multi-user.target
Replace the following placeholders with your own values accordingly:
<nas-host>: The hostname or IP address of the remote Samba server (i.e.mynas)<nas-share>: The remote directory on the Samba server (i.e.mydir)<mountpoint>: The intended mountpoint of the remote directory (i.e./mnt/mynas)<user>: The username of the user that is mounting the remote directory (i.e.myuser)<uid>: The numerical value of the user's UID (i.e.1000)<gid>: The numerical value of the user's GID (i.e.1000)
Feel free to also omit or add to these sample mount options depending on your needs.
-
Sample mount unit file configuration:
[Unit] Description=Mount Samba share After=network-online.target Wants=network-online.target [Mount] What=//mynas/mydir Where=/mnt/mynas Type=cifs Options=_netdev,nofail,mfsymlinks,credentials=/home/myuser/.config/smb/.smbcreds,vers=3.0,uid=1000,gid=1000,iocharset=utf8 [Install] WantedBy=multi-user.target
-
-
-
Reload the
systemdmanager configuration for our configuration to be recognised:sudo systemctl daemon-reload
-
Mount the remote directory to our mountpoint (i.e.
/mnt/mynas) based on ourfstabconfiguration:sudo mount <mountpoint>
For example:
sudo mount /mnt/mynas
-
Alternatively, enable and start the mount unit file (i.e.
mnt-mynas.mount) to mount the remote directory immediately and on boot:sudo systemctl enable --now '<mount-unit-file>'
For example:
sudo systemctl enable --now 'mnt-mynas.mount'
This details how to unmount a remote directory from our local machine.
-
Depending on how you have mounted the remote directory, unmount it from your system:
-
If you had mounted it using the
mountcommand, unmount the remote storage from the mountpoint (i.e./mnt/mynas) usingumount:sudo umount <mountpoint>
For example:
sudo umount /mnt/mynas
If you receive an error message such as the target being busy, add the
--lazyflag to the same command to force the unmount:sudo umount --lazy <mountpoint>
-
Alternatively, if you had mounted it using a
systemdmount unit file (i.e.mnt-mynas.mount), unmount the remote storage by stopping the mount unit:sudo systemctl stop '<mount-unit-file>'For example:
sudo systemctl stop 'mnt-mynas.mount'
-
-
Prevent the remote directory from being automatically mounted from the next boot, again, depending on how you have mounted the remote directory:
-
If you had made your mount configuration using the
fstabfile, remove it from the file:sudo nano /etc/fstab
Remove the line that corresponds to the remote directory by deleting it 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 + #//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
-
Alternatively, if you had made your mount configuration using a
systemdmount unit file (i.e.mnt-mynas.mount), disable the mount unit and delete its corresponding file:sudo systemctl disable '<mount-unit-file>' && sudo rm /etc/systemd/system/'<mount-unit-file>'
For example:
sudo systemctl disable 'mnt-mynas.mount' && sudo rm /etc/systemd/system/'mnt-mynas.mount'
-
-
Reload the
systemdmanager configuration for our configuration changes to be recognised:sudo systemctl daemon-reload
This details how to share a local directory remotely as a Samba share.
-
Ensure that all necessary packages are installed.
-
Create a basic Samba configuration to start with if one does not exist yet:
-
Create the Samba configuration file:
sudo nano /etc/samba/smb.conf
-
Add and save the following configuration to the file:
[global] follow symlinks = yes wide links = yes unix extensions = no
-
-
Prepare the directory that is to be shared on the system, if not already:
mkdir -p <share-directory-path>
For example, create a directory (i.e.
myshare) on a mounted external storage (i.e./run/media/myuser/external-storage):mkdir -p /run/media/myuser/external-storage/myshare
-
Create a configuration file dedicated to configuring the Samba share:
-
Create the Samba share configuration file (i.e.
myshare.conf):sudo nano /etc/samba/<share-config-file>
For example:
sudo nano /etc/samba/myshare.conf
-
Add and save the following configuration to the Samba share configuration file:
[<share-name>] path = <share-directory-path> browseable = yes writable = yes guest ok = no read only = no
For example:
[myshare] path = /run/media/myuser/external-storage/myshare browseable = yes writable = yes guest ok = no read only = no
Feel free to update the Samba share configuration according to your needs.
-
Update the system's Samba configuration file to include the new Samba share configuration file we had just created:
sudo nano /etc/samba/smb.conf
Add the following line to the existing configuration:
[global] follow symlinks = yes wide links = yes unix extensions = no + include = <share-config-file-path>For example:
[global] follow symlinks = yes wide links = yes unix extensions = no include = /etc/samba/myshare.conf
-
-
Since the Samba share was configured to require an authenticated user, create a Samba user credentials for an existing user on the system (i.e.
myuser):sudo smbpasswd -a <username>
For example:
sudo smbpasswd -a myuser
Set a secure password for the Samba share user when prompted.
-
Finally, start and enable the Samba service to apply the configuration changes we had made:
sudo systemctl enable --now smb.serviceor restart it if it is already running:
sudo systemctl restart smb.service