Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Linux::usermod - user name ... already exists

by thanos1983 (Parson)
on Mar 19, 2019 at 12:09 UTC ( [id://1231430]=note: print w/replies, xml ) Need Help??


in reply to Linux::usermod - user name ... already exists

Hello mldvx4,

An alternative solution would be to change the username through the usermod and also after the /home dir if exists?

Sample of code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $nname = "monk"; my $name = "testuser"; sub rename_account { my ( $name , $nname ) = ( @_ ); my $username = `usermod -l $nname $name`; my $dirusername = `usermod -d /home/$nname -m $nname`; chomp(my @dir = `ls -la /home`); print Dumper \@dir; } # rename_account("monk", "testuser"); rename_account("testuser", "monk"); __END__ $ sudo perl sample.pl $VAR1 = [ 'total 16', 'drwxr-xr-x 4 root root 4096 Mar 19 12:57 .', 'drwxr-xr-x 24 root root 4096 Mar 7 15:04 ..', 'drwxr-xr-x 2 monk testuser 4096 Mar 19 12:51 monk', ];

The script will not produce any error if the user exists etc.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Linux::usermod - user name ... already exists
by haukex (Archbishop) on Mar 21, 2019 at 13:44 UTC
    my $username = `usermod -l $nname $name`; my $dirusername = `usermod -d /home/$nname -m $nname`;

    I would recommend against this way of doing this because it allows for injection of arbitrary shell commands via $nname and $name, and it does not check for errors in the commands. See also.

      Hello haukex,

      I read your post so I guess something like that should be better :).

      Update: Adding minor conversion STDOUT to array for better data manipulation.

      Thank you for the information. BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        Yes, that does look better to me, thanks!

Re^2: Linux::usermod - user name ... already exists
by mldvx4 (Friar) on Mar 19, 2019 at 13:43 UTC

    Yes, a shell script would be an option. It looks like the problem may be Linux::usermod not taking care of /etc/shadow properly. I'm doing some experiments to see if I can scope out the bug.

    It's part of a larger, more complex task where the script can change the name of the account and then update all the groups in which it is a member.

      Hello again mldvx4,

      I was thinking exactly like this. That your script will do something like that.

      Well an alternative solution would be to remove the user Linux::usermod->del(username); (I assume the module removes the user from all groups) and after that create a new user Linux::usermod->add(username, password, uid, gid, comment, home, shell); with the updated name. That could be a workaround (assuming there are no errors on this one as well). Not to forget you can assign the user to the desired groups

      @users = qw(user1 user2 user3); Linux::usermod->grpadd(groupname, gid, "@users")
      . But again this is just theory I have not tested it so I do not know if there is a bug in all these commands.

      Let us know at the end what solution you came up with. BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        Thanks. I gave Linux::usermod->del(username); a try and see that it leaves the groups in /etc/group and /etc/shadow untouched instead of removing / modifying them as needed. I took a look at the source for Linux::usermod and it's over my skill level to dig in and form a patch, at least in a timely manner.

        I'll look into several options now but it may take some time.

        It looks like this is the distilled version of what I will try. The validation of the two variables takes place earlier in another function.

        $ENV{'PATH'}=q(/sbin:/bin:/usr/sbin:/usr/bin); my ($um,$gm); qx/usermod --login $newname --home $newname $oldname/; $um = $?; if ($um==0) { qx/groupmod --new-name $newname $oldname/; $gm = $?; }

        Ideas or corrections?

        No output is produced, so I don't try to capture any. And the home directory gets reassigned and renamed elsewhere, too, since I have a little non-standard home directory arrangement for those groups.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1231430]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-19 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found