我正在制作一个应该创建ftp用户的bash脚本。
ftpasswd --passwd --file=/usr/local/etc/ftpd/passwd --name=$USER --uid=[xxx]
--home=/media/part1/ftp/users/$USER --shell=/bin/false
脚本唯一提供的参数是用户名。但ftpasswd
也需要uid
。我怎么得到这个号码?有没有一种简单的方法来扫描passwd
文件并获取最大数量,增加并使用它?也许有可能从系统中获得这个数字?
答案 0 :(得分:5)
除了阅读/etc/passwd
之外,您还可以采用更加友好的方式进行阅读:
getent passwd
另外请不要忘记,不能保证此序列的UID已经排序。
答案 1 :(得分:3)
要获得给定用户名“myuser”的UID:
cat /etc/passwd | grep myuser | cut -d":" -f3
要在passwd文件中获取最大的UID:
cat /etc/passwd | cut -d":" -f3 | sort -n | tail -1
答案 2 :(得分:2)
获取用户的UID:
cat /etc/passwd | grep "^$usernamevariable:" | cut -d":" -f3
要向系统添加新用户,最好的选择是使用useradd
,或adduser
,如果您需要精细控制。
如果你真的只需要找到最小的免费UID,这里的脚本可以找到大于999的最小空闲UID值(UID 1-999通常保留给系统用户):
#!/bin/bash
# return 1 if the Uid is already used, else 0
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
答案 3 :(得分:1)
我将cat /etc/passwd
更改为geuse passwd以获取Giuseppe的答案。
#!/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 "$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=( $( 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
答案 4 :(得分:0)
这是一个更短的方法:
document.addEventListener("deviceready", onDeviceReady, false);
var deviceUID
function onDeviceReady() {
deviceUID=device.uuid
console.log(deviceUID);
}
//app.js//
var app;
(function () {app = new kendo.mobile.Application(document.body, { skin: 'flat', });}());
答案 5 :(得分:0)
因为这是 bash ,事情会变得更简单
#!/bin/bash
get_available_uid_basic(){
local uid_free=1000
local uids_in_use=( $(cut -d: -f3 < /etc/passwd) )
while [[ " ${uids_in_use[@]} " == *" $uid_free "* ]]; do
(( uid_free++ ))
done
echo $uid_free
}
uid=$(get_available_uid_basic)
echo $uid
说明:
uids_in_use 是一个用于删除换行符的数组
没有“ |排序”,在其他答案中也没有用
$ {uids_in_use [@]}是uids_in_use数组,以空格作为分隔符爆炸
在第一个和最后一个数组条目之前和之后都有空格,因此每个条目的每一边都由空格分隔
bash的[[]]在'=='
useradd / adduser具有--system参数,这将创建用户,其uid在100-999之间
另外,useradd确实会查找从999开始向下的可用uid。
就我而言,我需要这两种行为,这是一个接受“系统”和“反向”参数的函数
#!/bin/bash
get_available_uid(){
local system_range
[[ $* == *system* ]] && system_range=TRUE
local reverse
[[ $* == *reverse* ]] && reverse=TRUE
local step
local uid_free
if [ -n "$system_range" ]; then
if [ -n "$reverse" ]; then
uid_free=999
step=-1
else
uid_free=100
step=1
fi
else
if [ -n "$reverse" ]; then
uid_free=9999
step=-1
else
uid_free=1000
step=1
fi
fi
local uids_in_use=( $(cut -d: -f3 < /etc/passwd) )
while [[ " ${uids_in_use[@]} " == *" $uid_free "* ]]; do
(( uid_free+=step ))
done
if [ -n "$system_range" ]; then
if (( uid_free < 100 )) || (( uid_free > 999 )); then
echo "No more available uids in range" >&2
return 1
fi
else
if (( uid_free < 1000 )); then
echo "No more available uids in range" >&2
return 1
fi
fi
echo $uid_free
return 0
}
uid=$(get_available_uid)
echo "first available user uid: $uid"
uid=$(get_available_uid system)
echo "first available system uid: $uid"
uid=$(get_available_uid reverse)
echo "last available user uid: $uid"
uid=$(get_available_uid system reverse)
echo "last available system uid: $uid"