When I chroot while logged in using a ssh session. How do I get back to the chroot environment when the ssh terminal session ends. When I ssh back in I'm back on the non chroot environment. While I'm in a chroot session and I start a process. I then exit out of the ssh session. Later I want to go back to the chroot environment and stop the process and restart it. Is it just a simple as chroot again to the previous chroot folder?



When you "exit out of the ssh session", you send the process you're connected to a SIGHUP (Hangup) signal (from man 7 signal):

   Signal     Value     Action   Comment
   SIGHUP        1       Term    Hangup detected on controlling terminal
                                 or death of controlling process

When this SIGHUP is seen by the process on the server, it terminates. :-(

There are a couple ways of working around this feature:

  1. Start your chroot environment on the server with the nohup (man nohup says nohup — invoke a utility immune to hangups).
  2. Suspend the local end of your ssh session with ~CtrlZ after an Enter, background it with bg, connection intact, no SIGHUPs. Bring it back into the foreground with fg. See man bash especially the sections on SIGNALS and JOB CONTROL. If you terminate the ssh session, SUGHUP ensues.
| improve this answer | |
  • I'm curious if I were to start the sshd in the chroot environment. Then I should always be in the chroot environment when I ssh into the device. What I'm trying to do is setup a NAS to run off of a USB drive. When running from disk it wakes up the disk to often. When running from the USB it almost never wahes up unless you are actually accessing your data. – rac8006 May 23 '17 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.