متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 109Network · Storage

How to mount a Windows or Samba share

Many companies keep files on a Windows file server or a NAS: scanned invoices, ERP exports, reports. A Linux server can mount that share (SMB/CIFS) like a local folder — for an import job, a backup, or a web app. This how-to mounts it with a protected credentials file, the right owner and SMB version, makes it permanent in fstab, and fixes the classic mount errors.

  • 5 الدروس
  • 12 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

109.1

The quick answer

dnf -y install cifs-utils
install -m 600 /dev/null /root/.smb-fs1
printf 'username=svc-linux\npassword=%s\ndomain=CORP\n' '<password>' > /root/.smb-fs1
mkdir -p /mnt/invoices
mount -t cifs //fs1.corp.example.com/Invoices /mnt/invoices -o credentials=/root/.smb-fs1,uid=app,gid=app,vers=3.1.1,seal
ls /mnt/invoices | head

On Ubuntu: apt-get install -y cifs-utils.

109.2

Step by step

  1. Use a service account with only the rights the job needs (read-only if it only reads) — not your own Windows login.
  2. Keep the password in a credentials file with 600 rights (quick answer), never in fstab or on the command line.
  3. The mount options:
OptionMeans
credentials=FILEUser, password, domain from the file.
uid=app,gid=appFiles appear owned by this Linux user (SMB has no Linux owners).
file_mode=0640,dir_mode=0750Linux rights shown for files and folders.
vers=3.1.1SMB protocol version (3.x; never SMB1).
sealEncrypt the traffic (SMB 3).
roRead-only mount.
  1. Mount at boot:
/etc/fstab (line)
//fs1.corp.example.com/Invoices  /mnt/invoices  cifs  credentials=/root/.smb-fs1,uid=app,gid=app,file_mode=0640,dir_mode=0750,vers=3.1.1,seal,_netdev,nofail  0 0
systemctl daemon-reload
mount -a
  1. SELinux — for a web server or service that reads the share, set a label at mount time:
option to add
context=system_u:object_r:httpd_sys_content_t:s0

(Or turn on the boolean that fits, for example setsebool -P httpd_use_cifs on.)

  1. Find the shares a server offers (needs samba-client):
smbclient -L //fs1.corp.example.com -U 'CORP\svc-linux'

109.3

Check that it worked

findmnt /mnt/invoices
sudo -u app ls /mnt/invoices | head -3

The share is mounted, the app user can read (and write, if allowed), and after a reboot it is mounted again.

109.4

Common problems

ProblemCauseFix
mount error(13): Permission deniedWrong user/password/domain, or no share rights.Check the credentials file (no spaces, right domain); rights on the Windows side.
mount error(95): Operation not supportedSMB version not supported by the server.Try vers=3.0 (never 1.0).
mount error(112): Host is downPort 445 blocked or wrong name.nc -zv fs1.corp.example.com 445; DNS.
mount error(2): No such file or directoryShare name wrong.smbclient -L to list the share names.
Everything is owned by rootNo uid=/gid= options.Add uid=, gid=, file_mode, dir_mode.
Boot hangs on the mountNo _netdev/nofail.Add both (or use a systemd automount: x-systemd.automount).

الأوامر في هذا الدرس

109.5

Go further

1Why use a credentials file for a CIFS mount?

The password stays out of fstab, the command line and ps; the file has 600 rights.

2Why are all files owned by root after the mount, and how do you change it?

SMB has no Linux owners; set uid=, gid= (and modes) in the mount options.

كل الأوامر في هذا الفصل