Why a command goes missing
You need that long command you used yesterday to inspect the nginx log, and Ctrl+R replies
(failed reverse-i-search). With Ubuntu’s defaults, several things cause that:
- You ran it in another SSH window that’s still open. Bash keeps a session’s commands in memory and writes
them to
~/.bash_historyonly on exit. - The command aged out. Ubuntu keeps 2,000 lines in the file, but only 1,000 in session memory where Ctrl+R
looks. Past 1,000, Ctrl+R can’t see it even though
grepfinds it in the file; past 2,000, Bash trims it from the file on login. - The line started with a space. Bash skips those — a feature meant for hiding passwords, which also catches indented commands pasted from a guide.
- The session was killed:
kill -9, an out-of-memory kill, or a power reset. Commands that never reached the file are gone. Closing a window or dropping the connection doesn’t lose history: Bash catches SIGHUP and saves before exiting. - You ran it inside
sudo -i. Root has its own history file,/root/.bash_history.
ssh web1 'docker ps' never enters the server’s history at all: a command passed after the host runs in a
non-interactive Bash, where history is off. The line stays in your local machine’s history.
How Bash stores history
session start ~/.bash_history ──► memory: last HISTSIZE commands
each command ──► memory, skipping leading-space lines and consecutive duplicates
session exit new session commands ──► appended to ~/.bash_history
file trimmed to HISTFILESIZE lines
Ctrl+R, Up Arrow, and history all read from session memory. The file is what future sessions inherit.
For a new user, Ubuntu puts these lines in ~/.bashrc:
| Line | What it does |
|---|---|
HISTCONTROL=ignoreboth |
skips lines starting with a space and consecutive duplicates; a command repeated after another command is recorded |
shopt -s histappend |
appends to the file on exit instead of overwriting it |
HISTSIZE=1000 |
how many commands stay in session memory |
HISTFILESIZE=2000 |
how many lines stay in the file |
Root’s /root/.bashrc has the same setup, written as HISTCONTROL=ignoredups:ignorespace — the same as
ignoreboth spelled out. Timestamps are off because HISTTIMEFORMAT isn’t set.
Searching for a command
Ctrl+R
Press Ctrl+R and type part of the command. Bash shows the most recent match:
(reverse-i-search)`nginx': systemctl status nginx
| Keys | What they do |
|---|---|
| Ctrl+R again | jump to the next older match |
| Enter | run the shown command |
| Right Arrow | drop the command onto the prompt to edit it first |
| Ctrl+G | cancel search and return to your previous line |
When no older matches remain, the prompt switches to failed while keeping the last match visible:
(failed reverse-i-search)`nginxzzz': systemctl status nginx
If you skip past the command you wanted, Ctrl+S searches forward toward newer entries. By default, though,
the terminal uses Ctrl+S to pause output: the screen freezes while your keystrokes queue up until you press
Ctrl+Q. Hand Ctrl+S over to history search by putting stty -ixon in ~/.bashrc or ~/.bash_aliases. After
that, Ctrl+S shows (i-search).
All matches at once
history | grep nginx
grep -n 'nginx' ~/.bash_history
history | grep searches session memory and prints entry numbers. grep on the file also finds entries that
fell out of Ctrl+R’s memory window and commands saved by other sessions.
Configure history in ~/.bashrc
Keep most personal settings in ~/.bash_aliases (Bash aliases), but history size doesn’t work there. The
HISTFILESIZE=2000 line in ~/.bashrc trims the file the moment Bash executes it — on every login, before
~/.bash_aliases is read. Tested on a 3,000-line file: putting HISTFILESIZE=100000 in ~/.bash_aliases
still leaves 2,000 lines after login, while changing the line inside ~/.bashrc keeps all 3,000. Edit
Ubuntu’s lines in place.
HISTCONTROL=ignoreboth
shopt -s histappendUbuntu already ships these two lines. Without histappend, a session longer than HISTSIZE commands overwrites the file on exit and wipes out commands from other windows.
HISTTIMEFORMAT='%F %T 'Adds date and time to each entry: 3001 2026-11-11 17:36:56 systemctl status nginx. Placed above HISTFILESIZE on purpose — see “If it doesn’t work” for why.
HISTSIZE=20000
HISTFILESIZE=20000Matching sizes for memory and file mean Ctrl+R can find everything stored on disk. The cost is RAM per open Bash process: 20,000 commands take about 9 MB instead of 5 MB; 100,000 take about 24 MB.
PROMPT_COMMAND+=('history -a')Runs history -a after every command so it hits disk right away: a new SSH window sees it immediately, and kill -9 can’t wipe it. Using +=( ) matters because Ubuntu 26.04 defines PROMPT_COMMAND as an array holding a systemd hook (/etc/profile.d/80-systemd-osc-context.sh); += appends without touching it.
Back up ~/.bashrc
cp ~/.bashrc ~/.bashrc.bakUpdate the lines in one command
sed -i -e "s/^HISTSIZE=1000$/HISTTIMEFORMAT='%F %T '\nHISTSIZE=20000/" \ -e "s/^HISTFILESIZE=2000$/HISTFILESIZE=20000\nPROMPT_COMMAND+=('history -a')/" \ ~/.bashrcsedfinds Ubuntu’sHISTSIZE=1000andHISTFILESIZE=2000lines, updates the numbers, insertsHISTTIMEFORMATbefore the first, and putsPROMPT_COMMANDafter the second. If those lines were already edited earlier,sedwon’t match and leaves the file untouched — edit~/.bashrcinnanoto match the block above.Check the lines
grep -nE '^(HIST|shopt -s histappend|PROMPT_COMMAND)' ~/.bashrc # 13:HISTCONTROL=ignoreboth # 16:shopt -s histappend # 19:HISTTIMEFORMAT='%F %T ' # 20:HISTSIZE=20000 # 21:HISTFILESIZE=20000 # 22:PROMPT_COMMAND+=('history -a')Restart the shell
exec bashBash replaces itself with a new shell that loads the updated config, saving the current session’s history first. Other open windows pick up the changes on their next login.
Do the same for root
sudo cp /root/.bashrc /root/.bashrc.bak sudo sed -i -e "s/^HISTSIZE=1000$/HISTTIMEFORMAT='%F %T '\nHISTSIZE=20000/" \ -e "s/^HISTFILESIZE=2000$/HISTFILESIZE=20000\nPROMPT_COMMAND+=('history -a')/" \ /root/.bashrc/root/.bashrchas the exact sameHISTSIZE=1000andHISTFILESIZE=2000lines, so the same command works with only the path changed.
Shared history across windows
With history -a, each command lands on disk right away, yet an already open window still reads the file
only at login: Ctrl+R in an older window won’t see a command run in another window. If you want live sharing,
have each window reload the file before every prompt. Replace PROMPT_COMMAND+=('history -a') with:
PROMPT_COMMAND+=('history -a; history -c; history -r')
history -a writes your new commands, history -c clears session memory, and history -r reads the whole
file back in. There are two trade-offs. Pressing Up Arrow walks through commands from every window in the
order they were saved, mixing another window’s commands into yours. And Bash re-reads the entire file before
every prompt: with 100,000 lines, that adds 55 ms after each command.
Guides often recommend the shorter history -a; history -n, where -n reads only lines the session hasn’t
seen yet. Tested across two windows, the first window never picked up the second window’s commands and read
its own last command twice. -n tracks how many lines N it has already read from the file, and -a bumps N
by the lines it appends — so commands written earlier by the other window fall under N, while your own lines
get read a second time.
| If… | Use |
|---|---|
| each window keeps its own Up Arrow history, and new windows see everything | immediate write: history -a |
| you need another window’s command right away and mixed Up Arrow history is fine | write and reload: the line above |
Beyond Ctrl+R: fzf and Atuin
With fzf, Ctrl+R opens an interactive list showing all matches at once: pick one with arrow keys, press Enter
to place it on the prompt, press Enter again to run it. Characters don’t need to be adjacent, only in order:
jungx matches journalctl -u nginx --since today. Ubuntu ships fzf as a package:
sudo apt install fzf
Add one line to ~/.bashrc or ~/.bash_aliases:
source /usr/share/doc/fzf/examples/key-bindings.bash
This works with both fzf 0.44 on Ubuntu 24.04 and fzf 0.67 on 26.04. eval "$(fzf --bash)" from fzf’s docs
fails on 0.44 because that flag didn’t exist yet. Alongside Ctrl+R, fzf binds Ctrl+T (pick a file and insert
its path) and Alt+C (jump to a directory). fzf searches the same session memory as Ctrl+R, so with
HISTSIZE=1000 it still sees only the last thousand commands.
Atuin stores its own history in an SQLite database (~/.local/share/atuin/history.db), recording timestamp,
duration, exit code, and working directory for every command. Ctrl+R opens its search UI. Atuin syncs history
across machines through a server — https://api.atuin.sh by default, encrypted with a key in
~/.local/share/atuin/key — or your own server via atuin server start. Until you run atuin register or
atuin login, the database stays local.
Ubuntu 26.04 packages atuin (18.8.0); 24.04 does not. To capture Bash commands, Atuin needs bash-preexec
or ble.sh, neither of which is packaged in Ubuntu. Without one, Atuin installs and searches fine, but never
records new commands: atuin doctor reports "preexec": "none", and the database holds only what you
imported from ~/.bash_history.
sudo apt install atuin
curl -fsSL -o ~/.bash-preexec.sh \
https://raw.githubusercontent.com/rcaloras/bash-preexec/0.7.0/bash-preexec.sh
sha256sum ~/.bash-preexec.sh
# 33de4e70ee84981d46e7d8a0e3105f1dd9affc9c4178594446cd96e7ef3b2752
atuin import bash
Fetch bash-preexec by version tag rather than master: a tagged file won’t change, and its SHA-256 matches
the hash above. atuin import bash pulls your existing ~/.bash_history into the database.
Add to the bottom of ~/.bashrc:
source ~/.bash-preexec.sh
eval "$(atuin init bash --disable-up-arrow)"
--disable-up-arrow leaves Up Arrow to Bash; without it, Atuin takes over Up Arrow too. Bash keeps writing
~/.bash_history as before, all the settings above stay active, and atuin doctor now shows
"preexec": "bash-preexec".
| If… | Use |
|---|---|
| you want a recent command and remember a chunk of it | Ctrl+R |
| you want every match or entries older than session memory | history | grep, or grep on ~/.bash_history |
| you remember fragments of words rather than an exact substring | fzf |
| you want exit codes, directories, and one synced history across machines | Atuin |
A password ended up in history
Bash never records a command that starts with a space — neither in memory nor on disk, even with history -a.
Put the space as the first character before pressing Enter; afterward it’s too late.
If you already pressed Enter and history -a is on, the line is already on disk, so clean both places.
Remove it from session memory right after the command:
history -d -2
-1 is the history -d command itself; -2 is the command before it. If you ran other commands in between,
find the entry number with history | tail and delete it by number: history -d 3010.
Remove it from the file by line number:
grep -n 'S3cret' ~/.bash_history
# 3010:mysql -u root -p"S3cret-pass" -e "select 1"
sed -i '3009,3010d' ~/.bash_history
Prefix grep with a space so the password fragment doesn’t land in history a second time. Delete both the
command line and the line right above it — that’s its #… timestamp. Without HISTTIMEFORMAT, there’s no
timestamp line, so run sed -i '3010d' instead.
Don’t use history -w here. It overwrites the file with your current session memory: under Ubuntu’s defaults
that’s 1,000 lines, shrinking a 2,000-line file down to 1,000 and erasing anything other windows appended.
History isn’t the only place that saw the password: while the command ran, it appeared in ps for every user
on the server. Rotate it — see Keep passwords out of command arguments.
If it doesn’t work
Ctrl+R says (failed reverse-i-search), yet grep finds the command in ~/.bash_history
- Cause
- Ctrl+R searches the session’s in-memory list, which holds fewer lines than the file: Ubuntu sets
HISTSIZE=1000andHISTFILESIZE=2000. - Fix
- Search right now with
grep -n 'fragment' ~/.bash_history. For a permanent fix, set both memory and file size to the same number — see the “Configure history in ~/.bashrc” section.
You raised HISTFILESIZE in ~/.bash_aliases, yet ~/.bash_history drops back to 2,000 lines after every login
- Cause
- The
HISTFILESIZE=2000line in~/.bashrctruncates the file the moment Bash runs it — before~/.bash_aliasesis sourced. - Fix
- Change the number right in
~/.bashrcand remove the line from~/.bash_aliases.
You added HISTTIMEFORMAT at the bottom of ~/.bashrc, and every login cuts your history in half
- Cause
- Each command now has a
#…timestamp line above it in the file. At login, Bash truncates the file when it hitsHISTFILESIZEbeforeHISTTIMEFORMATis set, counting timestamps as commands. - Fix
- Move
HISTTIMEFORMATaboveHISTFILESIZE.
After every command you see bash: PROMPT_COMMAND: line 1: syntax error near unexpected token, and nothing gets written to the file
- Cause
~/.bashrchasPROMPT_COMMAND="$PROMPT_COMMAND; history -a". On UbuntuPROMPT_COMMANDstarts empty, leaving a leading;.- Fix
- Use
PROMPT_COMMAND+=('history -a'): it appends to the list whether it’s empty or not.
Older entries in history all show today’s timestamp
- Cause
- They were recorded before
HISTTIMEFORMATwas set, so they have no timestamp markers. Bash displays the session start time for them. - Fix
- Leave it as is: commands run after the change get real timestamps.
Commands from a second SSH window don’t show up in this one
- Cause
- Each window reads
~/.bash_historyonce at login. Withouthistory -a, the second window also waits until exit to write its commands. - Fix
- Run
history -a; history -c; history -rin this window to save your own commands, clear memory, and reload the file. To make it automatic, see the “Shared history across windows” section.
A command pasted from a guide is missing from history
- Cause
- The line started with a leading space. With
HISTCONTROL=ignoreboth, Bash skips lines starting with a space — that’s how you keep passwords out of history. - Fix
- Strip the leading space before pressing Enter. To record those lines too, set
HISTCONTROL=ignoredups, though you lose the leading-space shortcut for hiding passwords.
After Ctrl+S, the terminal freezes and stops responding to keystrokes
- Cause
- By default, Ctrl+S pauses terminal output. Nothing you type is lost — it’s buffered.
- Fix
- Press Ctrl+Q to resume. To free Ctrl+S for forward history search, put
stty -ixonin~/.bashrc.
Under sudo -i, history is still short and has no timestamps even though you updated your ~/.bashrc
- Cause
- Root has its own
/root/.bashrcand/root/.bash_history. Commands run withsudogo into the calling user’s history; commands run insidesudo -igo into root’s. - Fix
- Update
/root/.bashrcwith the samesedcommand — see the “Do the same for root” step.
Commands from a session vanished after kill -9 or a hard reboot
- Cause
- Without
history -a, Bash writes history only on exit, andkill -9, OOM kills, and hard resets never let it exit cleanly. Closing a terminal window or losing the network does: Bash receives SIGHUP and flushes history. - Fix
- Add
PROMPT_COMMAND+=('history -a')so every command hits the file right after it finishes.
How to check
Verify timestamps and immediate saving:
history 3
# 3000 2026-11-11 17:36:55 journalctl -u nginx --since today
# 3001 2026-11-11 17:36:56 systemctl status nginx --no-pager
# 3002 2026-11-11 17:36:56 history 3
tail -2 ~/.bash_history
# #1794418616
# history 3
history 3 is already in the file while the session is still open, confirming history -a works. Seeing
dates and times confirms HISTTIMEFORMAT; in the file, each timestamp sits on the line above its command,
where #1794418616 is Unix seconds since 1970.
Check what your next SSH login will get:
bash -lic 'echo $HISTSIZE $HISTFILESIZE'
# 20000 20000