Would you like to react to this message? Create an account in a few clicks or log in to continue.

3 posters
  • Post new topic
  • Reply to topic

For those who wish to set user IDs. FINAL SCRIPT see Post #7

avatar
davecs


Posts : 110
Join date : 2024-05-14

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by davecs 14th June 2024, 4:03 pm

On my computer, my stuff is shared between distros and I always use UID 1000. My wife's UID is 1002 and the guest UID is 1005. Don't ask where I picked these from, I just did! Once upon a time ....

This means that when a new system is installed, I want to use the same IDs. When installing PCLOS Debian, the user ID at the time of installation is 1001, and GID 1004. Despite that fact that is starts there, I've noticed that UID and GID of 1000 are not used. I've come across a few problems due to using adduser and useradd, but I think I've found my workaround. I've written a script called "newuser" as follows.

Usage: newuser UID username

If UID is 1000, it creates a private GID, and puts the user in the sudo group.
If UID is anything else, if does not create a GID and puts the user in users, bluetooth and netdev groups.
In both cases, it prompts for a password.

I'm not that clever at writing foolproof scripts, so if anyone can add a test to only allow the UID in range, and a valid username, please tell me how and I'll add it. Anyway this works for me:

Code:

#!/bin/bash
die(){
   local m="$1"  # the first arg
   local e=$2    # the second arg
   echo "$m"
   exit $e
}

# if not right number of arguments displayed, display an error and die
[ $# != 2 ] && die "Usage: $0 UID Username" 1

# Rest of script goes here
if [[ $1 -eq 1000 ]]
then
    groupadd -g $1 $2
    useradd -u $1 -m -g $1 -G sudo -s /bin/bash $2
    echo New admin user created, UID $1 Username $2
else
    useradd -u $1 -m -G users,netdev,bluetooth -N -s /bin/bash $2
    echo New regular user created, UID $1 Username $2
fi

passwd $2


Are there other groups that I ought to put users in?


Last edited by davecs on 15th June 2024, 7:11 pm; edited 2 times in total
nobody
nobody


Posts : 48
Join date : 2024-05-12
Location : Finland

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty Re: For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by nobody 14th June 2024, 10:10 pm

In antiX default groups are

Code:
lp dialout cdrom floppy sudo audio dip video plugdev users netdev lpadmin scanner
avatar
davecs


Posts : 110
Join date : 2024-05-14

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty Re: For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by davecs 15th June 2024, 7:26 am

nobody wrote:In antiX default groups are

Code:
lp dialout cdrom floppy sudo audio dip video plugdev users netdev lpadmin scanner

Strange. I wouldn't use sudo nor lpadmin for non root users.

Does the group "users" include access to other groups (i.e., is there a need for "lp" if you're in "users")?

Now you've mentioned antiX, I'm going to take a look at the related MX Linux to see how that works. I've got that already installed on one partition so shouldn't be a problem.
Upgreyed
Upgreyed
Admin


Posts : 129
Join date : 2024-05-12
Location : The Sunshine State FL USA

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty Re: For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by Upgreyed 15th June 2024, 8:24 am

The live-installer default for the user in groups but more can be added if needed from here:
         
Code:
# Add user to additional groups
            for group in config.get("additional_user_groups", [
                                    "audio", "video", "netdev"]):
                self.run("chroot||usermod -aG {} {}".format(group, self.setup.username), False)
avatar
davecs


Posts : 110
Join date : 2024-05-14

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty Re: For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by davecs 15th June 2024, 9:48 am

OK I tested whether a non-sudo user could print if in lp group but not in lpadmin. And of course they could. I've also added "bluetooth" to the list, and I wonder whether "pipewire" would be needed at some point as that replaces pulseaudio. And maybe "games". So I've got:

Code:

lp dialout cdrom floppy audio dip video plugdev users netdev bluetooth scanner pipewire

And for root users, add:
Code:

sudo lpadmin

So the script becomes:

Code:


#!/bin/bash
die(){
   local m="$1"  # the first arg
   local e=$2    # the second arg
   echo "$m"
   exit $e
}

# if not right number of arguments displayed, display an error and die
[ $# != 2 ] && die "Usage: $0 UID Username" 1

# Rest of script goes here
if [[ $1 -eq 1000 ]]
then
    groupadd -g $1 $2
    useradd -u $1 -m -g $1 -G sudo,users,lp,lpadmin,dialout,cdrom,floppy,audio,dip,video,plugdev,netdev,bluetooth,scanner,pipewire -s /bin/bash $2
    echo New admin user created, UID $1 Username $2
else
    useradd -u $1 -m -G users,lp,dialout,cdrom,floppy,audio,dip,video,plugdev,netdev,bluetooth,scanner,pipewire -N -s /bin/bash $2
    echo New regular user created, UID $1 Username $2
fi

passwd $2

Now it remains to add a filter to the script to trap invalid values. Even better, to trap numbers/usernames in use already.

avatar
davecs


Posts : 110
Join date : 2024-05-14

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty Re: For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by davecs 15th June 2024, 9:49 am

Upgreyed wrote:The live-installer default for the user in groups but more can be added if needed from here:
         
Code:
# Add user to additional groups
            for group in config.get("additional_user_groups", [
                                    "audio", "video", "netdev"]):
                self.run("chroot||usermod -aG {} {}".format(group, self.setup.username), False)

That's well beyond my comprehension I'm afraid.
avatar
davecs


Posts : 110
Join date : 2024-05-14

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty DONE: My script to add users on my terms!!

Post by davecs 15th June 2024, 7:10 pm

I think I've done it!

The following script will:

Accept two arguments. The first must be a user ID number from 1000-9999, the second must be a valid username.

The first argument is tested to ensure that it is in range. Then it is tested to ensure that the UID isn't already in use.

The second argument is tested to ensure its suitability as a username. Then it is tested to ensure that it's not already in use.

If all those tests are passed, then the UID of 1000 is set up as the admin user, with a private group GID 1000, other UIDs are set up as normal users, and added to group "users".

Finally, the script prompts for a password for the new user.

Of course it can be worked to add different options, but this is what I want!

Code:

#!/bin/bash
die(){
 local m="$1"  # the first arg
 local e=$2    # the second arg
 echo "$m"
 exit $e
}

# if not two args displayed, display an error and die
[ $# != 2 ] && die "Usage: $0 UID Username" 1

#see if number is in range
if (( $1 >= 1000 && $1 <= 9999 )) ; then
    :
else die "UID No. out of range" 2
fi

#check username is valid
pattern="^[a-z][-a-z0-9]*\$"
if [[ $2 =~ $pattern ]] ; then
    :
else die "Invalid User Name" 3
fi

# see if uid already exists
record=`cat /etc/passwd | grep $1`
check=$(echo $record | cut -d ':' -f 3)
if [[ $check ]]
then die "UID No. already in use" 4
fi

# see if username already exists
record=`cat /etc/passwd | grep $2`
check=$(echo $record | cut -d ':' -f 1)
if [[ $check ]]
then die "username already in use" 5
fi

# Rest of script goes here
if [[ $1 -eq 1000 ]]
then
    groupadd -g $1 $2
    useradd -u $1 -m -g $1 -G sudo,users,lp,lpadmin,dialout,cdrom,floppy,audio,dip,video,plugdev,netdev,bluetooth,scanner,pipewire -s /bin/bash $2
    echo New admin user created, UID $1 Username $2
else
    useradd -u $1 -m -G users,lp,dialout,cdrom,floppy,audio,dip,video,plugdev,netdev,bluetooth,scanner,pipewire -N -s /bin/bash $2
    echo New regular user created, UID $1 Username $2
fi

#set password for newly-created user.
passwd $2

I invariably will delete the default first user setup (UID 1008 GID 1010) because I'm not going to use that! I always use the same UIDs for myself, my wife and a guest account, to enable data to be shared across partitions.


Last edited by davecs on 19th July 2024, 9:38 am; edited 2 times in total
nobody
nobody


Posts : 48
Join date : 2024-05-12
Location : Finland

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty Re: For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by nobody 15th June 2024, 11:07 pm

Do you start the script like

Code:
./the-script-here usename_here
?
avatar
davecs


Posts : 110
Join date : 2024-05-14

For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty Re: For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by davecs 16th June 2024, 1:30 am

No, it's

Code:

./the-script-here uid-number username

For example

Code:

./script 1004 john

And as I said, it's only useful for people who need to set specific UID numbers.

Sponsored content


For those who wish to set user IDs. FINAL SCRIPT see Post #7 Empty Re: For those who wish to set user IDs. FINAL SCRIPT see Post #7

Post by Sponsored content

  • Post new topic
  • Reply to topic

Similar topics

-

Current date/time is 20th September 2024, 6:54 pm