bash add_user - aragorn/home GitHub Wiki
κ°μ
- λ¬Έμ/μμ€μ½λ μ΅μ’ μμ : 2012-03-04, κΉμ κ²Έ
- 리λ μ€μμ μμ€ν κ³μ μ μΆκ°λ‘ μμ±νλ μ€ν¬λ¦½νΈμ΄λ€. νμ¬μμ μ°λ κ΄λ‘μ λ°λΌ /data1/ λλ ν 리 μλμ νλλ ν 리λ₯Ό μμ±νκ³ , /data[n]/ λλ ν 리μ λ°μ΄ν° μ μ₯μ© λλ ν 리λ€μ λͺ¨λ μμ±νλ€.
- ssh μ μμ μν rsa keyλ₯Ό μμ±νλ μ΅μ μ μ 곡νμ¬, .ssh/id_rsa, id_rsa.pub νμΌμ μμ±νλ€.
- .ssh/authorized_keys νμΌμλ μμ μ .ssh/id_rsa.pub νμΌμ κΈ°λ³Έμ μΌλ‘ νμ μΆκ°νκ² λλ€. μ΄λ, μ€λ³΅ μ¬λΆ κ²μ¬ν΄μ, νμν κ²½μ°μλ§ μΆκ°νλ€.
- .ssh/known_hosts νμΌλ μ΅μ μΌλ‘ μλ μμ±νλ€. /etc/hosts νμΌμ λ±λ‘λ λͺ¨λ host μ λν΄μ known_hosts νμΌμ μμ±νλ€.
- λ©±λ±μ±(idempotence)λ₯Ό κ°λλ‘ κ΅¬ννμλλ°, μμ§ μΆ©λΆν ν μ€νΈν΄λ³΄μ§λ λͺ»νμλ€. μμΌλ‘ ν μ€νΈν΄ λ³Έ λͺλͺ μΌμ΄μ€μ λν΄μλ bug fixλ₯Ό νλλ°, λ€λ₯Έ λΆλ€μ΄ μ¨ λ³΄μκ³ feedback μ£Όμλ©΄ μ’κ² λ€.
- RHEL5 μμ μ£Όλ‘ μ¬μ©νκΈ° λλ¬Έμ, λ€λ₯Έ 리λ μ€ λ°°ν¬λ³Έμμλ ν μ€νΈκ° λ νμν μ μλ€.
- λΉλ°λ²νΈ μ§μ ν λμλ openssl λͺ λ Ήμ μ΄μ©νλ©΄ νΈλ¦¬νλ€. μνΈνλ λΉλ°λ²νΈ λ¬Έμμ΄μ μμ±ν λ μ μ©νλ€. μμΈν 건 openssl(1) man page μ°Έμ‘°νλ©΄ λλ€.
μ£Όμν μ
--dry-run
μ΅μ
μ μλνμ§ μλλ€. ꡬνλμ§ μμ κΈ°λ₯μ΄λ€.
μ¬μ©λ²
- κ³μ μμ±νκΈ° - λΉλ°λ²νΈλ κΈ°λ³Έ κ° (μμ€μ½λ λ΄ μνΈνλ λΉλ°λ²νΈλ₯Ό κ³ μ³μ μ¬μ©νμΈμ)
add_user username
- λΉλ°λ²νΈ μμλ‘ μ§μ νκΈ°
add_user username $(openssl passwd -1 'λΉλ°λ²νΈ')
- known_hosts νμΌ μμ±νκΈ° (κΈ°λ³Έ λΉλ°λ²νΈλ‘ μ΄κΈ°νλ¨)
add_user --known-hosts username
- ssh rsa key μμ±νκΈ° (κΈ°λ³Έ λΉλ°λ²νΈλ‘ μ΄κΈ°νλ¨)
add_user --rsa-key username
- λΉλ°λ²νΈ μ§μ νλ©΄μ known_hosts μμ±νκ³ rsa key μμ±νκΈ°
add_user --known-hosts --rsa-key username $(openssl passwd -1 'λΉλ°λ²νΈ')
- μμΈν μμ
νν© νμΈνκΈ° : -v μ΅μ
μΆκ°
add_user -v --known-hosts --rsa-key username $(openssl passwd -1 'λΉλ°λ²νΈ')
- μ¬μ©λ² μμ½
Usage: add_user [--known-hosts] [--rsa-key] [-v] [-v] [--dry-run] [-h] [--help] username username login username to create account of encrypted password encrypted password of account
Options - --known-hosts generates .ssh/known_hosts from /etc/hosts --rsa-key generates .ssh/id_rsa -v enables verbose mode 1 -v -v enables verbose mode 2 --dry-run shows what would have been done -h, --help shows this help message
## μ¬λ¬ μλ²μμ κ³μ μ μμ±νκΈ°
μ¬λ¬ μλ²μμ λμΌν κ³μ μ μΌκ΄ μμ±νλ €λ κ²½μ°, λ€μκ³Ό κ°μ λ°©μμ μ¬μ©ν μ μλ€.
### 첫λ²μ§Έ μλ²μ root κ³μ μμ λλ¨Έμ§ μλ²λ‘ λΉλ°λ²νΈ μμ΄ μ μ κ°λ₯ν νκ²½μ ꡬμ±νλ€.
#### 첫λ²μ§Έ μλ²μ root κ³μ μμ, ssh μ°κ²°μ μν rsa keyλ₯Ό μμ±νλ€.
[root@hostname ~] ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa Generating public/private rsa key pair. Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 5f:92:e9:f1:9f:f7:5a:dc:8e:da:ea:66:a8:1b:34:1c root@hostname [root@hostname ~]
#### .ssh/known_hosts νμΌμ μ€λΉνλ€.
##### /etc/hosts μμ hostμ΄λ¦, ipμ£Όμλ₯Ό μΆμΆνκΈ° μν awk λͺ
λ Ήμ μ€λΉνλ€.
μλ AWK_KNOWN_HOSTS=... λΆλΆμμ END) λΆλΆκΉμ§λ₯Ό copy & pasteλ‘ λΆμ¬λ£κΈ° νλ©΄ λλ€. μ΄ν `echo ${AWK_KNOWN_HOSTS}` λͺ
λ ΉμΌλ‘ μ μμ μΌλ‘ μ
λ ₯λμλμ§ νμΈνλ€.
[root@hostname ~] AWK_KNOWN_HOSTS=$(cat <<-'END' { ip_addr=$1; if ( NF == 3 ) { hostname=$3; } else if ( NF == 2 ) { hostname=$2; } else { hostname="unknown"; } print hostname "," ip_addr; } END) [root@hostname ~] echo ${AWK_KNOWN_HOSTS} { ip_addr=$1; if ( NF == 3 ) { hostname=$3; } else if ( NF == 2 ) { hostname=$2; } else { hostname="unknown"; } print hostname "," ip_addr; } [root@hostname ~]
##### /etc/hosts μ κ° νΈμ€νΈμ λν΄ ssh-keyscan(1) λͺ
λ Ήμ μ€ννκ³ , κ·Έ κ²°κ³Όλ₯Ό .ssh/known_hosts λ‘ μ μ₯νλ€.
μλ λͺ
λ Ήμμ > ~/.ssh/known_hosts μ€κΉμ§λ§ μ
λ ₯νλ©΄, λͺ
λ Ήμ΄ μ€νλλ©΄μ κ° νΈμ€νΈλ³λ‘ sshd versionμ νμΈνλ λ©μμ§κ° μΆλ ₯λλ€.
[root@hostname ~] sed -e '/^\s*#/d' -e '/^$/d' /etc/hosts
| gawk "${AWK_KNOWN_HOSTS}"
| ssh-keyscan -t rsa -f - \
~/.ssh/known_hosts
host1-blah SSH-2.0-OpenSSH_4.3
host1-blah SSH-2.0-OpenSSH_4.3
host1-blah SSH-2.0-OpenSSH_4.3
host1-blah SSH-2.0-OpenSSH_4.3
host1-blah SSH-2.0-OpenSSH_4.3
[root@hostname ~]
* μ°Έκ³ μ¬ν
known_hosts νμΌμ ~/.ssh/known_hosts μ μ μ₯ν΄λ λμ§λ§, system-wide νκ² μ¬μ©ν μ μλλ‘, /etc/ssh/ssh_known_hosts νμΌμ μ μ₯νμ¬λ λλ€.
#### μ 체 μλ² λ¦¬μ€νΈλ₯Ό μ€λΉνλ€.
##### /etc/hosts μμ hostμ΄λ¦μ μΆμΆνκΈ° μν awk λͺ
λ Ήμ μ€λΉνλ€.
μλ AWK_HOSTS=... λΆλΆμμ END) λΆλΆκΉμ§λ₯Ό copy & pasteλ‘ λΆμ¬λ£κΈ° νλ©΄ λλ€. μ΄ν `echo ${AWK_HOSTS}` λͺ
λ ΉμΌλ‘ μ μμ μΌλ‘ μ
λ ₯λμλμ§ νμΈνλ€.
[root@hostname ~] AWK_HOSTS=$(cat <<-'END' { ip_addr=$1; if ( NF == 3 ) { hostname=$3; } else if ( NF == 2 ) { hostname=$2; } else { hostname="unknown"; } print hostname; } END) [root@hostname ~] echo ${AWK_HOSTS} { ip_addr=$1; if ( NF == 3 ) { hostname=$3; } else if ( NF == 2 ) { hostname=$2; } else { hostname="unknown"; } print hostname; } [root@hostname ~]
##### /etc/hosts μμ hostμ΄λ¦λ§ μΆμΆνμ¬ μ λΉν μ΄λ¦μ νμΌλ‘ μ μ₯νλ€.
[root@hostname ~] sed -e '/^\s*#/d' -e '/^$/d' /etc/hosts
| gawk "${AWK_HOSTS}" > list.txt
[root@hostname ~]
#### 첫λ²μ§Έ μλ²μμ λλ¨Έμ§ λͺ¨λ μλ²λ‘ ssh-copy-id λͺ
λ Ήμ μ€ννλ€.
##### μ¬λ¬ νΈμ€νΈμ λν΄ ssh-copy-id λͺ
λ Ήμ μ€ννλ€.
λͺ
λ Ήμ μ
λ ₯νλ©΄, κ° νΈμ€νΈμ root λΉλ°λ²νΈλ₯Ό λ°λ³΅μ μΌλ‘ λ¬Όμ΄λ³΄κ² λλ€. λΉλ°λ²νΈλ₯Ό λ°λ³΅νμ¬ μ
λ ₯νλ©΄ λλ€. copy & pasteλ‘ λ°λ³΅μ μΌλ‘ μ
λ ₯νλ©΄ νΈλ¦¬νλ€.
[root@hostname ~] cat list.txt | xargs -I {} ssh-copy-id -i ~/.ssh/id_rsa {} root@localhost's password: Now try logging into the machine, with "ssh 'localhost'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
0 root@hostname-blah's password: Now try logging into the machine, with "ssh 'hostname-blah'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
0 ... μ€λ΅ ... root@hostname-blah's password: Now try logging into the machine, with "ssh 'hostname-blah'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@hostname ~]
##### root κ³μ μμ λΉλ°λ²νΈ μμ΄ μ¬λ¬ μλ²λ‘ ssh μ°κ²°λλμ§ νμΈν΄ λ³Έλ€.
### κ° μλ²μ add_user μ€ν¬λ¦½νΈλ₯Ό μΌκ΄ μ€μΉνλ€.
[root@hostname ~] mkdir bin [root@hostname ~] curl -u ssdbuilder:ssdbuilder http://source.daumcorp.com/private/aragorn/trunk/setup/add_user > bin/add_user % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 5330 100 5330 0 0 34264 0 --:--:-- --:--:-- --:--:-- 1297k [root@hostname ~] chmod 755 bin/add_user [root@hostname ~] cat list.txt | xargs -I {} rsync -a bin/add_user {}:bin/ [root@hostname ~]
### κ° μλ²μμ μνλ κ³μ μ μΌκ΄ μμ±νλ€.
[root@hostname ~] cat list.txt | xargs -I {} ssh {} bin/add_user foobaruser [root@hostname ~]
### 첫λ²μ§Έ μλ² κ³μ μμ rsa key, authorized_keys νμΌμ μμ±ν μ΄ν, κ° μλ²μ ν΄λΉ κ³μ μΌλ‘ 볡μ¬νλ€.
[root@hostname ~] cat list.txt | xargs -I {} ssh {} ls -l /data1/foobaruser/.ssh ls: /data1/foobaruser/.ssh: κ·Έλ° νμΌμ΄λ λλ ν λ¦¬κ° μμ ls: /data1/foobaruser/.ssh: No such file or directory ls: /data1/foobaruser/.ssh: No such file or directory ...μ€λ΅... [root@hostname ~] bin/add_user --known-hosts --rsa-key foobaruser [root@hostname ~] cat list.txt | xargs -I {} rsync -a /data1/foobaruser/.ssh {}:/data1/foobaruser/ [root@hostname ~] cat list.txt | xargs -I {} ssh {} ls -l /data1/foobaruser/.ssh ν©κ³ 20 -rw------- 1 foobaruser users 408 3μ 4 15:23 authorized_keys -rw------- 1 foobaruser users 1675 3μ 4 15:23 id_rsa -rw-r--r-- 1 foobaruser users 408 3μ 4 15:23 id_rsa.pub -rw-r--r-- 1 foobaruser users 4516 3μ 4 15:23 known_hosts total 20 -rw------- 1 foobaruser users 408 Mar 4 15:23 authorized_keys -rw------- 1 foobaruser users 1675 Mar 4 15:23 id_rsa -rw-r--r-- 1 foobaruser users 408 Mar 4 15:23 id_rsa.pub -rw-r--r-- 1 foobaruser users 4516 Mar 4 15:23 known_hosts ...μ€λ΅... [root@hostname ~]
### κ° μλ²μ ν΄λΉ κ³μ μμ λΉλ°λ²νΈ μμ΄ λ€λ₯Έ μλ²λ‘ μ°κ²°λλμ§ νμΈνλ€.
## μμ€μ½λ
add_user
```bash
#!/bin/bash
set -o nounset
set -o errexit
VERBOSE_MODE=0
VERBOSE=''
function error_handler()
{
local STATUS=${1:-1}
[ ${VERBOSE_MODE} == 0 ] && exit ${STATUS}
echo "Exits abnormally at line "`caller 0`
exit ${STATUS}
}
trap "error_handler" ERR
PROGNAME=`basename ${BASH_SOURCE}`
DRY_RUN_MODE=0
GENERATE_KNOWN_HOSTS=0
GENERATE_RSA_KEY=0
function print_usage_and_exit()
{
local STATUS=$1
echo "Usage: ${PROGNAME} [--known-hosts] [--rsa-key] [-v] [-v] [--dry-run] [-h] [--help] username <encrypted password>"
echo " username login username to create account of"
echo " encrypted password encrypted password of account"
echo ""
echo " Options -"
echo " --known-hosts generates .ssh/known_hosts from /etc/hosts"
echo " --rsa-key generates .ssh/id_rsa"
echo " -v enables verbose mode 1"
echo " -v -v enables verbose mode 2"
echo " --dry-run shows what would have been done"
echo " -h, --help shows this help message"
exit ${STATUS:-0}
}
function debug()
{
if [ "$VERBOSE_MODE" != 0 ]; then
echo $@
fi
}
GETOPT=`getopt -o vh --long known-hosts,rsa-key,dry-run,help -n "${PROGNAME}" -- "$@"`
if [ $? != 0 ] ; then print_usage_and_exit 1; fi
eval set -- "${GETOPT}"
while true
do case "$1" in
--known-hosts) GENERATE_KNOWN_HOSTS=1; shift;;
--rsa-key) GENERATE_RSA_KEY=1; shift;;
-v) let VERBOSE_MODE+=1; VERBOSE="-v" shift;;
--dry-run) DRY_RUN_MODE=1; shift;;
-h|--help) print_usage_and_exit 0;;
--) shift; break;;
*) echo "Internal error!"; exit 1;;
esac
done
if (( VERBOSE_MODE > 1 )); then
set -x
fi
# template area is ended.
# -----------------------------------------------------------------------------
if [ ${#} == 0 ]; then print_usage_and_exit 1; fi
DEFAULT_PASSWD='$1$ojDY/m1G$m2pSKWobHPoeaGPrKcx/70'
HOSTNAME=$(hostname)
USERNAME=$1
ENCRYPTED_PASSWD=${2:-${DEFAULT_PASSWD}}
debug "USERNAME=${USERNAME}"
debug "ENCRYPTED_PASSWD=${ENCRYPTED_PASSWD}"
if [ "${USERNAME}" =~ '[^[:alnum:](/aragorn/home/wiki/-"${USERNAME}"-=~-'[^[:alnum:)' ]]; then
echo "Invalid username which has non-alphanumeric."
print_usage_and_exit 1
fi
if [ "${USERNAME}" =~ '[[:space:](/aragorn/home/wiki/-"${USERNAME}"-=~-'[[:space:)' ]]; then
echo "Invalid username which has whitespace(s)."
print_usage_and_exit 1
fi
if [ "${ENCRYPTED_PASSWD}" =~ '[[:space:](/aragorn/home/wiki/-"${ENCRYPTED_PASSWD}"-=~-'[[:space:)' ]]; then
echo "Invalid encrypted password which has whitespace(s)."
print_usage_and_exit 1
fi
if [ $(id ${USERNAME} 2>/dev/null) == "" ](/aragorn/home/wiki/-$(id-${USERNAME}-2>/dev/null)-==-""-); then
debug "${USERNAME} does not exist."
/usr/sbin/useradd -g users -d /data1/${USERNAME} -m -p "${ENCRYPTED_PASSWD}" ${USERNAME}
else
debug "${USERNAME} already exists."
/usr/sbin/usermod -g users -d /data1/${USERNAME} -m -p "${ENCRYPTED_PASSWD}" ${USERNAME}
fi
for DATADIR in /data?
do
if [ -d ${DATADIR}/${USERNAME} ]
then
debug "${DATADIR}/${USERNAME} already exists."
else
install -d -o ${USERNAME} -g users -m 755 ${DATADIR}/${USERNAME}
fi
done
if [ ${GENERATE_KNOWN_HOSTS} == 1 ]
then
AWK_CMD=$(cat <<-'END'
{
ip_addr=$1;
if ( NF == 3 ) { hostname=$3; } else
if ( NF == 2 ) { hostname=$2; } else
{ hostname="unknown"; }
print hostname "," ip_addr;
}
END)
install -d -o ${USERNAME} -g users -m 700 /data1/${USERNAME}/.ssh
touch /data1/${USERNAME}/.ssh/known_hosts
chmod 644 /data1/${USERNAME}/.ssh/known_hosts
chown ${USERNAME}:users /data1/${USERNAME}/.ssh/known_hosts
# redirect stderr to /dev/null when verbose mode is not enabled.
if [ "${VERBOSE_MODE}" == 0 ]
then
exec 3<&2
exec 2>/dev/null
fi
echo "stderr would be redirected to /dev/null." >&2
sed -e '/^\s*#/d' -e '/^$/d' /etc/hosts \
| gawk "${AWK_CMD}" \
| ssh-keyscan ${VERBOSE} -t rsa -f - \
| comm -23 - /data1/${USERNAME}/.ssh/known_hosts \
>> /data1/${USERNAME}/.ssh/known_hosts
# restore stderr
if [ "${VERBOSE_MODE}" == 0 ]
then
exec 2<&3
fi
debug "stderr is restored."
fi
if [ ${GENERATE_RSA_KEY} == 1 ]
then
install -d -o ${USERNAME} -g users -m 700 /data1/${USERNAME}/.ssh
rm -f /data1/${USERNAME}/.ssh/id_rsa
rm -f /data1/${USERNAME}/.ssh/id_rsa.pub
ssh-keygen -q -t rsa -N '' -C "${USERNAME}@${HOSTNAME}" -f /data1/${USERNAME}/.ssh/id_rsa
chown ${USERNAME}:users /data1/${USERNAME}/.ssh/id_rsa
chown ${USERNAME}:users /data1/${USERNAME}/.ssh/id_rsa.pub
fi
if [ -f /data1/${USERNAME}/.ssh/id_rsa.pub ](/aragorn/home/wiki/--f-/data1/${USERNAME}/.ssh/id_rsa.pub-)
then
if [[ -f /data1/${USERNAME}/.ssh/authorized_keys \
&& $(comm -12 /data1/${USERNAME}/.ssh/authorized_keys \
/data1/${USERNAME}/.ssh/id_rsa.pub | wc -l) == 1 ]]
then
debug "authorized_keys has id_rsa.pub of ${USERNAME}@${HOSTNAME} already."
else
debug "authorized_keys does not have id_rsa.pub of ${USERNAME}@${HOSTNAME} yet."
install -d -o ${USERNAME} -g users -m 700 /data1/${USERNAME}/.ssh
touch /data1/${USERNAME}/.ssh/authorized_keys
chmod 600 /data1/${USERNAME}/.ssh/authorized_keys
chown ${USERNAME}:users /data1/${USERNAME}/.ssh/authorized_keys
sed -i "/${USERNAME}@${HOSTNAME}/d" /data1/${USERNAME}/.ssh/authorized_keys
cat /data1/${USERNAME}/.ssh/id_rsa.pub >> /data1/${USERNAME}/.ssh/authorized_keys
debug "appended id_rsa.pub to authorized_keys."
fi
else
debug "/data1/${USERNAME}/.ssh/id_rsa.pub does not exists."
fi