Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-28-2012, 07:32 AM   #1
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:
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:
#! /bin/bash

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


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



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

# 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, 08:10 AM   #2
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 08:27 AM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
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 08:33 AM.


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 06:35 PM
remove duplicate lines from shell script himu3118 Programming 16 04-21-2012 07:37 AM
Shell Script :- to get UID and GID furquan Programming 30 02-21-2006 07:44 AM
Script to change uid's pingu Linux - Software 2 03-23-2005 04:15 AM
need a script that can change the passwd bahadur Linux - Security 1 07-09-2004 05:07 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:04 PM.

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