Samba / NFS ファイル共有
Samba は Windows システムとのファイル共有 (SMB/CIFS プロトコル) に使用され、NFS は Linux システム間のファイル共有に使用されます。
Samba ファイル共有
Sambaをインストールする
sudo apt update
sudo apt install samba samba-common-bin -y
# Check version
smbd --version
# Check service status
sudo systemctl status smbd nmbd基本構成
Samba 構成ファイルは /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パブリック共有 (認証なし)
# /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認証された共有
[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検証して開始する
# 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クライアント接続
Linuxクライアント
# 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 -aWindowsクライアント
ファイル エクスプローラーで、アドレス バーに次のように入力します。
\\server-ip\sharedネットワークドライブをマッピングするには:
- 「このPC」を右クリック→「ネットワークドライブの割り当て」
- 「
\\server-ip\shared」と入力します - 「別の認証情報を使用して接続する」にチェックを入れます
NFS ファイル共有
NFSサーバーをインストールする
sudo apt update
sudo apt install nfs-kernel-server -y
# Check status
sudo systemctl status nfs-serverエクスポートされたディレクトリの構成
/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)共通オプション
| オプション | 説明 |
|---|---|
rw | 読み取り/書き込みアクセス |
ro | 読み取り専用アクセス |
sync | 同期書き込み (より安全) |
async | 非同期書き込み (高速ですがリスクが高くなります) |
no_subtree_check | サブツリーチェックを無効にする (パフォーマンスを向上させる) |
no_root_squash | クライアントの root 権限を保持する |
root_squash | ルートを誰にもマップしない (デフォルト) |
all_squash | すべてのユーザーを誰にもマッピングしない |
anonuid/anongid | 匿名ユーザーの UID/GID を指定する |
共有を作成して開始する
# 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 # rpcbindNFSクライアント
# 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 -aautofs による自動マウント
# 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/dataNFS パフォーマンスのチューニング
# 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/nfsSamba と NFS の比較
| 特徴 | サンバ (SMB/CIFS) | NFS |
|---|---|---|
| プロトコル | SMB/CIFS | NFS v3/v4 |
| ユースケース | Windows と Linux の混合 | Linux/Unix システム間 |
| 認証 | 個別のパスワードデータベース | UID/GID ベースまたは Kerberos |
| パフォーマンス | 良い | より良い (Linux システム間) |
| 構成の複雑さ | 中等度 | シンプル |
| Windowsのサポート | ネイティブ | 追加のソフトウェアが必要です |
| ACLのサポート | フル | 基本 |
| 暗号化されたトランスポート | SMB3対応 | NFS v4 + Kerberos |
トラブルシューティング
# 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)"