# !/bin/bash
sudo -i
cd /etc/apt/sources.list.d
echo "deb http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse" >ia32-libs-precise.list
sudo apt-get update
sudo apt-get install ia32-libs
sudo rm /etc/apt/sources.list.d/ia32-libs-raring.list
sudo apt-get update
when I execute this script ,it just do 'sudo -i ' then stop, who can help me ?
The sudo manpage says :
-i,--login
Run the shell specified by the target user's password data‐base entry as a login shell.
.
.
.
If no command is specified, an interactive shell is executed.
No wonder the execution of your script stops.
The commands
cd /etc/apt/sources.list.d
.
.
sudo apt-get update
are never reached because you have just spawned a new shell with sudo -i.
As [ #mona_sax ] suggested in comment,running a script as sudo may not be a good idea in the security context. It's not clear what your actual intention is, but if the intention is to run the script in background then remove sudo -i line and do :
./script 2>&1 1>/dev/null &
Because you don't specify a command to run as root, sudo invokes an interactive shell. It won't terminate until you exit from it (or it is killed by a signal, etc).
If you need it to return immediately, you could pass true as the command:
sudo true
However, in your case, it's probably better, given what you're doing, to just limit the script to only superusers:
#!/bin/sh
set -e
# check we are running as root
if [ $(id -u) != "0" ]; then
echo "ERROR: this script must be run as a superuser" >&2
exit 1
fi
Then it is up to the user to gain appropriate privileges, rather than encoding that into the script.
Related
To permanently update ~/.profile with source, only working on manual input. Also reboot of the whole system won‘t update ~/.profile and i need to update it manual.
Is there a special code style to use it as working code inside a bash/shell script or is this special code not intended to be used in automated scripts?
Need it to automate installation of golang.
In the following code the line "source ~/.profile" won't work, and without any error messages, the rest is working fine:
#!/bin/bash
sudo apt update
sudo apt -y upgrade
cd ~
curl -O https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
tar xvf go1.12.5.linux-amd64.tar.gz
sudo chown -R root:root ./go
sudo mv go /usr/local
cd ~
sudo rm go1.12.5.linux-amd64.tar.gz
sudo echo "export GOPATH=\$HOME/work" >> ~/.profile
sudo echo "export PATH=\$PATH:/usr/local/go/bin:\$GOPATH/bin" >> ~/.profile
source ~/.profile
Preferred:
Source the script itself rather than running it - then the commands in the script are run in the current shell, including the source ~/.profile.
Alternative (since this replaces the running shell, history, variable values, and other state will be lost. So there should be a very good reason to use this method):
Use exec bash or something similar instead of source ~/.profile - this replaces the currently running Bash with another instance which will itself load the new .profile.
Here is a refactoring which defers the decision to the user and cleans up the script somewhat.
#!/bin/bash
# Put this in a variable so there is only one place to update
tarball='go1.12.5.linux-amd64.tar.gz'
sudo apt update
sudo apt -y upgrade
# cd || why would you?
curl -O "https://dl.google.com/go/$tarball"
tar xvf "$tarball"
sudo chown -R root:root ./go
sudo mv go /usr/local
rm "$tarball"
printf '%s\n' "export GOPATH=\$HOME/work" \
"export PATH=\$PATH:/usr/local/go/bin:\$GOPATH/bin" >> ~/.profile
echo "$0: done. source ~/.profile or exec bash to activate new settings." >&2
I am a new Ubuntu user.
Recently, I try to set up a server on Ubuntu.
I am wondering how to write a automatically script to run a series of script one by one.
For example, I need to install squid first, after that I need to make a copy of config file then modify the file. The following are the steps that I write in the command console. I wonder how to make a script to run that automatically.
sudo apt-get install squid -y;
cd /etc/squid3;
sudo cp squid.conf squid.conf.bak;
sudo rm -rf squid.conf;
sudo nano squid.conf
Just add a shebang, place everything in a ".sh" file, make the file executable, and run it...
Save this as test.sh
#!/bin/bash
sudo apt-get install squid -y;
cd /etc/squid3;
sudo cp squid.conf squid.conf.bak;
sudo rm -rf squid.conf;
sudo nano squid.conf
Make it executable chmod +x test.sh
Run it... ./test.sh
To edit the file from a terminal
Get a terminal on the box where you want the script to live. Probably you will SSH into it.
Then just cd to the path you want the script to live and do the following...
nano test.sh This opens the nano terminal text editor.
Copy the above test.sh commands, make sure to get the shebang (#!/bin/bash).
Paste the script into the nano editor, you'll need to use ctrl+v or cmd+v.
Hit the key combination of ctrl + o, hit the enter key.
Hit the key combination of ctrl + w. This exits nano. Proceed with the abov instructions.
I suggest you read up on nano so you can get more familiar with its abilities as it can save a lot of time!
I have wrote some script for my VPS and this is a example for Squid3
#!/bin/bash
function add_user () {
while true; do
echo -e "\nInsert a name for the Squid3 user (0=exit): \c"
read utente
case "$utente" in
0)
echo -e "\nGoodbye $USER!\n"
exit 0
;;
*\ *)
echo -e "\nYou can't use spaces in the name!"
sleep 2
continue
;;
*)
break
;;
esac
done
if [ ! -e '/etc/squid3/.passwd' ]; then
sudo htpasswd -c /etc/squid3/.passwd $utente
else
sudo htpasswd /etc/squid3/.passwd $utente
fi
}
function installer () {
sudo apt-get install squid3 apache2-utils -y
sudo bash -c "echo 'here
you
must
paste
your
configuration
file' > /etc/squid3/squid.conf"
sudo service squid3 restart
}
if ! [ "$(sudo which squid3)" ]; then
installer
add_user
else
add_user
fi
First run it install squid3 and apache2-utils (for htpasswd) and after make a new user.
If you run it again you can add more users.
Reading the sudo man page, I see that the -v flag can be used to check if the user has sudo privileges in his workstation. I have a piece of script that needs to test it. If the user has not sudo privileges, it prints on screen:
Sorry, user tester may not run sudo on debian.
How can I suppress this message and just execute the rest of the code?
Try to append >/dev/null in your command. In case the message is printed in stderr then use 2>/dev/null or as advised in comments use &>/dev/null to redirect both stdout and stderr to null.
Using sudo -l or --list
As per the man page, sudo can be used with -l or --list to get the list of allowed and forbidden commands for any particular user.
The syntax would be: sudo -l [-AknS] [-a type] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
If we use sudo --list without any arguments, then it will print the list of allowed and forbidden commands for the user who is executing the sudo command
sudo --list
User root may run the following commands on client:
(ALL) ALL
Depends on what you mean by "can user sudo"
Short answer:
If can_auto_sudo=$(sudo -l -n sudo &>/dev/null; echo $?) is 0, you can sudo as much as you want.
Long Answer
Do you need to test before or can you just handle error cases?
How much do you need to know, the sudoers real username is a valid piece of data to want, for example.
This question is often asking several different but related questions. So I will ask those more precisely and then answer for each.
1. Is this script being run using sudo?
[ $EUID -eq 0 ] || exit 1 # Exit if not effectively root
2. Can this user run a specific command as root using sudo?
sudo -l /usr/bin/program &>/dev/null || exit 2 # Exit if it can't run this as sudo
3. Can this user run sudo without interacting?
sudo -l -n /usr/bin/program &>/dev/null || exit 3 # Exit if requires interaction
4. Can I check all that ahead of time?
`sudo -ll -U $USER # tells you which commands can be runs with sudo by user (have to parse yourself)
5. Script being run with sudo or actually root?
[[ "$(printenv SUDO_USER)" = "" ]] || echo "$SUDO_USER is sudoing!" && exit 5
I have been looking everywhere for something to disable sudo access for this one command while maintaining sudo for the rest for the script. The script is called using sudo e.g. "sudo ./install.sh". The snippet in question is this (I currently was trying to make a new user to call the command, but to no use):
echo >&2 "Installing Homebrew"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
I only want line 2's command to run as not root/ no sudo. I am very much a noob, so any guidance/alternative methods not including making a new user, would be very appreciated.
If the script was run with sudo, then you can use $SUDO_USER to get the original username:
echo >&2 "Installing Homebrew"
sudo -u "$SUDO_USER" /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
(And don't use sudo on the other commands in the script -- if the script itself is run with sudo, that's completely redundant.)
BTW, this will fail completely if the script was not run with sudo -- for example, if someone logs in as root and runs it, uses su to switch to root and run it, etc. It might be better to check first, something like this:
if [ -n "$SUDO_USER" ]; then
echo >&2 "Installing Homebrew"
sudo -u "$SUDO_USER" /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
echo "No original user found to install Homebrew under" >&2
fi
I tried to execute the multiple commands in a single line using &&, but it isn't working in the following case. It just executes the first sudo command alone.
sudo /usr/bin/rootsh -i -u ditest && Set_proj && 1
Note: Only the first one is Sudo command and the remaining are a normal ones. I already tried using the following command. But it doesn't works
sudo /usr/bin/rootsh -i -u 'ditest && Set_proj && 1'
Multiple command doesn't works even though if I enter "Set_Proj && 1" after executing the first command. Only the first command is executed (Set_proj).
In other cases multiline commands are working fine. Eg: cd jtest && ls
Edited:
Its not a duplicate of how to run two commands in sudo?. I described clearly that I haven't used multiple sudo commands. Once logged into putty, the command sequence will be
jacob:/home/users/jacob: sudo /usr/bin/rootsh -i -u ditest
[ditest] ditest> Set_Proj ***List of projectes displayed***
> 1 ***Select any project***
simple. Put one sudo by command:
sudo apt-get update && sudo apt-get dist-upgrade -y
Usually, only the first sudo ask for password, andit cached for next sudos
Use bash -c, like this
sudo bash -c "ditest && Set_proj && 1"
If you want to run sudo as another user then,
sudo -H -u user bash -c "ditest && Set_proj && 1"