`
sitoto
  • 浏览: 124220 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Adding users to sudoers through shell script

阅读更多
You could simply echo (with elevated privileges, of course) directly to the /etc/sudoers file:

nickw444@laptop ~ $ sudo -i
nickw444@laptop ~ $ echo 'nickw444  ALL=(ALL:ALL) ALL' >> /etc/sudoers
(note the tab character between the username and the first ALL)

Or, for a script:

#!/bin/bash
sudo echo 'nickw444 ALL=(ALL:ALL) ALL' >> /etc/sudoers
Then save to somefile.sh and run ./somefile.sh from a terminal window.

To add multiple users, change the script to this;

#!/bin/bash

while [[ -n $1 ]]; do
    sudo echo "$1   ALL=(ALL:ALL) ALL" >> /etc/sudoers;
    shift # shift all parameters;
done
Then, run the script like this (assuming you saved it as addsudousers.sh):

nickw444@laptop ~ $ sudo ./addsudousers.sh username1 username2 somenewadmin bob
that is, space-separated.

To read the names from a file:

nickw444@laptop ~ $ sudo ./addsudusers.sh `cat listofusers.txt`
listofusers.txt should also be space-separated.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics