samba and smbclient packages, mount your additional storage device to a specific directory, and then configure the /etc/samba/smb.conf file with the appropriate path and security settings. After restarting the Samba services, you can access the shared folder from Windows or other Linux clients using the server's IP address. This process ensures that your data is accessible across your local network while maintaining proper permissions and security controls for your Ubuntu system.
Prerequisites and Installation
Before configuring Samba, ensure that your Ubuntu 22.04.3 system has the necessary software installed. You will need administrative privileges to modify system configurations and install new packages. Open your terminal and update your package list to ensure you are installing the latest versions of the required tools.
sudo apt update
sudo apt upgrade
Next, install the Samba suite and the client utilities. The server package provides the daemon and configuration files, while the client utilities allow you to test connections from the command line. Run the following command to install both:
sudo apt install samba smbclient
Once the installation completes, verify that the Samba service is installed correctly by checking its version. This confirms that the binaries are available in your system path. You can do this by running smbd --version. If the command returns a version number, the installation was successful. If you encounter permission errors, ensure you are running the commands with sudo.
Preparing the Second Drive
The next critical step is to prepare the second drive so that it is mounted at boot and accessible by the Samba service. First, identify the device name of your second drive using the lsblk command. Look for the drive that is not your root partition, typically labeled as sdb, sdc, or similar.
lsblk
Create a mount point directory where the drive will be accessible. A common convention is to create a directory under /mnt or /media. For example, you might create a directory named shared_drive:
sudo mkdir -p /mnt/shared_drive
You should now mount the drive manually to test that it works. Use the mount command with the device identifier. If your drive is formatted as ext4, the command would look like this:
sudo mount /dev/sdb1 /mnt/shared_drive
To ensure the drive mounts automatically every time the system boots, you need to add an entry to the /etc/fstab file. Open the file with a text editor like nano or vim:
sudo nano /etc/fstab
Add a line at the end of the file specifying the device, mount point, filesystem type, and options. For an ext4 filesystem, use:
/dev/sdb1 /mnt/shared_drive ext4 defaults 0 2
Save the file and exit the editor. It is highly recommended to check your fstab syntax before rebooting to avoid boot failures. You can do this by running sudo findmnt --verify. If there are no errors, you can proceed to set up the Samba configuration.
Configuring Samba
Samba configuration is stored in the /etc/samba/smb.conf file. Before editing, it is wise to create a backup of the original configuration file. This allows you to restore the default settings if something goes wrong.
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
Open the main configuration file for editing:
sudo nano /etc/samba/smb.conf
At the bottom of the file, add a new section defining your share. The section header should be enclosed in square brackets and serve as the share name visible to network clients. Inside the section, specify the path to your mounted drive, whether guests are allowed, and the writable status.
[SharedDrive]
comment = Shared Second Drive
path = /mnt/shared_drive
browseable = yes
read only = no
guest ok = no
create mask = 0775
directory mask = 0775
In this configuration, guest ok = no ensures that users must authenticate to access the share, which is crucial for security. The create mask and directory mask settings define the permissions for newly created files and directories, allowing group members to read and write.
If you prefer to allow public access without authentication, you can change guest ok to yes and remove user-specific restrictions. However, for most home and small business environments, authenticated access is safer. After saving the changes, validate the configuration file syntax by running:
testparm
This tool checks for common errors in the smb.conf file and displays the effective configuration. If testparm reports no errors, your configuration is syntactically correct.
Setting Up Users and Permissions
Since we configured the share to require authentication, you must create a Samba user. This user does not need to have a shell account on the system, but they must exist in the Samba password database. First, ensure the user exists in the Linux system:
sudo adduser sambashareuser
Then, add this user to the Samba database. You will be prompted to enter a new Samba password:
sudo smbpasswd -a sambashareuser
Ensure that the Linux user has read and write permissions to the mounted directory. You can change the ownership of the mount point to the Samba user:
sudo chown -R sambashareuser:sambashareuser /mnt/shared_drive
Additionally, adjust the permissions to ensure the user can write to the drive:
sudo chmod -R 775 /mnt/shared_drive
Finally, restart the Samba services to apply all changes:
sudo systemctl restart smbd
sudo systemctl restart nmbd
You can verify that the services are running by checking their status:
sudo systemctl status smbd
For more detailed troubleshooting regarding access issues, refer to our guide on Troubleshooting Samba Share Access Issues on Ubuntu 22 LTS and Windows 10. Additionally, if you plan to manage multiple shares, understanding Resolving Samba Permissions Inconsistency Across Multiple Systems is essential for maintaining consistent access rights.
Connecting from Client Devices
Once the server is configured and running, you can connect from other devices on your network. On a Windows machine, open File Explorer and enter the UNC path to the share in the address bar: \\IP_ADDRESS_OF_UBUNTU\SharedDrive. Replace IP_ADDRESS_OF_UBUNTU with the actual IP address of your Ubuntu server.
You will be prompted for the username and password you created earlier. Enter these credentials to gain access to the shared drive. On Linux clients, you can mount the share using the cifs-utils package and the mount command with the vers=3.0 option for better compatibility.
If you experience connectivity issues, ensure that the firewall on the Ubuntu server allows SMB traffic. By default, Ubuntu uses ufw (Uncomplicated Firewall). To allow Samba traffic, run:
sudo ufw allow samba
This command opens the necessary ports for Samba communication. After applying the firewall rule, try connecting again. If problems persist, check the Samba logs located in /var/log/samba/ for detailed error messages.
Frequently Asked Questions
Why can't I see the Samba share on my Windows network?
Windows often relies on NetBIOS name resolution, which may not work reliably on modern networks. Instead of browsing the network neighborhood, directly enter the UNC path using the server's IP address in the File Explorer address bar. Ensure that the nmbd service is running on the Ubuntu server and that your Windows machine is on the same subnet.
How do I change the Samba password for a user?
You can change the Samba password at any time by running the command sudo smbpasswd username, replacing username with the actual Samba user name. This command will prompt you to enter the new password twice. Note that this only changes the Samba password; it does not affect the Linux system password unless you also update it separately.
Can I share the drive with read-only access?
Yes, you can restrict write access by changing the read only directive in the smb.conf file to yes. Alternatively, you can keep read only = no but set write list to specific users who are allowed to modify files, while others will have read-only access. This provides granular control over permissions.
What should I do if the drive unmounts after a reboot?
If the drive fails to mount automatically, check the /etc/fstab file for syntax errors. Use the findmnt --verify command to validate the entries. Also, ensure that the device identifier (like /dev/sdb1) has not changed. Using UUIDs in fstab is more reliable than device names because UUIDs remain constant even if disk ordering changes.
Is it safe to enable guest access on my Samba share?
Enabling guest access allows anyone on the network to read or write to the share without a password, which poses significant security risks. It is generally recommended to disable guest access (guest ok = no) and require authentication for all users. If you must use guest access, ensure the directory permissions are restricted to prevent accidental deletion or modification of important files.