Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 查看群組是否包含指定用戶:grep /etc/group 快速檢查法

編輯:關於 Linux

需確認某用戶是否已同步加入某群組(如 dc-group 包含 user1),可直接解析 /etc/group。

單行命令

# 查看群組完整資訊
cat /etc/group | grep dc-group

# 進一步檢查特定用戶
cat /etc/group | grep dc-group | grep user1

輸出解讀

/etc/group 格式:群組名:密碼佔位符:GID:用戶列表(逗號分隔)。
若第二個 grep 有輸出,表示用戶在該群組中;無輸出則不在。

替代工具

  • getent group dc-group:支援 LDAP/NIS 等遠端資料源
  • id user1:顯示用戶所有群組(含主群組 GID)
  • groups user1:僅列出群組名
  • lid -g dc-group (需 libuser):列出群組所有成員

注意事項

  • 用戶新增群組後需重新登入(或 newgrp / sg)生效
  • /etc/group 只反映本地檔案,若使用 SSSD/AD 需用 getent
  • 主群組(/etc/passwd GID)不會出現在 /etc/group 成員列表中

簡單場景用 grep 最快;生產環境建議用 getent 確保資料源一致性。

Copyright © Windows教程網 All Rights Reserved