Executing sudo command in bash script without displaying it - bash

I'm executing a command with sudo from bash script, and I'm wondering how to prevent sudo from displaying anything on the screen
echo "mypassword" | sudo -S cp -u /scripts/.bashrc ~/ > /dev/null 2>&1
The result will be an output displaying: [sudo] password for username:
I want to hide that output..
now, before the first comment;
This isn't the safest way, since you're entering your password into the script, but this is strictly internal servers.

Run sudo --help, we can get answer from the parameter list:
-p, --prompt=prompt use the specified password prompt
Then,
echo "mypassword" | sudo -S --prompt="" cp -u /scripts/.bashrc ~/ > /dev/null 2>&1
may do the trick.

Related

How to pass password to sudo from environment variable without prompt appearing?

How do I pass my password to sudo from an environment variable through stdin without the sudo prompt appearing?
I have tried $ echo $PASSWORD | sudo -S echo foo but that returns [sudo] password for mithic: foo.
Using the -n flag just always returns sudo: a password is required (unless I have recently inputted the correct password).
You can set an empty password prompt:
printf '%s\n' "$PASSWORD" | sudo -p "" -S echo foo
If it's really in the environment, I would recommend using the -A option instead of -S. Write a very small script that writes the value to standard output.
#!/bin/sh
printf '%s' "$PASSWORD"
Call it something like asker and make it executable
chmod +x asker
The do the following:
SUDO_ASKPASS=./asker sudo -A echo foo
-A makes sudo run the executable named by SUDO_ASKPASS and read the password from its output.

How to redirect to a file with a certain user for creating that file

Various times in my scripts I redirect output from the terminal to a file or something. Sometimes I'll specify a user for a command but that user doesn't work on the other side of the redirect.
sudo -i
# We are now the root user.
sudo -u abc echo 'Something...' > a_file.txt
Even though the echo is done as user abc the file will be created as root user.
I understand why this is happening, what I was hoping is that someone knows a way to make it work as desired so that the file a_file.txt gets created with the owner being user abc.
You can run multiple commands in a sudo shell and then all them are going to run under the same user:
sudo -u abc sh -c 'echo sth > a_file.txt'
Or you can use tee pipe:
sudo -u abc sthRequiringSudo | sudo -u abc tee a_file.txt >/dev/null
Or you can pass your commands to a sudo shell standard input:
sudo -u abc -s <<< "echo sth >a_file.txt"
You can even have the commands in multiple lines being sent to a sudo shell standard input:
sudo -u abc -s << EOF
echo sth >a_file.txt
echo sthelse >>a_file.txt
EOF

Update root crontab remotely for many systems by script

I am trying to update the crontab file of 1000+ systems using a for loop from jump host.
The below doesn't work.
echo -e 'pass365\!\n' | sudo -S echo 'hello' >> /var/spool/cron/root
-bash: /var/spool/cron/root: Permission denied
I do have (ALL) ALL in the sudoers file.
This is another solution;
echo 'pass365\!' | sudo -S bash -c 'echo "hello">> /var/spool/cron/root'
The below worked for me.
echo 'pass365\!' | sudo -S echo 'hello' | sudo -S tee -a /var/spool/cron/root > /dev/null
Problem 1: You are trying to send the password via echo to sudo.
Problem 2: You can't use shell redirection in a sudo command like that.
Between the two of these, consider setting up ssh public key authorization and doing
ssh root#host "echo 'hello' \>\> /var/spool/cron/root"
You may eventually get sudo working but it will be so much more pain than this.

How to execute chsh using a bash script I put up in github?

I have a gist that I always use to install the packages I need on a fresh server.
http://gist.github.com/4372049
All I need to do is to type the following in the fresh server via ssh
bash -c "$(curl -fsSL https://raw.github.com/gist/4372049)" <mysqlPassword>
I will be good to go.
Now I have this series of steps I always need to perform on a fresh installation of ubuntu.
first at root i did a
echo $SHELL
I saw that I have /bin/bash
then i switch to www-data
sudo su www-data
then i do a
echo $SHELL
I saw that I had
/bin/sh
instead.
So I did a
chsh -s /bin/bash
I was prompted for my www-data password so I gave it.
Password:
after that I switch back to root
exit
then i log back into www-data
sudo su www-data
I checked the $SHELL again
echo $SHELL
I saw that now it is
/bin/bash
listed here in https://askubuntu.com/a/232663/10591
Is there a way to write a bash script I can put up in gist.github.com to use in a similar way to execute?
if so, how do I write the bash script?
UPDATE:
I realized that I was given a vote to close this question because it was deemed too localized.
Let me rephrase this to
how do I write a bash script that I can put up in gist and use it in my linux console such that it can take in arguments for username and password and therefore execute the command
chsh -s /bin/bash
and supplying the password correctly?
This is my attempt: https://gist.github.com/simkimsia/5126919
the su worked, but not the chsh command
Update 2:
I have changed the script to be
EXPECTEDARGS=1
if [ $# -ne $EXPECTEDARGS -o "x$0" == "x" -o $0 == "bash" ]; then
echo "Usage:"
echo " Parameter 1: your username"
echo " Parameter 2: your password"
exit 1
fi
CHANGESHELL_FOR_USER=$0
PASSWORD_OF_USER=$1
########################################
## switch to another user
## read this https://stackoverflow.com/a/1988255/80353
########################################
sudo -u $CHANGESHELL_FOR_USER -H sh -c "chsh -s /bin/bash"
expect "*?assword:*" {send -- "$PASSWORD_OF_USER\r";}
expect eof
after reading how to use a shell script to supply a password when the interface asks for it
and
https://stackoverflow.com/a/1988255/80353
Now the problem is somehow sending the password when prompted for
Password:
As long as you are running the below command as root, you will be fine.
chsh -s /bin/bash <username>
in this case, it is
chsh -s /bin/bash www-data
See https://gist.github.com/simkimsia/4372049#file-installation-12-10-ubuntu-sh-L373

Sudo not prompting for password Mac bash script

I am trying to write a script that appends a line to the /etc/hosts, which means I need sudoer privileges. However, if I run the script from the desktop it does not prompt for a password. I simply get permission denied.
Example script:
#!/bin/bash
sudo echo '131.253.13.32 www.google.com' >> /etc/hosts
dscacheutil -flushcache
A terminal pops up and says permission denied, but never actually prompts for the sudo password. Is there a way to fix this?
sudo doesn't apply to the redirection operator. You can use either echo | sudo tee -a or sudo bash -c 'echo >>':
echo 131.253.13.32 www.google.com | sudo tee -a /etc/hosts
sudo bash -c 'echo 131.253.13.32 www.google.com >> /etc/hosts'
What you are doing here is effectively:
Switch to root, and run echo
Switch back to yourself and try to append the output of sudo onto
/etc/hosts
That doesn't work because you need to be root when you're appending to /etc/hosts, not when you're running echo.
The simplest way to do this is
sudo bash -c "sudo echo '131.253.13.32 www.google.com' >> /etc/hosts"
which will run bash itself as root. However, that's not particularly safe, since you're now invoking a shell as root, which could potentially do lots of nasty stuff (in particular, it will execute the contents of the file whose name is in the environment variable BASH_ENV, if there is one. So you might prefer to do this a bit more cautiously:
sudo env -i bash -c "sudo echo '131.253.13.32 www.google.com' >> /etc/hosts"

Resources