Skip to content

Change primary group

Generally, HPCC users need to request root privilege through the ticket system to change their primary group permanently. However, a user can also use newgrp command to change their primary group temporarily to any one of their groups.

Primary Group Change on Command Line

To change a user's primary group with a command line, simply run

1
[username@dev-intel18 CurrentPath]$ newgrp <Group Name>

Note that <Group Name> has to be one of the user's group names. To find out all group names, please run groups command:

1
2
[username@dev-intel18 CurrentPath]$ groups
chemistry VMD g09 BiCEP education-data ParaView

Here, the primary group is "chemistry", the first group name in the results. After the execution of newgrp command, a new shell session is created and the current environment, including the current working directory remains the same.

1
2
3
[username@dev-intel18 CurrentPath]$ newgrp g09
[username@dev-intel18 CurrentPath]$ groups
g09 chemistry VMD BiCEP education-data ParaView

Note that the primary group is changed to g09. To leave the session, just run "exit" command. It will go back to the previous session with the original primary group (chemistry).

If a user would like to start the shell session with a new primary group as though the user just logged in, the optional "-" flag can be used

1
2
3
[username@dev-intel18 CurrentPath]$ newgrp - g09
[username@dev-intel18 ~]$ groups
g09 chemistry VMD BiCEP education-data ParaView

Using the "-" flage causes the user's environment is reinitialized and the shell session starts from his home directory.

Some users might have the issue to lose the header in front of prompt after executing the command:

1
2
3
[username@dev-intel14-phi CurrentPath]$ newgrp g09
bash-4.2$ groups
g09 chemistry VMD BiCEP education-data ParaView

Using source the default bashrc file to get it back.

1
2
bash-4.2$ source /etc/bashrc
[changc81@dev-intel14-phi MyCurrentPath]$ 

Note that, if a job is submitted in the session of a new primary group, the environment with the new primary group will be used in the job running by default instead of the original one. For more information, please check the row of --export in the table of List of Job Specifications page.

Primary Group Change in Job Script

Many HPCC users have more than one research spaces. In a job running, the user might need his primary group set to be the group of the research space where the output files are. In order to do this, the job script can use newgrp from the beginning of the command lines. For example, an original job script may look like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash --login

#SBATCH --time=00:10:00             # limit of wall clock time - how long the job will run (same as -t)
#SBATCH --ntasks=5                  # number of tasks - how many tasks (nodes) that you require (same as -n)
#SBATCH --mem-per-cpu=2G            # memory required per allocated CPU (or core) - amount of memory (in bytes)

module purge
module load GCC/6.4.0-2.28 OpenMPI  ### load necessary modules, e.g. 

srun -n 5 <executable>              ### call your executable (similar to mpirun)

scontrol show job $SLURM_JOB_ID     ### write job information to output file

If the job script is submitted in a research space, adding a few lines can make the primary group the same as the group of the research space:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/bash --login

#SBATCH --time=00:10:00             # limit of wall clock time - how long the job will run (same as -t)
#SBATCH --ntasks=5                  # number of tasks - how many tasks (nodes) that you require (same as -n)
#SBATCH --mem-per-cpu=2G            # memory required per allocated CPU (or core) - amount of memory (in bytes)

GroupName=$(readlink -f $PWD|awk -F "/" '{ if ($2=="mnt" && $3=="research" && $4!="") system("ls -ld /mnt/research/"$4"|cut -d \" \" -f 4") }')
newgrp ${GroupName} << EOS

module purge
module load GCC/6.4.0-2.28 OpenMPI  ### load necessary modules, e.g. 

srun -n 5 <executable>              ### call your executable (similar to mpirun)

scontrol show job $SLURM_JOB_ID     ### write job information to output file
EOS

The group name of the research space for job running is automatically determined in line 7. You can also directly set the variable "GroupName" to be the group name of your research space in line 7 if you are sure about jobs running in the research space:

1
GroupName=<group name of your research space>

After the group name of the research space is given, the command lines between line 9 and 16 (inside the two "EOS") will be run under the environment of the new primary group (${GroupName}).

Automatic Primary Group Change after Login

If users need to change their primary group constantly, they can use the powertools script PrimaryGroup. Users can add a command line "source /opt/software/powertools/doc/PrimaryGroup " in the beginning of the file \~/.bashrc:

~/.bashrc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/bash

source /opt/software/powertools/doc/PrimaryGroup <Group Name>          # Add this line to change primary group automatically

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

...
...

where <Group Name> is one of his group names. This also provides a flexibility that user's primary group can be changed to any of his groups at any time without requesting root privilege.

Note that, the powertools script PrimaryGroup only works on dev nodes. It does not work on gateway or compute nodes. Users need to ssh to a dev node to see the effect. After this command line is added in the ~/.bashrc file, the command newgrp with the optional "-" flag will not work. Users can still use newgrp without "-" flag.