switch to root account and execute cd /root - shell

below is code preview
function f1{
sudo su -
cd /root
yum install expect -y
wget <some url>
}
ssh ec2-user#<ip> -i <key> "$(typeset -f f1); f1"
when I am running this script. It is hanging. I think it is not switching to root and executing rest of lines.
Can somebody help me to make it work.

Related

How to Start ssh-agent in Dockerfile

When entering my container, I want to log in as user ryan in directory /home/ryan/cas with the command eval "$(ssh-agent -c)" run. My following Dockerfile:
FROM ubuntu:latest
ENV TZ=Australia/Sydney
RUN set -ex; \
# NOTE(Ryan): Prevent docker build hanging on timezone confirmation
ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone; \
apt update; \
apt install -y --no-install-recommends \
sudo ca-certificates git gnupg openssh-client vim; \
useradd -m ryan -g sudo; \
printf "ryan ALL=(ALL:ALL) NOPASSWD:ALL" | sudo EDITOR="tee -a" visudo; \
# NOTE(Ryan): Prevent sudo usage prompt appearing on startup
touch /home/ryan/.sudo_as_admin_successful; \
git clone https://github.com/ryan-mcclue/cas.git /home/ryan/cas; \
chmod 777 -R /home/ryan/cas;
ENTRYPOINT ["/bin/bash", "-l", "-c"]
USER ryan
WORKDIR /home/ryan/cas
CMD eval "$(ssh-agent -s)"
However, running ssh-add I still get the Could not open a connection to your authentication agent which is indicative that the ssh-agent is not running. Manually typing eval "$(ssh-agent -c)" works.
I think you want remove your ENTRYPOINT statement, and then you want:
USER ryan
WORKDIR /home/ryan/cas
CMD ["ssh-agent", "bash", "-l"]
This will get you a login shell, run under the control of ssh-agent (so you'll have the necssary SSH_* environment variables and an active socket available).
To understand what's happening with your container, try running from the command line:
bash -l -c 'eval $(ssh-agent -s)'
What happens? The shell exits immediately, because running ssh-agent -s causes the agent to background itself, which looks pretty much the same as "exiting". Since you passed the -c flag, and the command given to -c has exited, the parent bash shell exits as well.

How do I write a Dockerfile which changes a protected text file in the docker image?

I have a Dockerfile that follows this pattern:
RUN echo "[DOCKER BUILD] Installing image dependencies..." && \
apt-get update -y && \
apt-get install -y sudo package_names ...
RUN useradd -ms /bin/bash builder
# tried this too, same error
# RUN useradd -m builder && echo "builder:builder" | chpasswd && adduser builder sudo
RUN mkdir -p /home/builder && chown -R builder:builder /home/builder
USER builder
RUN sudo sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf
The part that doesn't work is editing /etc/nsswitch.conf ... Why can't I configure my image to edit this file?
I've tried tweaking the useradd several different ways but the current error is:
Step 8/10 : RUN sudo sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf
---> Running in 97cd39584950
sudo: no tty present and no askpass program specified
The command '/bin/sh -c sudo sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf' returned a non-zero code: 1
How do I achieve editing this file inside the image?
A comment here suggests that all operations in dockerfile should be being run as root, which leads me to believe sudo is not needed. Why then do I see this?
RUN sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf
Step 8/10 : RUN sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf
---> Running in ad56ca17944c
sed: couldn't open temporary file /etc/sed8KGQzP: Permission denied
The problem is on the password for sudo, or a request for password. You need to pass ENV_VARIABLES to your container related with removing the sudo request for password, as follows:
<your-container-user> ALL = NOPASSWD: /sbin/poweroff, /sbin/start, /sbin/stop
You need to execute your sudo freely.
Related question:
How to fix 'sudo: no tty present and no askpass program specified' error?
I figured it out -- I can perform this task without sudo, but only if I do it before calling USER builder. It seems docker has the correct access it needs before I create any users.

Failed to Call Access Method Exception when Creating a MedicationOrder in FHIR

I am using this http://fhirtest.uhn.ca/baseDstu2 test FHIR server and it worked okay so far.
Now I am getting an HTTP-500 - Failed to Call Access Method exception.
Anyone has any idea on what has gone wrong?
This happens frequently. Probably because someone tested weird queries or similar that put the server in an unstable status.
I suggest posting a comment in https://chat.fhir.org/#narrow/stream/hapi to get the server restarted,
or install http://hapifhir.io/doc_cli.html which does basically the same but you have full control.
I built a Dockerfile:
FROM debian:sid
MAINTAINER Günter Zöchbauer <guenter#yyy.com>
ENV DEBIAN_FRONTEND noninteractive
RUN \
apt-get -q update && \
DEBIAN_FRONTEND=noninteractive && \
apt-get install --no-install-recommends -y -q \
apt-transport-https \
apt-utils \
wget \
bzip2 \
default-jdk
# net-tools sudo procps telnet
RUN \
apt-get update && \
rm -rf /var/lib/apt/lists/*
https://github.com/jamesagnew/hapi-fhir/releases/download/v2.0/hapi-fhir-2.0-cli.tar.bz2 && \
ADD hapi-* /hapi_fhir_cli/
RUN ls -la
RUN ls -la /hapi_fhir_cli
ADD prepare_server.sh /hapi_fhir_cli/
RUN \
cd /hapi_fhir_cli && \
bash -c /hapi_fhir_cli/prepare_server.sh
ADD start.sh /hapi_fhir_cli/
WORKDIR /hapi_fhir_cli
EXPOSE 5555
ENTRYPOINT ["/hapi_fhir_cli/start.sh"]
Which requires in the same directory as the Dockerfile
prepare_server.sh
#!/usr/bin/env bash
ls -la
./hapi-fhir-cli run-server --allow-external-refs &
while ! timeout 1 bash -c "echo > /dev/tcp/localhost/8080"; do sleep 10; done
./hapi-fhir-cli upload-definitions -t http://localhost:8080/baseDstu2
./hapi-fhir-cli upload-examples -c -t http://localhost:8080/baseDstu2
start.sh
#!/usr/bin/env bash
cd /hapi_fhir_cli
./hapi-fhir-cli run-server --allow-external-refs -p 5555
Build
docker build myname/hapi_fhir_cli_dstu2 -t . #--no-cache
Run
docker run -d -p 5555:5555 [image id from docker build]
Hope this helps.

Can't execute script from within rc.local

Within rc.local I have
sudo -H -u myUser -s -- "cd /home/myUser/parlar && /usr/local/bin/meteor &"
I want to test it but when I execute that with
myUser:~$ sudo service rc.local start
/bin/bash: cd /home/myUser/parlar && /usr/local/bin/meteor &: No such file or directory
If I execute the command
cd /home/myUser/parlar && /usr/local/bin/meteor &
it works
How can I execute rc.local so that it changes into the relevant directory, and runs the command as the requested user?
Whatever arguments you give to sudo after -- are considered as command & its arguments.
There is no command/executable named "cd /home/myUser/parlar && /usr/local/bin/meteor". You can however, start bash & run the command within that bash shell.
e.g.
sudo -H -u myUser -s -- bash -c "cd /home/myUser/parlar && /usr/local/bin/meteor &"
Since the first command is cd, this alternate approach may also work:
sudo -H -u myUser -s -i PWD=/home/myUser/parlar -- /usr/local/bin/meteor
To see the log of rc.local itself, it's better to run these commands:
**systemctl restart rc-local.service**
**systemctl status rc-local.service**
May be it can be help full for better trouble shooting

How to start multiple processes for a Docker container in a bash script

I found very strange behaviour when I build and run docker container. I would like to have container with cassandra and ssh.
In my Dockerfile I've got:
RUN echo "deb http://www.apache.org/dist/cassandra/debian 20x main" | sudo tee -a /etc/apt/sources.list
RUN echo "deb-src http://www.apache.org/dist/cassandra/debian 20x main" | sudo tee -a /etc/apt/sources.list
RUN gpg --keyserver pgp.mit.edu --recv-keys 4BD736A82B5C1B00
RUN apt-key add ~/.gnupg/pubring.gpg
RUN apt-get update
RUN apt-get -y install cassandra
And then for ssh
RUN apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo '{{ docker_ssh_user }}:{{docker_ssh_password}}' | chpasswd
EXPOSE 22
And I added start script to run everything I want:
USER root
ADD start start
RUN chmod 777 start
CMD ["sh" ,"start"]
And here comes problem. When I have start like this below:
#!/bin/bash
/usr/sbin/sshd -D
/usr/sbin/cassandra -f
SSH is working well. I can do ssh root#172.17.0.x. After I log in container I try to run cqlsh to ensure that cassandra is working. But cassandra is not started for some reason and I can't access cqlsh. I've also checked /var/log/cassandra/ but it was empty.
In second scenario I change my start script to this:
#!/bin/bash
/usr/sbin/sshd -D & /usr/sbin/cassandra/ -f
And I again try to connect ssh root#172.17.0.x and then when I run cqlsh inside container I have connection to cqlsh.
So I was thinking that ampersand & is doing some voodoo that all works well ?
Why I can't run bash staring script with one command below another?
Or I'm missing something else??
Thanks for reading && helping.
Thanks to my friend linux guru we found the reason of error.
/usr/sbin/sshd -D means that -D : When this option is specified, sshd will not detach and does not become a deamon. This allows easy monitoring of sshd
So in the first script sshd -D was blocking next command to run.
In second script I've got & which let sshd -D go background and then cassandra could start.
Finally I've got this version of script:
#!/bin/bash
/usr/sbin/sshd
/usr/sbin/cassandra -f

Resources