SSH Session Never Closes During apt-get Install – Troubleshooting

aptdebiansshUbuntu

Problem

When running apt-get install in an non-interactive SSH session, the session never closes. Example:

ssh user@target "sudo apt-get -y install my_package"

The my_package does get installed properly, but the SSH session just dangles open.

Question

Is there any flag to pass SSH to get apt-get to work?


Additional Information

Context

Remote installation is used for automated deployment of a package on an integration server. As soon as we push some code changes to a repository, a job pulls in the code, builds the package, and deploys it on integration to check that everything works well (as far as deployment is concerned).

Already Tried & Notes

  • The same SSH session executing apt-get update closes cleanly. Note that apt-get update is not interactive, whereas apt-get install is. This may suggest that interactivity is an issue.
  • A command like ssh user@target "sudo apt-get install my_package && echo Hello" never reaches the echo.
  • debconf complains that it cannot find a nice frontend (Display, Readline), and it falls back to Teletype (although Readline is available).
  • In relation to debconf's frontend, passing -t to force TTY with SSH does not help. Neither DEBIAN_FRONTEND=noninteractive.
  • All was done on Ubuntu 12_04 LTS.

Best Answer

The following answer on SF did the trick:

ssh fails to execute remote command when run from cron bash script

The -t flag forces a pseudo-tty allocation, except perhaps when there is no TTY locally. But passing the flag twice like in -t -t just pretends to do it. And that solved the problem.

See the SSH documentation:

-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Now, why did that work? It turns out that debconf does not complain anymore about the frontend in the logs. So I believe that the double -t sets (lures?) debconf as needed, which allows apt-get install completion to end the SSH session cleanly.