Ubuntu下使用CIFS掛載Windows共享資源

news/2024/7/23 17:08:17 标签: linux, ubuntu, mount, cifs

Ubuntu下使用CIFS掛載Windows共享資源

  • 前言
  • 方法一:mount
  • 方法二:/etc/fstab
  • 設定開機自動掛載
  • 修改權限
  • Troubleshooting
    • Unable to apply new capability set
    • bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount
    • Couldn't chdir to /mnt/: No such file or directory

前言

本篇記錄兩種在Ubuntu下掛載Windows CIFS的方式:mount/etc/fstab

mount_4">方法一:mount

cifs-utils是掛載SMB/CIFS shares所需的套件,使用以下指令安裝:

sudo apt update -y
sudo apt install cifs-utils -y

創建掛載的目標路徑,並實際掛載:

sudo mkdir /mnt/<your_target_dir>
sudo mount -t cifs -o vers=1.0,user=<yourusername>,password=<yourpassword> "//<windows_share_ip_addr>/<your_source_dir>" /mnt/<your_target_dir>

其中//<windows_share_ip_addr>/<your_source_dir>是掛載的源目錄,/mnt/<your_target_dir>是掛載的目標目錄。<yourusername><yourpassword>則是用於登入Windows CIFS share的帳號密碼。

方法二:/etc/fstab

上面的做法是把帳號密碼以明文寫在指令中的,如果想用安全一點的方式,可以參考Mount Windows (CIFS) shares on Linux with credentials in a secure way。

它的做法是把帳號密碼記錄到一個只有root可讀的檔案裡,然後在掛載時到該檔案裡提取所需的帳號密碼。

編輯credential檔案:

sudo vim /root/.smbcred

填上登入Windows CIFS所需的帳號密碼:

username=<yourusername>
password=<yourpassword>

將它改成只有root可讀:

sudo chmod 400 /root/.smbcred

查看權限修改是否成功:

sudo ls -al /root/.smbcred
-r-------- 1 root root 32 Apr 29 17:31 /root/.smbcred

接下來編輯/etc/fstab這個檔案:

sudo vim /etc/fstab

參考/etc/fstab 檔案說明,它的作用如下:

Linux 下有一個配置檔案 /etc/fstab,它的作用是設定硬碟分割區或其化儲存裝置,在開機時掛載點及如何掛載等選項。

在裡面加上如下內容:

//<windows_share_ip_addr>/<your_source_dir> /mnt/<your_target_dir> cifs credentials=/root/.smbcred 0 0

上面這種做法需要sudo權限,並且掛載目錄的擁有者會是root。

如果希望掛載後的目錄的擁有者是一個non-root的使用者(這樣一來對掛載目錄進行存取時就不必加sudo),參考mount share cifs folder without sudo,可以改為下述內容:

//<windows_share_ip_addr>/<your_source_dir> /mnt/<your_target_dir> cifs credentials=/home/<username>/.smbcred,noauto,user 0 0

然後記得要創建/mnt/<your_target_dir>這個目錄:

sudo mkdir /mnt/<your_target_dir>

最後使用以下指令掛載:

mount /mnt/<your_target_dir>

設定開機自動掛載

筆者是在Ubuntu 20.04的docker container裡進行測試的,照著方法二的步驟做了之後,發現docker container重啟後並未自動掛載。

試了CIFS mount through fstab not mounting at boot裡提到的幾個方法後都沒有用,最後是參考HowTo: Remount /etc/fstab Without Reboot in Linux,在/etc/bash.bashrc裡加上:

sudo mount -a

但是這代表每次登入時都還要再輸入一次密碼,為了避免這種麻煩,可以仿照上面的方式,在家目錄建立一個只有root可讀的`.password``檔案:

sudo vim /home/<user_name>/.password

在裡面填入登入密碼,然後將/etc/bash.bashrc裡的sudo mount -a改成:

cat /home/<user_name>/.password | sudo -S mount -a

如此一來,在不同的使用者登入時,都會自動掛載。

上面這種做法需要sudo權限,如果希望一般的使用者也能mount,可以改用:

for dir in $(ls -d -1 "/mnt/"**/)
do
        if ! findmnt $dir > /dev/null; then
                mount $dir
        fi
done

參考What is the noauto mount flag for?,因為在/etc/fstab中,掛載目錄都被加上了noauto這個flag,所以mount -a不會對它們進行自動掛載。上述腳本的邏輯為:尋找/mnt/下的所有目錄,對其中尚未被掛載者,使用mount進行掛載。

修改權限

一個使用場景是把Linux端的檔案備份到Windows share上。使用rsynccp把檔案複製到Windows share上之後,嘗試登入Windows share查看,會發現只有掛載的用戶有權寫入及刪除。如果希望所有人都能對檔案進行修改,則需修改檔案的權限:

sudo chmod -R 777 /mnt/<your_target_dir>/

Troubleshooting

Unable to apply new capability set

如果在使用mount指令時碰到如下錯誤:

Unable to apply new capability set.

參考docker&&samba|“Unable to apply new capability set”问题,在docker run後面加上:

--privileged=true

給予container最高權限,即可解決。

cifs_you_might_need_a_sbinmount_124">bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount

如果在使用mount指令時碰到如下錯誤:

mount: /mnt/<your_target_dir>: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.

表示缺少掛載CIFS必要的套件。使用如下指令安裝:

sudo apt update -y
sudo apt install cifs-utils -y

Couldn’t chdir to /mnt/<your_target_dir>: No such file or directory

如果在使用mount指令時碰到如下錯誤:

Couldn't chdir to /mnt/<your_target_dir>: No such file or directory

表示掛載的目標目錄不存在。在使用mount指令之前,需要先手動創建掛載的目標目錄:

mkdir /mnt/<your_taget_dir>

http://www.niftyadmin.cn/n/778428.html

相关文章

Redmine plugin redmine_2fa安裝

Redmine plugin redmine_2fa安裝 前言安裝步驟安裝redis安裝redmine_sidekiq安裝redmine_bots安裝redmine_2faTroubleshootingNoMethodError (undefined method tr for nil:NilClass使用方式重設2fa otp secret key)前言 redmine_2fa是redmine的一個plugin&#xff0c;提供了登…

將Redmine架在sub-URI

將Redmine架在sub-URI參考HowTo Install Redmine in a sub-URI及Defect #32318&#xff0c;在config/environment.rb檔案的最底下加上&#xff1a; ActionController::Base.relative_url_root RedmineApp::Application.routes.default_scope Redmine::Utils::relative_url_ro…

Windows 10下安裝OpenSSL 1.1.1

Windows 10下安裝OpenSSL 1.1.1前言透過installer安裝OpenSSL在Visual Studio 2019中使用範例程式使用命令直接加解密參考連結前言 OpenSSL官網為OpenSSL&#xff0c;可以從上面下載openssl-1.1.1k.tar.gz後按照NOTES-WINDOWS.md的說明手動編譯安裝。但是看起來OpenSSL的依賴並…

PCL - MLS代碼研讀(一)- MLS測試

PCL - MLS代碼研讀&#xff08;一&#xff09;- MLS測試前言測試曲面重建測試SAMPLE_LOCAL_PLANE上採樣方法測試VOXEL_GRID_DILATION上採樣方法主程序MLS module結構前言 PCL的MLS模塊用於對點雲做平滑處理&#xff08;或說曲面重建&#xff09;及上採樣&#xff0c;其中MLS的…

PCL - MLS代碼研讀(二)- MLSResult

PCL - MLS代碼研讀&#xff08;二&#xff09;- MLSResult前言pcl::MLSResultProjectionMethodPolynomialPartialDerivativeMLSProjectionResults成員變量computeMLSWeight成員函數兩個constructor坐標計算函數偏微分計算函數曲率計算函數各式投影函數擬合曲面計算函數前言 PC…

PCL - MLS代碼研讀(三)- 坐標計算函數

PCL - MLS代碼研讀&#xff08;三&#xff09;- 坐標計算函數前言三維getMLSCoordinates二維getMLSCoordinatesgetPolynomialValue前言 本篇延續PCL - MLS代碼研讀&#xff08;二&#xff09;&#xff0c;繼續介紹坐標計算函數。 三維getMLSCoordinates 此函數計算全局坐標系…

PCL - MLS代碼研讀(四)- 偏微分計算函數

PCL - MLS代碼研讀&#xff08;四&#xff09;- 偏微分計算函數前言getPolynomialPartialDerivative前言 本篇延續PCL - MLS代碼研讀&#xff08;二&#xff09;&#xff0c;繼續介紹偏微分計算函數。 getPolynomialPartialDerivative 還記得在PCL - MLS代碼研讀&#xff08…

PCL - MLS代碼研讀(五)- 曲率計算函數

PCL - MLS代碼研讀&#xff08;五&#xff09;- 曲率計算函數前言calculatePrincipalCurvatures參考連結前言 本篇延續PCL - MLS代碼研讀&#xff08;二&#xff09;&#xff0c;繼續介紹曲率計算函數。 calculatePrincipalCurvatures Eigen::Vector2f pcl::MLSResult::calc…