Here’s a concise guide to setting up file sharing in Debian using common protocols:
1. Samba (For Windows/Linux/macOS Interoperability)
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:
- Windows: Open File Explorer and enter
\\. Log in with\shared sambausercredentials. - Linux/macOS: Use
smbclientor mount viacifs:sudo mount -t cifs ///shared /mnt/shared -o username=sambauser,password=yourpassword
2. NFS (For Linux-to-Linux File Sharing)
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 :/srv/nfs/shared /mnt/nfs_shared
3. SSHFS (For Secure File Sharing via SSH)
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 ):
sshfs user@:/path/to/remote/folder /mnt/sshfs_shared
Enter the user’s SSH password when prompted.
Unmount When Done:
fusermount -u /mnt/sshfs_shared
Key Notes:
- Permissions: Ensure shared directories have correct ownership (e.g.,
chown -R :samba /srv/samba/sharedfor Samba) and permissions (e.g.,chmod -R 2775for group access). - Firewall: Always allow the required ports (Samba: 137-139, 445; NFS: 2049; SSHFS: 22).
- Testing: Use
testparm(Samba) orshowmount -e localhost(NFS) to verify configurations.
Choose the method that best fits your network environment and client requirements.
以上就是关于“如何在debian中设置文件共享”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm