Repeated SSH connections for account checks or status reports often begin with the same command. PuTTY can store that remote command in a saved session so it runs automatically after login.

PuTTY retaining the output of an automatically executed remote command

When a remote command is configured in PuTTY, the client requests command execution from the SSH server after authentication instead of starting a normal interactive shell. The server runs the command under the authenticated account, returns the output to the terminal window, and ends the session when the command finishes.

Remote commands often run with a different environment than an interactive login, so PATH and shell startup files may not behave as expected. Commands that prompt for input or require a proper terminal (including some sudo workflows) can fail or hang, and fast commands can close the window immediately when they exit.

Steps to run a remote command automatically after connecting in PuTTY:

  1. Enter your SSH server's hostname in Host Name (or IP address) under Session. PuTTY Session with the SSH host field ready for entry

    An SSH server and an account that already authenticate successfully with PuTTY are required. The example uses server.example.com on port 22. If your server uses another SSH port, enter it in Port.

  2. Open Connection → SSH in the category tree. PuTTY Session showing the SSH category beneath Connection
  3. Enter your command in Remote command. PuTTY SSH settings with the Remote command field empty

    The id command prints the remote account's user ID and groups on a Unix-like server:

    /bin/sh -c 'id'

    A saved remote command runs on every connection using that profile, so a destructive command can repeatedly delete data or change configuration unexpectedly.

  4. Return to Session in the category tree. PuTTY SSH settings with the remote command entered and Session visible in the tree
  5. Select Never under Close window on exit. PuTTY Session showing the Close window on exit options

    This keeps a fast command's output visible after the remote session ends.

  6. Enter a name such as my-server under Saved Sessions. PuTTY Session with Never selected and the saved-session name field empty
  7. Click Save to store the command in that session. PuTTY Session with my-server entered beside the Save button
  8. Click Open to connect with the saved command. PuTTY Session with my-server saved and the Open button available

    Normal SSH authentication still applies; the command starts only after authentication succeeds.

  9. Dismiss the Connection closed by remote host notice with OK if it appears. PuTTY displaying automatic command output and the connection-closed notice
  10. Confirm the command output appears without typing a command in the terminal. Inactive PuTTY session retaining the automatically returned account identity
    uid=911(abc) gid=911(abc) groups=911(abc),1000(users)

    The example output identifies the remote abc account; your account's IDs and groups will differ. PuTTY (inactive) indicates that the session has ended, while Never retains its output.

Share this guide