Run a command as su - through ssh through a bash script - linux

Hi so basicly what I am trying to do is run a script that is located on the remote server as root by running su - before running the script.
I need to run the script after running the command su - otherwise it wont run. And at the state that the server will be sudo isnt installed yet.
Right now I have ssh -t $USERNAME#$IP "su - && /home/$USERNAME/firstboot/firstboot.sh" but it connects me as su - on the remote server but dosnt execute the script.
Any idea would be greatly apreciated thank you.

Right now I have ssh -t $USERNAME#$IP "su - && /home/$USERNAME/firstboot/firstboot.sh" but it connects me as su - on the remote server but dosnt execute the script.
Fundamentally, you can't use su like that. Forget ssh for a moment; if you run su - && date, the date command will only execute after the su shell has exited (because command1 && command2 means "run command1 first, and if it exits successfully, run command2).
You need to use the -c option to su if you want to run a command in the su environment:
su - -c date
In your script, that means you want to run:
su - -c /home/$USERNAME/firstboot/firstboot.sh
So your ssh command should look like:
ssh -t $USERNAME#$IP su - -c /home/$USERNAME/firstboot/firstboot.sh

Related

ssh-add on command argument to su

I want to start a docker container that adds a ssh key at startup :
My entrypoint looks like this :
#!/bin/bash
set -e
service ssh start
su anotherUser -s /bin/bash -c "eval \"$(ssh-agent)\" && ssh-add /Keys/id_rsa"
I've seen many posts that use sudo, but I do not have sudo available. I've found this solution but at the startup it shows me :
[....] Starting OpenBSD Secure Shell server: sshd 7[ ok 8.
Agent pid 36
Error connecting to agent: Permission denied
But when I execute the same lines at the promp everythings is ok :
xxx# su anotherUser
anotherUser#xxx:~$ eval $(ssh-agent)
Agent pid 47
anotherUser#xxx:~$ ssh-add /keys/id_rsa
Identity added: /keys/id_rsa (yyy#yyy-HP-EliteBook-850-G4)
You are running ssh-agent before su runs. The $ needs to be escaped so that the literal command substitution is passed to bash for execution.
su anotherUser -s /bin/bash -c 'eval $(ssh-agent) && ssh-add /Keys/id_rsa'
(Untested; probably needs more details about how the container is run and why ssh-add needs to be run as a different user.)
It may be simpler, though, to run your entry point with ssh-agent. For example,
# In the Dockerfile...
ENTRYPOINT ["ssh-agent", "entry.sh"]
Inside entry.sh, your environment will already have access to the agent.
#!/bin/bash
set -e
service ssh start
su anotherUser -s ssh-add /Keys/id_rsa

Commands will not pass to CLI after logging into new user with sudo su - user

Obligatory 'first post' tag. Issue: Commands will not pass to command line after entering password for a sudo su - userB
I am writing a script in bash that requires to be run as a specific user. Ideally we would like this script to be able to be run on our local workstations for ease of use. Here is the command I am running to test:
ssh -qt -p22 userA#hostname "whoami; sudo su - userB; whoami"
Expected:
userA
[sudo] password for userA:
userB
With this command I am able to get the prompt for sudo password but once it is entered I am presented with a normal terminal where I can manually run commands. Once I ctrl-D/exit it runs the second whoami as the userA and closes. I work in an extremely jailed environment so sudo su -c and similar "run-as-root" commands do not work and I cannot ssh directly to userB.
Is there any way to send the commands to userB by logging in with sudo su - userB?
su creates a subshell that reads the commands from standard input by default. It executes whoami after that exits. You can use the -c option to pass a command to it.
ssh -qt -p22 userA#hostname "whoami; sudo su - userB -c 'whoami'"
You can also use the -u option to sudo instead of using su:
ssh -qt -p22 userA#hostname "whoami; sudo -u userB whoami"

How can I Enter into postgres bash command line and exit using shell script?

I'm beginner in writing script in linux. Can anyone please help!
From terminal I enter into postgres bash command line by typing:
[root#localhost Desktop]# su - postgres
-bash-3.2$
then I can create user,db from there and I can exit by typing "exit" command.
How can I do this by script? I've written a scrip but that does enter into bash but doesn't run the commands. I want to enter into bash, create a user & db then exit. My scripts concept is given below:
#!/bin/bash
createuser -P -s -e asterisk
createdb --owner=asterisk asterisk2
You should run your script this way:
su -c myscript.sh postgres
That is, run myscript.sh (or whatever it's called) as the postgres user. No need for an interactive prompt, which is what - was giving you.
Try this:
su postgres -c "createuser -P -s -e asterisk && createdb --owner=asterisk asterisk2"

su command in shell script

I have a script which copies a file, then untar and install it (agent-service) on multiple systems (IPs are read from systems.txt file). In the script, I wanted to start the agent-service as user "test". However after the script execution, when I check the target system, the agent-service is shown as running as "root" user. What could be wrong here? Am I not using su command correct within the script?
~]# ps -ef | grep agent-service
root 23511 15196 0 02:12 pts/3 00:00:00 agent-service
Script>
#!/bin/bash
export AGENT=linux-5.8.1.tar.gz
while read host; do
scp $AGENT root#$host:/opt
ssh -n root#$host 'cd /opt/linux;
tar zxvf linux-5.8.1.tar.gz;
mkdir /opt/hyperic;
useradd -m test;
chown -R test:test /opt/linux;
su - test;
/opt/linux/agent-service start'
done < systems.txt
Using su as you do here spawns a new shell that has nothing to do thus exits immediately.
Either pass the command to su:
su - test -c /opt/linux/agent-service start
Or use sudo in a similar manner:
sudo -u test /opt/linux/agent-service start

ssh command to run remote script exist shell on remote server when switching user

When I run a script such as this:
ssh -t root#10.10.10.10 '/tmp/somescript.sh'
where the script is defined as:
#!/bin/sh
mkdir -p /data/workday/cred
chown -R myuser:myuser /data
su myuser - # <------- NOTICE THIS ! ! ! !
rpm -Uvp --force --nodeps --prefix /data/place /data/RPMs/myrpm.rpm
Notice the above su command.
If I comment-out the su command, the script runs remotely and then my shell prompt returns to where I came from ( same server where I ran the ssh command above )
But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server.
How can I prevent that ? Making sure that the issuer of the rpm command is a different user than root just a listed ?
But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server.
Not exactly. The script is running up to the su command, which spawns a new subshell, and stopping there until you exit the shell. Until you exit that shell, the rpm command never runs, and when it does, it runs as root.
If you want to run the rpm command as a non-root user, you'd need to do something a little different, like:
sudo -u myuser rpm -Uvp ...
add 'exit' at the end of your script

Resources