Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: executing commands from within a chroot and switch user

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...

like image 294
dgrandes Avatar asked Nov 16 '11 20:11

dgrandes


2 Answers

try

chroot /chroot_dir /bin/bash -c "su - -c ./startup.sh"
like image 166
Michael Krelin - hacker Avatar answered Oct 04 '22 01:10

Michael Krelin - hacker


chroot /chroot_dir /bin/bash -x <<'EOF'
su -
./startup.sh
EOF
like image 24
garuse Avatar answered Oct 04 '22 01:10

garuse