阅读更多
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.