LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-28-2012, 08:32 AM   #1
hugataga
LQ Newbie
 
Registered: Nov 2012
Posts: 1

Rep: Reputation: Disabled
Unhappy duplicate UID in /etc/passwd - change with shell script


Hello, I need to read /etc/passwd and check for duplicate UIDs for different users. If found, assign them the next free UID above 1000.

can somebody help me ?

A sample from my /etc/passwd file:
Code:
hugataga:x:1000:1000:hugataga,,,:/home/hugataga:/bin/bash
proftpd:x:107:65534::/var/run/proftpd:/bin/false
ftp:x:108:65534::/srv/ftp:/bin/false
petri:x:1000:1000:petri,,,:/home/petri:/bin/bash
The user hugataga and petri have the same UID 1000. i would like to change the UID for the user petri to the next not used 1001. Is there a way to do that with a bash script ?

I have the script to find the next free UID:
Code:
#! /bin/bash

function usedUid()
{
    if [ -z "$1" ]
    then
    return
    fi
    for i in ${lines[@]} ; do
        if [ $i == $1 ]
        then
        return 1
    fi
    done
return 0
}

i=0

# load all the UIDs from /etc/passwd
lines=( $( cat /etc/passwd | cut -d: -f3 | sort -n ) )

testuid=999

x=1

# search for a free uid greater than 999 (default behaviour of adduser)
while [ $x -eq 1 ] ; do
    testuid=$(( $testuid + 1))
    usedUid $testuid
    x=$?
done

# print the just found free uid
echo $testuid
but i have no idea how to change the uid with a script. It is possible to change it manually with nano, but not for a passwd file with 600 entries.
 
Old 11-28-2012, 09:10 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Sounds like you want "usermod" to make the change - then if you aren't aiming for top efficiency just have a block of code repeated until there are no more changes to make.

Changing owners of home directories and all that would be a further step.
 
Old 11-28-2012, 09:27 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
awk -F: -v OFS=: '{ $3=$4=($3+a[$3]++) }1' <( sort -t : -k3n /etc/passwd )

First, the input needs to be sorted, so I used a bash process substitution to dynamically create the sorted "file".

awk's -F and -v options set the input and output field separators to ":".

$3=$4=() changes fields 3 and 4 (I'm assuming that gid needs to be incremented too) to the new value calculated inside the parentheses.

$3+a[$3]++ adds the current value of the array entry "a[$3]" to the value of field three, then post-increments the array value by one ("a[$3]++"). This means that the first time you encounter a given value in field 3, a[$3] will be zero, and one higher for every time encountered after that.

The final "1" at the end is a shortcut trick for printing the current line after the modifications have been made. awk only outputs to stdout, so you'll have to save it to a temp file and replace it after that.

Edit: Note however that this is just a quick and brain-dead solution. As it stands it only increments the uid number by one and doesn't do any testing against subsequent conflicts.

Last edited by David the H.; 11-28-2012 at 09:33 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passwd change script cbaldwin12 Linux - Newbie 2 05-11-2012 07:35 PM
remove duplicate lines from shell script himu3118 Programming 16 04-21-2012 08:37 AM
Shell Script :- to get UID and GID furquan Programming 30 02-21-2006 08:44 AM
Script to change uid's pingu Linux - Software 2 03-23-2005 05:15 AM
need a script that can change the passwd bahadur Linux - Security 1 07-09-2004 06:07 AM


All times are GMT -5. The time now is 06:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration