Skip to Content

Samba/NFS-Dateifreigabe

Samba wird zum Teilen von Dateien mit Windows-Systemen (SMB/CIFS-Protokoll) verwendet, während NFS für den Dateiaustausch zwischen Linux-Systemen verwendet wird.

Samba-Dateifreigabe

Installieren Sie Samba

sudo apt update sudo apt install samba samba-common-bin -y # Check version smbd --version # Check service status sudo systemctl status smbd nmbd

Grundkonfiguration

Die Samba-Konfigurationsdatei ist /etc/samba/smb.conf:

# Back up the original configuration sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak # Edit the configuration sudo nano /etc/samba/smb.conf

Öffentliche Freigabe (keine Authentifizierung)

# /etc/samba/smb.conf [global] workgroup = WORKGROUP server string = Ubuntu File Server security = user map to guest = Bad User dns proxy = no # Performance tuning socket options = TCP_NODELAY IPTOS_LOWDELAY read raw = yes write raw = yes max xmit = 65535 [public] path = /srv/samba/public browsable = yes read only = no guest ok = yes create mask = 0664 directory mask = 0775 force user = nobody
# Create the shared directory sudo mkdir -p /srv/samba/public sudo chown nobody:nogroup /srv/samba/public sudo chmod 0775 /srv/samba/public

Authentifizierte Freigabe

[shared] path = /srv/samba/shared browsable = yes read only = no guest ok = no valid users = @smbgroup create mask = 0660 directory mask = 0770 force group = smbgroup [homes] comment = Home Directories browsable = no read only = no valid users = %S create mask = 0600 directory mask = 0700
# Create a Samba user group sudo groupadd smbgroup # Create a system user and add to the group sudo useradd -M -s /usr/sbin/nologin smbuser sudo usermod -aG smbgroup smbuser # Set a Samba password (independent of the system password) sudo smbpasswd -a smbuser sudo smbpasswd -e smbuser # Enable the user # Create the shared directory sudo mkdir -p /srv/samba/shared sudo chown root:smbgroup /srv/samba/shared sudo chmod 0770 /srv/samba/shared

Validieren und starten

# Test configuration file syntax testparm # Restart services sudo systemctl restart smbd nmbd # Enable at boot sudo systemctl enable smbd nmbd # Open Samba ports in the firewall sudo ufw allow samba

Client-Verbindungen

Linux-Client

# Install client tools sudo apt install cifs-utils smbclient -y # Browse shares smbclient -L //server-ip -U smbuser # Mount a share sudo mkdir -p /mnt/samba sudo mount -t cifs //server-ip/shared /mnt/samba -o username=smbuser,password=yourpass,uid=$(id -u),gid=$(id -g) # Auto-mount at boot (/etc/fstab) # First create a credentials file sudo tee /etc/samba/.credentials << 'EOF' username=smbuser password=yourpass EOF sudo chmod 600 /etc/samba/.credentials # Add an fstab entry echo "//server-ip/shared /mnt/samba cifs credentials=/etc/samba/.credentials,uid=1000,gid=1000,iocharset=utf8 0 0" | sudo tee -a /etc/fstab sudo mount -a

Windows-Client

Geben Sie im Datei-Explorer Folgendes in die Adressleiste ein:

\\server-ip\shared

So ordnen Sie ein Netzlaufwerk zu:

  1. Klicken Sie mit der rechten Maustaste auf „Dieser PC“ -> „Netzlaufwerk zuordnen“
  2. Geben Sie \\server-ip\shared ein
  3. Aktivieren Sie „Mit unterschiedlichen Anmeldeinformationen verbinden“

NFS-Dateifreigabe

Installieren Sie den NFS-Server

sudo apt update sudo apt install nfs-kernel-server -y # Check status sudo systemctl status nfs-server

Exportierte Verzeichnisse konfigurieren

Bearbeiten Sie /etc/exports:

# /etc/exports # Format: directory client(options) # Allow a single IP /srv/nfs/data 192.168.1.100(rw,sync,no_subtree_check) # Allow an entire subnet /srv/nfs/data 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash) # Allow multiple clients /srv/nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check) 10.0.0.0/8(ro,sync,no_subtree_check) # Read-only share /srv/nfs/public *(ro,sync,no_subtree_check)

Allgemeine Optionen

OptionBeschreibung
rwLese-/Schreibzugriff
roNur lesender Zugriff
syncSynchrone Schreibvorgänge (sicherer)
asyncAsynchrone Schreibvorgänge (schneller, aber riskanter)
no_subtree_checkTeilbaumprüfung deaktivieren (verbessert die Leistung)
no_root_squashBehalten Sie die Root-Rechte des Clients bei
root_squashRoot auf „Niemand“ zuordnen (Standard)
all_squashOrdnen Sie alle Benutzer niemandem zu
anonuid/anongidGeben Sie die UID/GID für anonyme Benutzer an

Erstellen und starten Sie Freigaben

# Create shared directories sudo mkdir -p /srv/nfs/data /srv/nfs/shared /srv/nfs/public sudo chown nobody:nogroup /srv/nfs/data /srv/nfs/shared /srv/nfs/public sudo chmod 0777 /srv/nfs/data # Apply the export configuration sudo exportfs -ra # View current exports sudo exportfs -v # Open NFS ports in the firewall sudo ufw allow from 192.168.1.0/24 to any port nfs sudo ufw allow from 192.168.1.0/24 to any port 111 # rpcbind

NFS-Client

# Install the client sudo apt install nfs-common -y # View the server's export list showmount -e server-ip # Mount an NFS share sudo mkdir -p /mnt/nfs sudo mount -t nfs server-ip:/srv/nfs/data /mnt/nfs # Mount with NFS v4 sudo mount -t nfs4 server-ip:/srv/nfs/data /mnt/nfs # Auto-mount at boot (/etc/fstab) echo "server-ip:/srv/nfs/data /mnt/nfs nfs defaults,_netdev 0 0" | sudo tee -a /etc/fstab sudo mount -a

Automatisches Mounten mit Autofs

# Install autofs sudo apt install autofs -y # Configure the master map file echo "/mnt/nfs /etc/auto.nfs" | sudo tee -a /etc/auto.master # Configure the NFS map echo "data -rw,soft,intr server-ip:/srv/nfs/data" | sudo tee /etc/auto.nfs # Restart autofs sudo systemctl restart autofs # Access triggers automatic mounting ls /mnt/nfs/data

NFS-Leistungsoptimierung

# Server: increase the number of NFS threads # Edit /etc/nfs.conf sudo tee -a /etc/nfs.conf << 'EOF' [nfsd] threads = 16 EOF # Client: optimized mount options sudo mount -t nfs -o rw,hard,intr,rsize=1048576,wsize=1048576,timeo=600 server-ip:/srv/nfs/data /mnt/nfs

Samba vs. NFS-Vergleich

FunktionSamba (SMB/CIFS)NFS
ProtokollSMB/CIFSNFS v3/v4
AnwendungsfallGemischtes Windows + LinuxZwischen Linux/Unix-Systemen
AuthentifizierungSeparate PasswortdatenbankUID/GID-basiert oder Kerberos
LeistungGutBesser (zwischen Linux-Systemen)
KonfigurationskomplexitätMäßigEinfach
Windows-UnterstützungEinheimischErfordert zusätzliche Software
ACL-UnterstützungVollEinfach
Verschlüsselter TransportSMB3 unterstütztNFS v4 + Kerberos

Fehlerbehebung

# Samba logs sudo tail -f /var/log/samba/log.smbd # NFS checks showmount -e server-ip rpcinfo -p server-ip nfsstat # Check mounts mount | grep -E "(cifs|nfs)" df -h | grep -E "(cifs|nfs)" # Check ports ss -tlnp | grep -E "(445|139|2049|111)"