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
[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

This Post Has 8 Comments

  1. vreyro linomit

    whoah this blog is excellent i really like reading your posts. Stay up the good work! You know, lots of people are hunting round for this information, you could help them greatly.

  2. zortilonrel

    Awesome blog you have here but I was curious about if you knew of any user discussion forums that cover the same topics talked about here? I’d really like to be a part of community where I can get feedback from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Many thanks!

  3. Christina

    whoah this weblog is wonderful i really like reading your posts.
    Keep up the good work! You already know, many people are searching around
    for this information, you could aid them greatly.

  4. Dorie

    Awesome blog! Do you have any suggestions for aspiring writers?
    I’m hoping to start my own site soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or go
    for a paid option? There are so many choices out
    there that I’m totally confused .. Any recommendations?

  5. frolep rotrem

    The following time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my option to learn, however I really thought youd have something fascinating to say. All I hear is a bunch of whining about something that you might fix in the event you werent too busy searching for attention.

  6. zovre lioptor

    Magnificent website. Plenty of useful info here. I’m sending it to several pals ans also sharing in delicious. And naturally, thanks on your effort!

  7. gralion torile

    naturally like your website but you need to take a look at the spelling on quite a few of your posts. A number of them are rife with spelling issues and I find it very bothersome to tell the truth nevertheless I’ll certainly come again again.

  8. zoritoler imol

    I got what you intend,saved to my bookmarks, very nice website .

Leave a Reply