温馨提示×

如何在debian中设置文件共享

小樊
36
2025-10-06 18:13:08
栏目: 智能运维

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 \\<Debian-IP>\shared. Log in with sambauser credentials.
  • Linux/macOS: Use smbclient or mount via cifs:
    sudo mount -t cifs //<Debian-IP>/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 <Debian-IP>:/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 <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

Key Notes:

  • Permissions: Ensure shared directories have correct ownership (e.g., chown -R :samba /srv/samba/shared for Samba) and permissions (e.g., chmod -R 2775 for group access).
  • Firewall: Always allow the required ports (Samba: 137-139, 445; NFS: 2049; SSHFS: 22).
  • Testing: Use testparm (Samba) or showmount -e localhost (NFS) to verify configurations.

Choose the method that best fits your network environment and client requirements.

0