更改使用者的主要群組 - 在該群組中建立新文件

更改使用者的主要群組 - 在該群組中建立新文件

我正在設定一個環境,​​在我的伺服器上為多個使用者提供 SSH 存取權限。它們都是可信的,但我想將它們限制在檔案系統的一部分。我像這樣創建了用戶:

adduser username -ingroup groupname

效果很好。當我以其中一個的身份登入時,我可以執行此操作並獲得所有正確的答案:

$id -r -u -n
username
$id -r -g -n
groupname

groupname我透過執行以下操作將我自己的使用者帳戶的主要群組切換到:

$usermod myuser groupname

然後我登出並重新登入。

$ls -l / | grep groupname
drwxr-xr-x  3 root     groupname    4.0K 2012-03-26 20:20 groupfiles
$cd /groupfiles
$ls -l 
drwxrwxr-x  2 root groupname    4.0K 2012-03-26 20:32 project

這些權限是根據設計而設計的,群組成員不能更改其下的檔案/資料夾,/groupfiles但可以在其下方新增、編輯和刪除/groupfiles/project

我遇到的問題是,當我這樣做時,我得到了錯誤的群組:

$touch test
$ls -l test
-rw-rw-r-- 1 myuser myuser 0 2012-03-26 20:58 test
$id -r -g -n
groupname

我需要使 vim、touch 等創建的檔案具有正確的群組。我知道newgrp()這些setgid。這些都是不是我在尋找什麼。這對於我創建的新用戶來說效果很好,但對於我的用戶來說不起作用。我不確定發生了什麼,但現在一切正常。我只是把這個問題留給未來的修補者。

答案1

我猜你想要的行為是sudo usermod -g groupname myuser.

注意:此指令會變更 $HOME 中所有檔案的群組擁有權,但不會變更 home 以外的檔案。您可能希望將它們改回來chgrp -R myuser $HOME

然後,您需要登出並重新登入才能使變更生效。

如果您不想變更「myuser」主群組,則可以選擇 newgrp。

sudo usermod -g admin bodhi

ssh localhost

touch file

newgrp bodhi
touch file2

ls -l file file2
-rw-r--r-- 1 bodhi admin 0 2012-03-26 19:28 file
-rw-r--r-- 1 bodhi bodhi 0 2012-03-26 19:29 file2

相關內容