Guide to Creating a Shared Hard Drive Partition via Samba on Linux

Guide to Creating a Shared Hard Drive Partition via Samba on Linux

Guide to Creating a Shared Hard Drive Partition via Samba on Linux

This article will provide detailed instructions on how to create a hard drive partition and share it over the network using Samba on Linux-based operating systems such as Ubuntu, Raspberry Pi, and other distributions.

Step 1: Mount the drive

Check the list of drives

First, check the existing drives and partitions on the system:

sudo lsblk -f

or more simply:

sudo lsblk

Example output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 111.8G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 111.3G  0 part /
sdb      8:16   0   1.8T  0 disk 
└─sdb1   8:17   0   1.8T  0 part 

In this example, we see:

  • sda is the system drive with two partitions: sda1 (boot) and sda2 (root)
  • sdb is the second drive with one partition sdb1 that has not been mounted yet

We will use the sdb1 partition to create the share.

Format the drive (optional)

If the sdb1 partition has not been formatted, you can format it with the ext4 file system:

sudo mkfs -t ext4 /dev/sdb1

Note: This command will erase all data on the partition.

Create a mount point

Create a directory to be used as the mount point for the drive:

sudo mkdir -p /mnt/data_share

Mount the drive

Mount the drive to the created mount point:

sudo mount /dev/sdb1 /mnt/data_share

Assign permissions to the mount directory

Assign ownership to the user (for example: pi) so they can read/write to the drive:

sudo chown -R pi:pi /mnt/data_share

Step 2: Install and configure Samba

Install Samba

sudo apt install samba samba-common-bin -y

Configure Samba

Open the Samba configuration file for editing:

sudo nano /etc/samba/smb.conf

Add the following share configuration at the end of the file:

[NAS]
path = /mnt/data_share
writeable = yes
guest ok = yes
create mask = 0777
directory mask = 0777

Restart the Samba service

sudo /etc/init.d/smbd restart

Step 3: Support other drive formats (optional)

If you need support for the exFAT format (commonly used for external hard drives):

sudo apt install exfat-fuse exfat-utils -y

Step 4: Manage users and permissions

Check current permissions

ls -l /mnt/data_share

Add a new user

sudo useradd leolion

Add the user to the authorized group

sudo usermod -aG pi leolion

Create a Samba password for the user

sudo smbpasswd -a leolion

This command will prompt you to enter and confirm the password for the user.

Step 5: Configure the drive to mount automatically at boot

Determine the partition UUID

First, get the UUID of the partition that needs to be mounted:

sudo lsblk -f

Example output:

NAME   FSTYPE   LABEL UUID                                 MOUNTPOINT
sda                                                        
├─sda1 vfat           EFI5-7823                            /boot/efi
└─sda2 ext4           81bb4976-a820-4e0d-92ab-1a754f9837bd /
sdb                                                        
└─sdb1 ext4           a63e1694-79fe-4d7d-9e69-a9d4f67ce28a 

In this example, the UUID of the sdb1 partition is a63e1694-79fe-4d7d-9e69-a9d4f67ce28a.

Edit the fstab file

sudo nano /etc/fstab

Add the following line at the end of the file so the system automatically mounts the drive at boot:

UUID=a63e1694-79fe-4d7d-9e69-a9d4f67ce28a /mnt/data_share ext4 defaults,auto,users,rw,nofail,noatime 0 0

Connect to the Samba shared folder

After completing the configuration, you can access the shared folder from other devices on the network:

From Windows

Open File Explorer and enter in the address bar:

\\<server_IP_address>\NAS

From Linux

smbclient //server_IP_address/NAS -U username

From macOS

Open Finder, select "Connect to Server" and enter:

smb://server_IP_address/NAS

With the above steps, you have completed the installation and configuration of a simple NAS server using Samba on Linux, allowing you to share data over the network easily and securely.

Bạn đã đăng ký thành công vào Geek Playground
Tuyệt vời! Tiếp theo, hoàn tất thanh toán để có quyền truy cập đầy đủ vào Geek Playground
Chào mừng trở lại! Bạn đã đăng nhập thành công.
Thành công! Tài khoản của bạn đã được kích hoạt đầy đủ, bạn hiện có quyền truy cập vào tất cả nội dung.
Thành công! Thông tin thanh toán của bạn đã được cập nhật.
Cập nhật thông tin thanh toán không thành công.