📄 adduser
字号:
#!/bin/sh## adduser 1.4: a utility to add users to the system## Copyright (C) 1994 Ian A. Murdock <imurdock@shell.portal.com>## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.## Written for the Debian Linux distribution (01/21/94). Feel free to use# it in YOUR distribution, too. :) Please note that this is just a simple# script that _somewhat_ automates the really boring and repetitive task# of creating new user accounts. It makes no attempt to be sophisticated.# Let me know if you improve it in any way.## I need to write a man page for this.## Modified by Marc Ewing <marc@redhat.com> for Red Hat Linux# Modified by Michael K. Johnson <johnsonm@redhat.com> for optional# shadow password support.# Modified by Michael K. Johnson <johnsonm@redhat.com> to make it safer# Modified by Joshua Go <joshuago@usa.net> to make it add anon FTP space# Everything happens too fast, so don't let the user interrupt.trap "" 1 2 3 15# Set a few important variables before getting started.NUMARG=$#LOGIN="$1"EXIST=0NOHOME="$2"PASSWD="/etc/passwd"PBAK="/etc/passwd-" # Some programs use /etc/passwd-, others use # /etc/passwd.OLD. Take your pick.PNEW="/etc/passwd.new"SHADOW="/etc/shadow"SBAK="/etc/shadow-"SNEW="/etc/shadow.new"GROUP="/etc/group"GBAK="/etc/group-"GNEW="/etc/group.new"PLOCK="/etc/.pwd.lock" # Standard method of locking the password file.DSHELL="/bin/bash"DHOME="/home"SKEL="/etc/skel"SPOOL="/var/spool/mail"FIRST_UID=500FIRST_GID=500# A few sanity checks...if [ `id -u` != 0 ]; then echo "Only root may add users to the system." ; exit 1fiif [ $NUMARG = 0 ]; then echo "You need to specify the login to add; for example, \`adduser redneck'." ; exit 1# this is the donnie memorial shell script...fiif id "$LOGIN" >/dev/null 2>&1 ; then echo "User $LOGIN already exists" exit 1fiif [ $(echo "$LOGIN" | wc -c | tr -d ' ') -gt 9 ] ; then # that 9 is intentional -- it counts the newline, too... echo "$LOGIN is over eight characters" exit 1fiif echo "$LOGIN" | grep -q : ; then echo "$LOGIN contains a \`:' character" exit 1fiid $LOGIN >/dev/null 2>/dev/null && EXIST=1if [ $EXIST = 1 ]; then echo "The login $LOGIN already exists." exit 1fiif ln $PASSWD $PLOCK; then : ; else echo "$PASSWD is locked. Try again later." ; exit 1fidie (){ rm -f $PLOCK exit 1}# And now the program begins: echo "" ; echo -n "Looking for first available UID..."NUID=`cut -f 3 -d ":" $PASSWD | sort -n | awk -v uid=$FIRST_UID ' { if ($1 == uid) uid = uid + 1; }END { print uid; }'`if [ $NUID -ge 65535 ]; then echo "Sorry, ran out of uids." diefiecho " $NUID"echo -n "Looking for first available GID..."NGID=`cut -f 3 -d ":" $GROUP | sort -n | awk -v gid=$FIRST_GID ' { if ($1 == gid) gid = gid + 1; }END { print gid; }'`if [ $NGID -lt $FIRST_GID ]; then NGID=$FIRST_GIDfiif [ $NGID -ge 65535 ]; then echo "Sorry, ran out of gids." diefiecho " $NGID"echo "" ; echo -n "Adding login: $LOGIN..."cp $PASSWD $PBAK || diecp $PASSWD $PNEW || dieif [ -f "$SHADOW" ] ; then echo "$LOGIN:x:$NUID:$NGID:RedHat Linux User:$DHOME/$LOGIN:$DSHELL" | \ cat >> $PNEW || die # need cat because echo won't return a return code cp $SHADOW $SBAK || die cp $SHADOW $SNEW || die # The date invocation is non-standard, but works with GNU date. # It fills in the "last changed" field with the number of DAYS # since epoch (86400 seconds per day; %s is second since epoch). echo "$LOGIN:*:$(($(date '+%s')/86400))::::::" | \ cat >> $SNEW || dieelse echo "$LOGIN:*:$NUID:$NGID:RHS Linux User:$DHOME/$LOGIN:$DSHELL" | \ cat >> $PNEW || diefi# Add user to users groupcp $GROUP $GBAK || diecp $GROUP $GNEW || diesed "s/^\(users:.*[^:]\)\$/\1,$LOGIN/"' '"s/^\(users:.*:\)\$/\1$LOGIN/" < $GBAK > $GNEW || dieecho "$LOGIN::$NGID:$LOGIN" | cat >> $GNEW || diemv $GNEW $GROUPif [ -f "$SHADOW" ] ; then mv $SNEW $SHADOWfimv $PNEW $PASSWDrm -f $PLOCKecho "done."if [ "x$NOHOME" = "x" ]; then echo -n "Creating home directory: $DHOME/$LOGIN..." mkdir $DHOME/$LOGIN chmod 2775 $DHOME/$LOGIN cp -a $SKEL/.??* $SKEL/* $DHOME/$LOGIN >/dev/null 2>/dev/null chown -R $NUID.$NGID $DHOME/$LOGIN echo "done."# Add FTP directories echo -n "Creating FTP directory: /home/ftp/pub/$LOGIN..." mkdir /home/ftp/pub/$LOGIN chmod 2775 /home/ftp/pub/$LOGIN chown -R $NUID.$NGID /home/ftp/pub/$LOGIN echo "done."# End add FTP directoriesecho -n "Creating mailbox: $SPOOL/$LOGIN..."touch $SPOOL/$LOGIN ; chmod 660 $SPOOL/$LOGIN ; chown $NUID.mail $SPOOL/$LOGINecho "done."fiecho ""echo "Don't forget to set the password."if [ "x$NOHOME" != "x" ]; then echo "" echo "The home directory for $LOGIN was set to $DHOME/$LOGIN but the directory" echo "was not created. Be sure that you set it up properly."fi#passwd $LOGIN#chfn $LOGIN# EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -