Samba-Server

Siehe auch

Links:

Namen der Dienste:

  • Auf Samba AD DCs: samba oder samba-ad-dc

  • Auf Samba NT4-style PDC/BDCs: smbd, nmbd

  • Auf Samba domain members: smbd, nmbd, winbind

  • Auf Samba standalone hosts: smbd

Installation

Das Samba-Paket unterstützt Samba nur als Domänenmitglied und NT4 PDC oder BDC (und das will niemand wirklich mehr). Red Hat bietet aber keine Pakete für den Betrieb von Samba als AD DC an. Wird ein AD DC benötigt, muss Samba compiliert werden.

Für ein Domänenmitglied genügt daher:

dnf -y install samba
systemctl enable --now smb.service
# supports Network Browsing:
systemctl enable --now nmb.service

Soll Samba den AD DC spielen, muss es compiliert werden - hier auf RHEL 8:

hostnamectl set-hostname ad.example.com

yum -y install epel-release

VER=4.18.5
wget https://download.samba.org/pub/samba/stable/samba-$VER.tar.gz
tar xzvf samba-$VER.tar.gz

cd samba-$VER
cd bootstrap/generated-dists/centos8s
./bootstrap.sh
cd ../../../..

cd samba-$VER
./configure \
    --enable-cups \
    --enable-fhs \
    --libdir=/usr/lib64 \
    --localstatedir=/var \
    --prefix=/usr \
    --with-acl-support \
    --with-ads \
    --with-automount \
    --with-configdir=/etc/samba \
    --with-lockdir=/var/lib/samba \
    --with-logfilebase=/var/log/samba \
    --with-modulesdir=/usr/lib64/samba \
    --with-pam \
    --with-pammodulesdir=/lib64/security \
    --with-piddir=/run/samba \
    --with-privatedir=/etc/samba \
    --with-quotas \
    --with-shared-modules=idmap_rid,idmap_ad,idmap_hash,idmap_adex \
    --with-syslog \
    --with-systemd \
    --with-utmp \
    --with-winbind
make --jobs 4
make install

cat > /etc/systemd/system/samba-ad-dc.service <<EOF
[Unit]
Description=Samba Active Directory Domain Controller
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/sbin/samba --daemon --configfile=/etc/smb.conf
PIDFile=/run/samba/samba.pid
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable samba-ad-dc.service

Domain erstellen

Neue Domain erzeugen:

samba-tool domain provision --use-rfc2307 --interactive
samba-tool user setexpiry --noexpiry Administrator

Backup

Bevorzugt benutzte Backup Methode: Online Backup. Diese erstellt ein komplettes Backup der Domain.

samba-tool domain backup online --targetdir=/backup/ad-dump --server=<DC-server> --user=Administrator

Ein dazu passendes „ad-dump“ Backup Shell-Script:

ad-dump
#!/usr/bin/env bash

HOST=$(hostname --short)
USER='Administrator'
PASSWORD='linuxfabrik'
BACKUP_DIR='/backup/ad-dump'

mkdir -p $BACKUP_DIR

# detect paths
LDBSEARCH=$(which ldbsearch)
SAMBATOOL=$(which samba-tool)

# make sure we can connect to server
$LDBSEARCH --url=ldap://"$HOST" --user=$USER --password=$PASSWORD &> /dev/null
if [ $? -ne 0 ]
then
    echo "Cannot connect to '$USER@$HOST'." >&2
    exit 1
fi

TMP_DIR=$(mktemp -d)
$SAMBATOOL domain backup online --targetdir="$TMP_DIR" --server="$HOST" --username=$USER --password=$PASSWORD &> /dev/null
mv "$TMP_DIR"/samba-backup-*.tar.bz2 $BACKUP_DIR/samba-backup.tar.bz2
rm -rf "$TMP_DIR"

Restore

  1. Samba auf allen DCs stoppen

  2. Backup wiederherstellen (targetdir muss leer sein oder nicht existieren; anschliessend Config-Files anpassen, die den Samba-Dienst starten (samba --configfile /etc/smb.conf))

    samba-tool domain backup restore --backup-file=<tar-file> --newservername=<DC-name> --targetdir=/etc/samba/ --site="New-Site-Name" --host-ip=192.2.0.50
    
  3. Samba starten

Tipp

Wird samba-tool domain backup oder samba-tool domain backup restore nicht angeboten? Dann ist Samba nur als Domänenmitglied konfiguriert. Diese Subcommands sind nur auf einem AD DC verfügbar.

Einfachster Samba-Share ohne Passwort-Authentifizierung

Vollständige Anleitung: Samba-Share ohne Authentifizierung anbieten, beispielsweise für ISO-Files über einen /isos/-Ordner. Achtung: Der in der Konfigurationsdatei gewählte Name der Sektion wird als Share-Name verwendet (hier also [isos]).

Ab Windows 10 1709 kann der Gastzugang in SMB2 und SMB3 standardmässig deaktiviert sein. Kann also sein, dass der Gastzugriff von Windows 10 auf eine Samba-Freigabe möglicherweise nicht funktioniert (weitere Informationen).

mkdir /isos
/etc/samba/smb.conf
[global]
log file = /var/log/samba/%m
log level = 3
map to guest = Bad User
server role = standalone server

[isos]
# This share allows anonymous (guest) access without authentication!
guest ok = yes
guest only = yes
path = /isos
read only = yes
testparm -s
systemctl restart smb

SELinux:

semanage fcontext -a -t samba_share_t '/isos(/.*)?'
restorecon -Fvr /isos/*

Test des Mounts:

smbclient --user guest --no-pass //samba-server/isos

Verbinden mit:

sudo mount --type cifs //samba-server/isos --options sec=none --verbose /mnt/smb

Einfachster Samba-Share mit Passwort-Authentifizierung

Wie oben, aber mit abweichender Konfiguration:

/etc/samba/smb.conf
[global]
log file = /var/log/samba/%m
log level = 3
server role = standalone server

[isos]
# This share requires authentication to access
inherit permissions = yes
path = /isos
read only = yes
unix password sync = yes

Benutzer anlegen:

useradd linus --shell /usr/sbin/nologin
smbpasswd -a linus

Test des Mounts:

smbclient --user linus //samba-server/isos

Verbinden mit:

mount --type cifs //samba-server/isos --options username=linus,password=linuxfabrik --verbose /mnt/smb

Cheat Sheet

Zeigt die verfügbaren Samba-Shares an:

smbclient --list //$SAMBA_SERVER

Zeigt die in Samba genutzten User an:

pdbedit --list --verbose

Built on 2024-04-18