Skip to content

How to Clear BASH Command Line History in Linux?

2 min read

The bash history keeps a record of all commands executed by a user on the Linux command line. This allows you to easily run previously executed commands by using the “up arrow” or “down arrow” keys to scroll through the command history file.

 

Take a look at the command below, here the user vyga has typed the database server password on the command line.

$ sudo mysql -u root -p=@!#@%$lab

 

If you look into th bash history file towards the end, you will see the password typed above in there.

$ history

 

The bash_history file is normally located in a user’s home directory /home/username/.bash_history.

$ ls -l /home/vyga/.bash_history

 

To remove a single line from the history file, use the -d option. For example, if you want to clear a command where you entered clear-text password as in the scenario above, find the line number in the history file and run this command.

$ history -d 2038

 

To delete or clear all the entries from bash history, use the history command below with the -c option.

$ history -c

 

Alternatively, you can use the command below to delete history of all last executed commands permanently in the file.

$ cat /dev/null > ~/.bash_history 

 

Note: A normal user can only view his/her own command history, but the root user can view the command history of all other users on the system.

 

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket