[精讚] [會員登入]
398

[Centos] Linux mount 掛載檔案時的使用者和SELINUX和權限處理

linux下使用mount來掛載目錄或磁碟,我們一般會使用這樣的指令 # mount /dev/sdc1 /home/h

分享此文連結 //n.sfs.tw/16421

分享連結 [Centos] Linux mount 掛載檔案時的使用者和SELINUX和權限處理 @新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2024-06-08 00:28:09 最後編修
2024-06-04 00:35:23 By 張○○
 

自動目錄

linux下使用mount來掛載目錄或磁碟,我們一般會使用這樣的指令

# mount /dev/sdc1 /home/hdd

意思是將磁碟分割 /dev/sdc1 指定掛載到 /home/hdd 這個目錄,簡單的操作可以參考[2]。

掛載完畢後,切換到 /home/hdd 這個目錄,其餘他是使用了另一個磁區 /dev/sdc1,而且他是屬於 root 的

如果這個磁區的檔案類型和你的系統一樣的話(例如 ext3, ext4, xfs),你可以使用 chmod, chcon, chgrp, chown 等指令去修改權限沒有任何問題。

但假設你掛載的檔案類型不是linux的類型的話,上面修改權限的指令將不會有任何作用,假設 /home/hdd 不是linux常見的檔案類型,而是例如常見的 cifs、nfs 類型。

# chown -R user:user /home/hdd
# chmod 777 /home/hdd

<結果是能跑完不會有任何錯誤,但是結果是完全沒有用>

換句話說,就是使用者和群組並不會改變成為 user

此外,Centos上還有 selinux的機制,要是沒被關掉的話,可能還會有selinux導致權限的問題。

 

使用 -o參數來指定權限及context

使用 mount 指令時,可以加上 -o 來指定 selinux 的 context,例如:

# mount /dev/sda2 /test/ -o defcontext="system_u:object_r:httpd_sys_rw_content_t:s0"

上面的指令中defcontext指定沒有selimux標籤的檔案會給予預設的selinux權限[4],原則上對於非linux相關的檔案類型的話,也可以用 context=,兩者是等義。

mount只是暫時的,在重開機後就消失,希望能永久掛載的話,可以修改 /etc/fstab

 

修改 /etc/fstab 來解決掛載後的權限問題

假設我要把 nas 掛載後指令給 apache (uid=48),同時也要指定selinux 的context,另外要掛載後能讀寫,並且目錄具有 讀寫的權限

修改 /etc/fstab,加入以下內容:

//192.168.1.20/home /path/to/uploads cifs username=user,password=123456,rw,uid=48,context="system_u:object_r:httpd_sys_rw_content_t:s0" 0 0

上面把nas(192.168.1.20)掛載上 /path/to/uploads 的目錄,nas的檔案型態是cifs。登入nas的帳密就寫在後面

uid=48 指定目錄的擁有者是 apache, 適應給selinux的內容作為apache可讀寫。

這裡要注意的是 rw 是指掛載完後可以讀寫這個目錄,如果沒有指定uid,預設會是 root(uid=0),如此這個目錄只有root可以讀寫。你也可以把他指定成777,像這樣[5]

//192.168.1.20/home /path/to/uploads cifs username=user,password=123456,rw,context="system_u:object_r:httpd_sys_rw_content_t:s0",dir_mode=0777,file_mode=0666 0 0

   ├── dir_mode=0777 目錄可讀寫,要注意這和 dmask 不一樣,dmask在cifs中是無效的
   └── file_mode=0666 檔案可讀寫

 

錯誤排除

沒有檔案 mount.cifs 的話,請安裝 mount.cifs檔案:

# dnf install cifs-utils

 

參考資料

[1] [Oracle9] 查看磁碟檔案類型、掛載和uuid@新精讚

[2] [Centos6 7] 掛載磁碟@新精讚

[3] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security-enhanced_linux/sect-security-enhanced_linux-mounting_file_systems-making_context_mounts_persistent

[4] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-security-enhanced_linux-working_with_selinux-mounting_file_systems

[5] https://superuser.com/questions/174776/modify-fstab-entry-so-all-users-can-read-and-write-to-an-ext4-volume

END

你可能感興趣的文章

[Centos7] 安裝 semanage (selinux工具程式) 安裝selinux的工具程式 semanage

[Rocky9] codeignitor4+ nginx+ php8.1-fpm + docker compose 這次的目標是把服務裝在docker,裡面放入 nginx 和 php8.1-fpm的 docker container, 並採用codeignitor4。

[CENTOS7] 使用 logrotate 來整理wildfly日誌檔 解決 wildfly 的日誌檔不斷長大的問題

[CENTOS7 8] 查看記憶體 linux 中查看記憶體狀態指令整理

[Linux] mongodb 初步--2/3 我使用mongo 的一些大小事:叢集

[Rocky9] 安裝SPHINX Search 3 支援中文 新版本的 sphinx 和舊版不同,網路上很多範例和教學是不能用的。此文是安裝和設定方法分享

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

SELinux 常用指令和檔案 在Redhat系列中,Centos5以後加入了selinux,他並沒有這麼可怕,不必每次看到Selinux ,就想把他

[札記] 2016.7~12月札記 札記,只是札記

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

APACHE的記錄檔格式 LogFormat 語法 在APACHE中有定義一些記錄的語法模版 在 /etc/httpd/conf/httpd.conf 中: LogForm

[CodeIgniter 3] 資料庫的使用方法整理2/2 CI3 承襲 CI2,有很多的builder class可以用,依各人的使用習慣,有人喜歡一堆sql字串,有人喜歡用helper