21

Im writing a script that should do this...

chroot /chroot_dir/ su -
./startup.sh (This should run within the su environment)

I have tried this approach:

chroot /chroot_dir /bin/bash -c " su -; ./startup.sh"

This tries to execute the user switching and the script as a string command to bash...however what it does, is it "stops" after "su -" and doesnt execute the script. However, once I leave the "su -" environment, it does try to run startup.sh but of course, it cant find it.

Basically I need to nest the "startup.sh" to be run inside the "su -" environment...

3 Answers 3

46

try

chroot /chroot_dir /bin/bash -c "su - -c ./startup.sh"
Sign up to request clarification or add additional context in comments.

1 Comment

thanks so much, I dont know how I didnt see this in the man page!
31
chroot /chroot_dir /bin/bash -x <<'EOF'
su -
./startup.sh
EOF

1 Comment

Nice syntax, this one.
10

basic option:

cat << EOF | chroot /chroot_dir 
touch aaaaa
touch bbbbb
EOF

option with different shell (eg. if using bash but in chrooted enviroment it doesn't exists)

cat << EOF | chroot /chroot_dir /bin/sh
touch aaaaa
touch bbbbb
EOF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.