简体   繁体   中英

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

尝试

chroot /chroot_dir /bin/bash -c "su - -c ./startup.sh"
chroot /chroot_dir /bin/bash -x <<'EOF'
su -
./startup.sh
EOF

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM