What are the exit statuses of ssh command?

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

Tutorial details
Difficulty level Easy
Root privileges No
Requirements ssh/bash+ksh+zsh
Est. reading time 1m
[/donotprint]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:
user@localhost: $ ssh user@somewhere-com

user@somewhere-com: $  exit
user@localhost: $ 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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Rusty Jan 30, 2009 @ 17:21

    Very well, but what ARE the exit stati of the ssh command?
    e.g, 0 if succesful? Or, 1?

  • wyrmmage Jul 16, 2009 @ 0:02

    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 :)

  • Whatever Sep 16, 2011 @ 12:02

    My SSH version returns 0 for okay, and 255 if connection failed.

  • josh Jun 15, 2012 @ 9:23

    if you looking for a way to return failure from a remote bash script, try:

    kill $$

    this causes bash to kill itself ($$ evaluates to bash’s pid) and ssh to return 255.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum