linuxbashsudousergroups

Unable to add user to group with usermod


I needed to create a group named support to manage access to some scripts that should be run as sudo, so I created a group and verified that the group existed in /etc/group:

# groupadd support
# cat /etc/group | grep support
support:x:1002:

Then, I wanted to add user1 to the group:

# usermod –a –G support user1; echo $?
Usage: usermod [options] LOGIN
...
<usermod help page>
...
2

The command returned code 2 and no error message occured. I thought that the problem could be with group support so I tried to add user1 to group sudo (just for testing) and the problem persisted. What am I doing wrong?

OS: Kubuntu 20.04 LTS (5.4.0-58-generic)

BASH: GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)


Solution

  • Your '-' character is incorrect. Maybe you've copy and paste it from somewhere or you're using a non-standard keyboard. Take a look at your command hex-dump:

    echo 'usermod –a –G support user1' | hd
    00000000  75 73 65 72 6d 6f 64 20  e2 80 93 61 20 e2 80 93  |usermod ...a ...|
    00000010  47 20 73 75 70 70 6f 72  74 20 75 73 65 72 31 0a  |G support user1.|
    00000020
    

    But the correct one is:

    echo 'usermod -a -G support user1' | hd
    00000000  75 73 65 72 6d 6f 64 20  2d 61 20 2d 47 20 73 75  |usermod -a -G su|
    00000010  70 70 6f 72 74 20 75 73  65 72 31 0a              |pport user1.|
    0000001c
    

    Notice the - character in the second hex and compare it with your