sos-ci run-on-vm-change-user.sh脚本分析
➜ sos-ci git:(master) cat -n run-on-vm-change-user.sh
1 #!/bin/bash
2
3 if [ "$EUID" -ne 0 ]
4 then echo "Please run as root"
5 exit 1
6 else
7 if [ "$USER" != "quantastor-ci" ]; then // 如果当前用户不是quantastor-ci
8 grep "/bin/bash" /etc/passwd | cut -d: -f1 | grep -q quantastor-ci //查找包含/bin/bash的行,并且取用分隔符为:分开的第一列内容,进一步查找quantastor-ci,-q表示不输出查找结果。
9 if [ $? != 0 ]; then //如果没找到的话
10 adduser --quiet --disabled-password --shell /bin/bash --home /home/quantastor-ci --gecos "User" quantastor-ci //增加用户, --quiet: 压缩informational message, 仅显示警告与错误,--disabled-password: disabled-login, but logins ate still possible(for example using SSH RSA keys) but not using password authentication.
11 echo "quantastor-ci:master#123" | sudo chpasswd //更新用户密码以batch模式
12 fi
13 fi
14 echo 'quantastor-ci ALL=(ALL:ALL) NOPASSWD:ALL' >> /etc/sudoers.d/quantastor-ci //设置用户为root权限
15 su - quantastor-ci -c "sudo bash /home/$1/sos-ci/start-ci.sh $1" //切换用户,
16 fi
17
18 #sudo bash initial-setup-ci.sh $USER
-------------------------------------------
adduser
-
–disabled-login
不创建用户密码,而且用户也不能从外部登录。
- –disabled-password
与 –disabled-login 类似,不过用户可以登录,但不用使用密码认证。
- --gecos GECOS : Set the gecos field for the new entry generated. adduser will not ask for finger information if this option is given.
---------------------------------------------
CHPASSWD(8) System Management Commands CHPASSWD(8)
NAME
chpasswd - update passwords in batch mode
SYNOPSIS
chpasswd [options]
DESCRIPTION
The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing users. Each line is of the format:
user_name:password
By default the passwords must be supplied in clear-text, and are encrypted by chpasswd. Also the password age will be updated, if present.
---------------------------------------------
SU(1) User Commands SU(1)
NAME
su - change user ID or become superuser
SYNOPSIS
su [options] [username]
DESCRIPTION
The su command is used to become another user during a login session. Invoked without a username, su defaults to becoming the superuser. The optional argument - may be used to
provide an environment similar to what the user would expect had the user logged in directly.
-c, --command COMMAND
Specify a command that will be invoked by the shell using its -c.
The executed command will have no controlling terminal. This option cannot be used to execute interactive programs which need a controlling TTY.
-, -l, --login
Provide an environment similar to what the user would expect had the user logged in directly.
When - is used, it must be specified before any username. For portability it is recommended to use it as last option, before any username. The other forms (-l and --login) do
not have this restriction.