Here’s a concise guide to setting up file sharing in Debian using common protocols:
Install Samba:
sudo apt update && sudo apt install samba -y
Configure Samba:
Backup the default config and edit /etc/samba/smb.conf:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
sudo nano /etc/samba/smb.conf
Add a share section (customize [shared] and path as needed):
[shared]
comment = Shared Folder
path = /srv/samba/shared
browseable = yes
read only = no
guest ok = no
valid users = @samba # Restrict to "samba" group
create mask = 0775
directory mask = 0775
Create Share Directory & Set Permissions:
sudo mkdir -p /srv/samba/shared
sudo chown -R :samba /srv/samba/shared # Assign "samba" group
sudo chmod -R 2775 /srv/samba/shared # Set sticky bit for group ownership
Add Samba Users:
Create a system user (no login shell) and add to Samba:
sudo useradd -M -s /usr/sbin/nologin sambauser
sudo passwd sambauser # Set system password (optional)
sudo smbpasswd -a sambauser # Add to Samba with a separate password
sudo smbpasswd -e sambauser # Enable the account
Restart Services & Enable Autostart:
sudo systemctl restart smbd nmbd
sudo systemctl enable smbd nmbd
Firewall Configuration (UFW):
Allow Samba traffic:
sudo ufw allow samba
Test Access:
\\<Debian-IP>\shared. Log in with sambauser credentials.smbclient or mount via cifs:sudo mount -t cifs //<Debian-IP>/shared /mnt/shared -o username=sambauser,password=yourpassword
Install NFS Server:
sudo apt update && sudo apt install nfs-kernel-server -y
Configure NFS Shares:
Edit /etc/exports to define shared directories and client access:
sudo nano /etc/exports
Add a line (e.g., share with a subnet):
/srv/nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check)
Export Shares & Restart Service:
sudo exportfs -a
sudo systemctl restart nfs-kernel-server
sudo systemctl enable nfs-kernel-server
Firewall Configuration (UFW):
Allow NFS ports (2049, 111):
sudo ufw allow from 192.168.1.0/24 to any port nfs
Test Access (Client-Side):
Install NFS client and mount the share:
sudo apt install nfs-common -y
sudo mkdir -p /mnt/nfs_shared
sudo mount <Debian-IP>:/srv/nfs/shared /mnt/nfs_shared
Install SSHFS:
sudo apt update && sudo apt install sshfs -y
Create Local Mount Point:
sudo mkdir -p /mnt/sshfs_shared
Mount Remote Directory:
Use SSH to mount a remote folder (replace user and <Debian-IP>):
sshfs user@<Debian-IP>:/path/to/remote/folder /mnt/sshfs_shared
Enter the user’s SSH password when prompted.
Unmount When Done:
fusermount -u /mnt/sshfs_shared
chown -R :samba /srv/samba/shared for Samba) and permissions (e.g., chmod -R 2775 for group access).testparm (Samba) or showmount -e localhost (NFS) to verify configurations.Choose the method that best fits your network environment and client requirements.