在脚本中创建一个sudo用户,不提示输入密码,在不中断脚本的情况下更改为user [英] Create a sudo user in script with no prompt for password, change to user without interrupting script

查看:377
本文介绍了在脚本中创建一个sudo用户,不提示输入密码,在不中断脚本的情况下更改为user的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我要在脚本中执行的操作.它在这里手动工作,但提示我输入密码.我如何:

This is what I'm trying to do in a script. It works here manually, but prompts me for a password. How do I:

  1. 创建一个新用户
  2. 使用sudo privs
  3. 切换到该用户
  4. 继续执行脚本的其余部分

  1. Create a new user
  2. With sudo privs
  3. Switch to that user
  4. Continue executing the rest of the script

sudo adduser centos
sudo passwd centos
usermod -aG wheel centos
sudo su centos

我尝试了以下方法,但是在Centos 7 bash中,--disabled-password和-gecos都说找不到选项".

I have tried the following but in Centos 7 bash, --disabled-password and -gecos both say "option not found."

adduser --disabled-password --gecos "" username

推荐答案

您不需要sudo su centos,因为您的脚本会被终端中断. 如果以下命令实际上是必须由"centos"用户启动的"./install.sh"(如),则可以进行以下修改:

You don't need sudo su centos because your script would be interrupted by a terminal. If the following commands are actually "./install.sh" (like) that have to be started by "centos" user, then you can do the following modification:

sudo adduser centos
sudo passwd centos
usermod -aG wheel centos
sudo su - centos -c ./install.sh
sudo su - centos -c ./install_another.sh

sudo su - centos -c "./install_more.sh ; cd /tmp ; ./install_almostlast.sh"

sudo su - centos -c bash -c "cd /somewhere; ./install_more.sh
        cp /tmp/files /somewhere
        ./install_last.sh
        rm /tmp/install.sh"

双引号之间可以根据需要编写整个脚本,并注意内容和引号.

Between double quotes you can write a whole script if you want and are careful of the content and quoting.

这篇关于在脚本中创建一个sudo用户,不提示输入密码,在不中断脚本的情况下更改为user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆