SSH Commands – history

On this page
  1. How the command works
  2. Re-running previous commands
  3. Searching history
  4. Clearing command history
  5. Practical use cases
  6. Practical implications
  7. Summary

The history command is used to display a list of previously executed commands in an SSH session. It helps you review, reuse, or track commands that were run on the server.

This is useful when working with long command sequences or when you need to repeat a previous action without retyping it.

How the command works

The basic structure is:

history

This displays a numbered list of commands executed in the current user session.

Re-running previous commands

You can re-execute a command directly from the history list using its number:

!123

This runs the command with ID 123 from the history output.

Searching history

You can filter results using grep:

history | grep "ssh"

This shows only commands that contain the word ssh.

Clearing command history

To clear the current session history:

history -c

This removes the recorded commands for the current session.

Practical use cases

The history command is commonly used for:

  • repeating frequently used commands
  • auditing recent server actions
  • troubleshooting command sequences
  • improving efficiency in SSH workflows

Practical implications

Command history is stored per session and may vary depending on system configuration. Closing the session may reset the visible history, while some systems persist history in user configuration files.

Be cautious when re-running commands using !number, especially on production systems, as the command executes immediately without confirmation.

Summary

The history command displays previously executed SSH commands and allows quick reuse of them. It is a practical tool for improving efficiency and tracking actions in a Linux environment.

Didn't find an answer?

Our support team is here for you 24/7 – write to us and we will help you sort it out.