![](/img/trans.png)
[英]How to ensure that gid in /etc/passwd also exist in /etc/group
[英]Is there a way to only edit the GID on /etc/passwd file using chef?
我們可以使用 Chef 資源在 /etc/passwd 文件中將 GID 從 101 更改為 100(或任何其他數字)嗎? libuuid:x:100:101::/var/lib/libuuid:
對於 /etc/passwd 中所有具有 101 的 GID,如果需要更改 GID,使用 Chef 資源的方法是什么?
在為此提出解決方案之前,我想指出兩件事:
/etc/passwd
文件,因為它可能會導致問題。 如果您仍想使用 Chef,您可以在ruby_block
資源中使用 Ruby 代碼。
但是,處理此問題的最簡潔方法是(單獨)識別用戶並使用用戶資源。
例子:
# This will set gid as 1001 for user1, user2, user3
%w(
user1
user2
user3
).each do |u|
user u do
# add any other properties as required
gid '1001'
end
end
更新:
包含以下內容的示例文件/tmp/userlist
:
john:x:100:101::/bin/bash:
david:x:207:100::/bin/bash:
joe:x:100:101::/bin/nologin:
mike:x:101:100::/bin/bash:
rich:x:103:207::/bin/bash:
fred:x:105:111::/bin/nologin:
不是 Ruby 的專家,但它就在這里。 以下ruby_block
將從文件中讀取行,並創建一個新文件,替換 GID 101 的行:
ruby_block "Write file" do
block do
puts
userlist = File.readlines('/tmp/userlist')
fp = File.open('/tmp/newuserlist', mode='w')
userlist.each do |line|
gid = line.split(':')[3]
if gid == '101'
fp.write line.sub(/.*\K101/, '1001')
else
fp.write line
end
end
fp.close
end
end
注意:使用 Ruby 或其他語言甚至 Shell 腳本可能有一種更清潔、更簡單的方法。 請考慮相同。
利用user
資源
user 'a user' do
comment 'A random user'
uid 1234
gid 'groupname'
home '/home/random'
shell '/bin/bash'
password '$1$JJsvHslasdfjVEroftprNn4JHtDi'
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.