linux 用户无需重登录或者newgrp,使新加组权限自动生效

维护完群组后,无法在Linux桌面生效。支持人员频繁接到这样的case,只能建议ta:

  • run ‘newgrp newly-added-group-name’, or
  • logout the vnc and login again.

在遵循Linux桌面设计范式的前提下,本文我们尽量做到让用户无感地让新加群组生效。

环境说明

CentOS 6.10/7, Gnome/xfce4, Krusader

定制完毕后,

  • 新开terminal,新群组就生效了。无需用户做任何操作。
  • 在新开terminal里打开krusader,可以浏览与编辑文件

新terminal生效新群组

用户只需要运行一次本脚本即可。也可以管理员将~/.flush_groups.csh文件放至NFS,用户家目录.cshrc文件去source即可。

#!/bin/bash


#################
# Update .cshrc.
CSHRC=~/.cshrc
if [ ! -f "$CSHRC" ]; then
    touch "$CSHRC"
fi
# The line to add if it doesn't exist
LINE="source ~/.flush_groups.csh"
if ! grep -Fxq "$LINE" "$CSHRC"; then
    # If the line doesn't exist, add it to the file
    echo "$LINE" >> "$CSHRC"
fi

# Add ~/.flush_groups.csh
cat > ~/.flush_groups.csh << 'EOF'
#!/bin/csh

# Writen by wanlinwang.
# Date: Dec 1, 2023
# Description: 支持自动将新增群组刷新到新terminal中,对研发人员透明。提升研发人员效率,降低支持工作量。

if ( ! $?new_grp_array) then

    # 获取当前用户在当前session的主群组以及全部群组
    setenv primary_group  `id -gn`
    set    current_groups=`sh -c 'id -Gn 2> /dev/null'`
    
    # 获取当前用户最新的全部群组
    set login_groups=`sh -c 'id -Gn $USER 2> /dev/null'`
    
    # 创建临时文件,并将群组信息写入临时文件
    set temp1=`mktemp`
    set temp2=`mktemp`
    echo $current_groups | tr ' ' '\n' | sort > $temp1
    echo $login_groups | tr ' ' '\n' | sort > $temp2
    
    # 使用comm比较文件内容
    #set new_group=`comm -13 $temp1 $temp2`
    setenv new_grp_array `comm -13 $temp1 $temp2 | tr '\n' ' '`
    
    # 删除临时文件
    rm -f $temp1 $temp2
endif

# iterate new_grp_array
if ("$new_grp_array" != "") then
    set new_group=`echo $new_grp_array | awk '{print $1}'`
    setenv new_grp_array `echo $new_grp_array | awk '{$1=""; print $0}' | sed 's/^[ \t]*//'`

    if ("$new_group" != "") then
        # 切换群组,使其生效
        exec newgrp $new_group
    endif
else
    set current_group=`id -gn`
    if ( $?primary_group ) then
        if ( "$primary_group" != "$current_group" ) then
            exec newgrp $primary_group
        endif
    endif
    unset current_group
    unsetenv primary_group
endif

unsetenv new_grp_array
EOF













#################
# Update .bashrc.
BASHRC=~/.bashrc
if [ ! -f "$BASHRC" ]; then
    touch "$BASHRC"
fi
# The line to add if it doesn't exist
LINE="source ~/.flush_groups.sh"
if ! grep -Fxq "$LINE" "$BASHRC"; then
    # If the line doesn't exist, add it to the file
    echo "$LINE" >> "$BASHRC"
fi

# Add ~/.flush_groups.sh
cat > ~/.flush_groups.sh << 'EOF'
# Written by wanlinwang.
# Date: Dec 6, 2023
# Description: Automates the process of refreshing newly added groups in new terminal sessions.

# Check if new groups array exists
if [ -z "$new_grp_array" ]; then

    # Retrieve the current user's primary group and all groups in the current session
    primary_group=$(id -gn)
    current_groups=$(id -Gn 2>/dev/null)

    # Get the current user's latest complete group list
    login_groups=$(id -Gn $USER 2>/dev/null)
    
    # Create temporary files and write group information into them
    temp1=$(mktemp)
    temp2=$(mktemp)
    echo $current_groups | tr ' ' '\n' | sort > "$temp1"
    echo $login_groups | tr ' ' '\n' | sort > "$temp2"
    
    # Compare file contents using 'comm'
    new_grp_array=$(comm -13 "$temp1" "$temp2" | tr '\n' ' ')
    
    # Delete temporary files
    rm -f "$temp1" "$temp2"
fi

# Iterate over new_grp_array
if [ ! -z "$new_grp_array" ]; then
    new_group=$(echo $new_grp_array | awk '{print $1}')
    new_grp_array=$(echo $new_grp_array | awk '{$1=""; print $0}' | sed 's/^[ \t]*//')

    if [ ! -z "$new_group" ]; then
        # Switch to the new group to activate it
        exec newgrp $new_group
    fi
else
    current_group=$(id -gn)
    if [ -n "$primary_group" ]; then
        if [ "$primary_group" != "$current_group" ]; then
            exec newgrp $primary_group
        fi
    fi
    unset current_group
    unset primary_group
fi

unset new_grp_array
EOF

krusader

按需,需要联网。如果离线环境则手动下载回来安装。

# Install krusader, a file manager. If you want to use this tool, please contact your system administrator to install.
sudo yum install epel-release
sudo yum localinstall http://rpms.plnet.rs/plnet-centos6-x86_64/RPMS.plnet-compiled/krusader-2.3.0-0.1.beta1.el6.x86_64.rpm

 详情查看:已开启的gnome桌面如何进入群组为新加群组且权限为770的文件夹 | IC InfraA simple, whitespace theme for academics. Based on [*folio](https://github.com/bogoli/-folio) design.icon-default.png?t=N7T8https://www.icinfra.cn/blog/2023/how-to-enable-newly-added-group-on-existing-gnome-desktop/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值