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.
Table of Contents
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.