What are the exit statuses of ssh command?

Posted on in Categories , , , , , last updated September 16, 2015

What are the exit statuses of ssh command on a Linux or Unix like system when you run ‘ssh host command‘?

SSH client is a program for logging into a remote machine and for executing commands on a remote machine. By default in Linux/UNIX if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not. This value is know as Exit Status of shell command or script.

The session terminates when the command or shell on the remote machine exits and all X11 and TCP/IP connections have been closed. The exit status of the remote program is returned as the exit status of ssh. You can use bash shell $? Variable to print exit status of shell. For example:
[email protected]: $ ssh [email protected]

[email protected]: $  exit
[email protected]: $ echo $?

In short,

  1. ssh exits with the exit status of the remote command which can be find with echo $? command.
  2. Or value 255 is return, if an error occurred while processing request via ssh session

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

4 comment

  1. Seeing as this is the top hit for googling “ssh return status”, I thought I’d go ahead and add that

    The remote command may also terminate violently due to a signal.
    Such a condition can be indicated by the following message. A zero
    ‘exit_status’ usually means that the command terminated successfully.
    uint32 recipient channel
    string “exit-signal”
    boolean FALSE
    string signal name (without the “SIG” prefix)
    boolean core dumped
    string error message in ISO-10646 UTF-8 encoding
    string language tag [RFC3066]

    So you don’t *always* get the return status of your program.

    Good post, though….it answered my question :)

Leave a Comment