smbclient 是 Samba 套件提供的 SMB/CIFS 客戶端工具,支援列舉共享、互動式檔案傳輸、批次指令執行。本文詳細說明四大使用模式與 mount.cifs 掛載替代方案。
smbclient -L 192.168.0.1 -U username%password
# -L: 列舉共享
# -U: 指定用戶名%密碼 (含特殊字符建議用 -U 'user%pass' 避免 shell 轉義)
smbclient //192.168.0.1/tmp -U username%password
# 進入 smb: \> 提示符後可用命令:
# cd, lcd, get, mget, put, mput, ls, dir, mkdir, rm, rmdir, prompt, recurse, binary, ascii, mask, exit
# 列出遠程目錄
smbclient -c "ls" //192.168.0.1/tmp -U username%password
# 建立遠程目錄
smbclient -c "mkdir share1" //192.168.0.1/tmp -U username%password
# 上傳檔案
smbclient -c "put local_file.txt remote_file.txt" //192.168.0.1/tmp -U username%password
# 下載檔案
smbclient -c "get remote_file.txt local_file.txt" //192.168.0.1/tmp -U username%password
若共享為唯讀,執行寫入操作會報 NT_STATUS_ACCESS_DENIED。
適合需要透明存取、應用程式直接讀寫的場景。
# 安裝 cifs-utils
yum install cifs-utils # 或 apt-get install cifs-utils
# 掛載
mount -t cifs //192.168.0.1/tmp /mnt/tmp -o username=administrator,password=123456,vers=3.0,iocharset=utf8
# 開機自動掛載 (/etc/fstab)
//192.168.0.1/tmp /mnt/tmp cifs username=administrator,password=123456,vers=3.0,iocharset=utf8,_netdev 0 0
smbmount //192.168.0.1/tmp /mnt/tmp -o username=administrator
--password-file 或 ~/.smbcredentials (權限 600)vers=3.0 或 vers=2.1,禁用不安全的 vers=1.0seal (SMB 3.0+ 支援)uid=1000,gid=1000,dir_mode=0700,file_mode=0600