How to remove history in Linux from Command Line

Dear friends today we will see how to remove history in Linux. We will also see what Linux Bash history is and why we need to delete it? So, let’s start and see step by step these sessions.

First, we should know what is Linux Bash history?

We can say, bash history keeps a record of all commands executed by a user on the Linux command line. it allows us to easily run previously executed commands by using the “up arrow” or “down arrow” keys to scroll through the command history file.

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 server.

For more about history in Linux you can Click-Here

Why we need to remove history in Linux?

The major reason for removing command-line history from the Linux terminal is to prevent another user, who could be using the same account.

For example, if we have typed a command that contained a password in plain-text and we don’t want another system user or any attacker to see this password, then we need to remove history from command line.

In this session I will show you two simple ways to remove your command-line history on a Linux server.

In below command you can see urclouds user has typed the database server password on the command line.

[stack@undercloud (urcloudsrc) ~]$ sudo mysql -u root -p=test123

Once you run the history command from Linux command line then you can find the same command with plan text password will be available in Bash history. That’s why we need to be removed history from commands line. Otherwise some other users or any hackers can run this command again and connect to the database. You can see below history command output.

[stack@undercloud (urcloudsrc) ~]$ history

999 03/08/20 11:24:34 openstack server list
1000 03/08/20 11:25:38 sudo mysql -u root -p=test123
1001 03/08/20 11:25:53 history
[stack@undercloud (urcloudsrc) ~]$

The bash_history file is normally located in a user’s home directory /home/username/.bash_history. You can see below path and file.

[stack@undercloud (urcloudsrc) ~]$ ls -l .bash_history
-rw-------. 1 stack stack 40393 Jul 29 13:49 .bash_history
[stack@undercloud (urcloudsrc) ~]$ pwd
/home/stack
[stack@undercloud (urcloudsrc) ~]$

To remove a single line from the history file, we can use the -d option.

For example, if we want to remove a command where we entered clear-text password as in the scenario above, then we need to be find out the line number in the history file and run this command.

[stack@undercloud (urcloudsrc) ~]$ history -d 1000
[stack@undercloud (urcloudsrc) ~]$

We need to be verified our history is deleted or not with history command. Run the history command and check line 1000 is deleted or not?

Remove history

We can see in above screen shot this “sudo mysql -u root -p=test123” command is successfully deleted from history list.

We can use the history command with the -c option to remove all entries from bash history. Like below: –

[stack@undercloud (urcloudsrc) ~]$ history -c

Alternatively, we can also use this below command to clean history of all last executed commands permanently in the file.

[stack@undercloud (urcloudsrc) ~]$ cat /dev/null > ~/.bash_history

That’s all in this session we have seen how to remove history in Linux.  

How to Increase SSH Connection Timeout in Linux

Leave a Reply