bash 如何获得唯一的`uid`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3649760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:35:04  来源:igfitidea点击:

How to get unique `uid`?

linuxbash

提问by Pablo

I'm making a bash script which should create an ftp user.

我正在制作一个 bash 脚本,它应该创建一个 ftp 用户。

ftpasswd --passwd --file=/usr/local/etc/ftpd/passwd --name=$USER --uid=[xxx]
     --home=/media/part1/ftp/users/$USER --shell=/bin/false

The only supplied argument to script is user name. But ftpasswdalso requires uid. How do I get this number? Is there an easy way to scan passwdfile and get the max number, increment it and use it? Maybe it's possible to obtain that number from the system?

脚本唯一提供的参数是用户名。但ftpasswd也需要uid. 我如何得到这个号码?有没有一种简单的方法来扫描passwd文件并获取最大数量,增加它并使用它?也许可以从系统中获取该号码?

采纳答案by Giuseppe Cardone

To get a user's UID:

获取用户的 UID:

cat /etc/passwd | grep "^$usernamevariable:" | cut -d":" -f3

To add a new user to the system the best option is to use useradd, or adduserif you need a fine-grained control.

要将新用户添加到系统中,最好的选择是使用useradd,或者adduser如果您需要细粒度的控件。

If you really need just to find the smallest free UID, here's a script that finds the smallest free UID value greater than 999 (UIDs 1-999 are usually reserved to system users):

如果你真的只需要找到最小的空闲 UID,这里有一个脚本可以找到大于 999 的最小空闲 UID 值(UID 1-999 通常保留给系统用户):

#!/bin/bash

# return 1 if the Uid is already used, else 0
function usedUid()
{
    if [ -z "" ]
    then
    return
    fi
    for i in ${lines[@]} ; do 
        if [ $i ==  ]
        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

回答by bhdnx

Instead of reading /etc/passwd, you can also do it in a more nsswitch-friendly way:

除了阅读之外/etc/passwd,您还可以通过一种对 nsswitch 更友好的方式来完成:

getent passwd

Also don't forget that there isn't any guarantee that this sequence of UIDs will be already sorted.

另外不要忘记,不能保证这个 UID 序列已经排序。

回答by Fernando Miguélez

To get UID given an user name "myuser":

要获取给定用户名“myuser”的 UID:

 cat /etc/passwd | grep myuser | cut -d":" -f3

To get the greatest UID in passwd file:

要在 passwd 文件中获得最大的 UID:

 cat /etc/passwd | cut -d":" -f3 | sort -n | tail -1

回答by Jon Zobrist

I changed cat /etc/passwdto getent passwd for Giuseppe's answer.

/etc/passwd对于 Giuseppe 的回答,我将 cat 更改为 getent passwd。

#!/bin/bash
# From Stack Over Flow
# http://stackoverflow.com/questions/3649760/how-to-get-unique-uid
# return 1 if the Uid is already used, else 0
function usedUid()
{
    if [ -z "" ]
    then
    return
    fi
    for i in ${lines[@]} ; do
        if [ $i ==  ]
        then
        return 1
    fi
    done
return 0
}

i=0

# load all the UIDs from /etc/passwd
lines=( $( getent 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

回答by Phidelux

This is a much shorter approach:

这是一种更短的方法:

#!/bin/bash
uids=$( cat /etc/passwd | cut -d: -f3 | sort -n )
uid=999

while true; do
    if ! echo $uids | grep -F -q -w "$uid"; then
        break;
    fi

    uid=$(( $uid + 1))
done

echo $uid