惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Ittavern.com

Wimage - Hosting Open-Source Image Uploader with Podman and external S3 Storage Switching from Hugo to picopaper Encryption using SSH Keys with age in Linux ETag in nginx - Simple Resource Caching Sending nginx Logs to Loki with Grafana Alloy How to: Cisco ISE backup to SFTP repository with public key authentication Dummy IP & MAC Addresses for Documentation & Sanitization Deploying ISSO Commenting System for Static Content using Docker Generate a Vanity v3 Hidden Service Onion Address with mkp224o ssh-audit Primer - Audit your SSH Server mtr - More Detailed Traceroute - Network Troubleshooting My Personal Backup Strategy - August 2024 iperf3 - User Authentication with Password and RSA Public Keypair Adding a trash can to Linux with trash-cli Bandwidth Measurement using netcat on Linux Getting started with rsync - Comprehensive Guide Cron Jobs on Linux - Comprehensive Guide with Examples SSH Server Hardening Guide v2 Port Knocking with knockd and Linux - Server Hardening Getting started with rclone - Data transmission Getting started with dig - DNS troubleshooting Getting started with Fail2Ban on Linux Getting started with netcat on Linux with examples URL explained - The Fundamentals Troubleshooting Asking The Right Questions Create tmux layouts using bash scripts Getting started with tcpdump - Ittavern.com Curl on Linux - Reference Guide Getting started with nmap scripts My Offsite Backup - March 2023 Getting started with iperf3 - Network Troubleshooting ICMP echo requests on Linux and Windows - Reference Guide Simulate an unreliable network connection with tc and netem on Linux Detecting Rogue DHCP Server - Ittavern.com Getting started with GNU screen - Beginners Guide Basics of Power over Ethernet (PoE) Difference between RSS and Atom SSH Troubleshooting Guide - Ittavern.com Backup Guide - how to secure crucial data SSH - run script or command at login Linux - unmount a busy target safely Visual guide to SSH tunneling and port forwarding Guide to Wireshark display filters Online Security Guide - Ittavern.com My IT EDC tool kit v2212 10 prompts - 1000 AI generated images - openAI Dall-E SSH - How to use public key authentication on Linux Ways to support open-source projects Getting started with nmap - Ittavern.com nginx - simple and native authentication function Linux - How to work with complex commands EICAR test file - riskless method to test your antivirus and firewall solution Linux - connect to a serial port with screen Podman / Docker - expose port only to the localhost of the host machine Tmux - reload .tmux.conf configuration file My use cases for CyberChef Nginx - simple permanent or temporary redirects Getting started with tmux - Ittavern.com Tmux - synchronize the input of all panes within a window Nginx - check your public IP CyberChef - How to remove empty lines
Basics of the Linux Bash Command History with Examples
2023-02-03 · via Ittavern.com

The bash command history shows the previously used commands. By default, the history is saved in memory per session and can be saved to a file for later sessions. We will explore ways to show, search and modify the history in this blog post.

I use RHEL and Debian-based Linux distributions and bash in this blog post as a reference.

Configuration

I want to start with ways to configure the behavior of the bash history.

The configuration of the history can be changed in the bash startup file. Those can typically be found in the home directory of the user.

System-wide /etc/profile or in the home directory ~/.bashrc or ~/.profile. There are more, but just to list some examples.

If you want to use an option for one session only, you can just type it in like this:

HISTFILE=/dev/null or unset HISTFILE

In both ways, you would disable the history for the current bash session.

The basics

The bash history should be enabled by default, but you might want to change some settings.

The history file that is stored on disk can be found with the following command:
echo $HISTFILE

The default location for the history on disk is ~/.bash_history.

Add the following option to change the name and storage location of the history file on disk:
HISTFILE=/path/to/the/history.txt
Show the complete history from memory:
history
Just show the last number of commands:
history 20
Read history from disk to memory:
history -r
Append history entries from memory to disk:
history -a
Overwrite the disk history with the memory history:
history -w

Since I am used to working with multiple sessions and I want to share the history of them, I've added the following line to my startup file to append every entry to the history on disk.

export PROMPT_COMMAND='history -a'

Delete a specific history entry or range:
history -d 20 # one specific entry
history -d 15-20 # range
history -d -5 # last 'n' of entries

Disabling bash command history #

As mentioned above, there are multiple options to disable the bash command history.

HISTFILE=/dev/null or unset HISTFILE

Number of history entries #

The following option sets the number of entries that are displayed if you enter history:
HISTSIZE=20
The following option sets the maximum number of entries in the history on disk:
HISTFILESIZE=2000

Search function

You can start a reversed search through the history by pressing CTRL + r and entering the search term. You can jump to the next result by pressing CTRL + r again. After finding the desired command, you can press TAB to get filled to the current command line or press ENTER to run the command immediately.

If you skipped through your desired command, you can cancel the current search request with CTRL + g and start from the top again.

There is no native way to jump forward again - but you could add a forward search by adding stty -ixon to your startup file. The keyboard shortcut for the forward search is CTRL + s.

Using 'grep' #

I prefer to use grep to find commands. Simply use one of these examples to do so.

history | grep SEARCHTERM
or
grep SEARCHTERM $HISTFILE

Side note: use the -i flag if you want search case-insentitive.

Add comments to commands #

You can add comments to commands with #. This makes it easy to find commands again or document the thoughts behind the command in troubleshooting sessions and later reviews.

kuser@pleasejustwork:$ echo nightmare # dolphins chasing me in a mall
nightmare

Exclusions

We can add exclusion with the HISTIGNORE option in your startup file. This can be useful for privacy and security reasons.

There are some predefined options we can choose from:
ignorespaces - if the command starts with a SPACE, it will be excluded
ignoredups - duplicate commands will be excluded
ignoreboth - both above-mentioned options together

If those options are not enough, you can create your own rules. For example, the ignoreboth rule could be written like this:

HISTIGNORE="&:[ ]*" # the ampersand & means no duplicates, : is the separator, [ ]* checks if the command begins with a SPACE

You can add commands too.

HISTIGNORE="ls:pwd:cd"

Timestamps

Timestamps are often important for reviews of troubleshooting sessions. With the HISTTIMEFORMAT option, you can add timestamps in various formats to your history.

The default history looks like this:

kuser@pleasejustwork:$ history 6
 1150  history
 1151  vim .bash_history 
 1152  vim .bashrc 
 1153  source .bashrc
 1154  ls
 1155  history

And the same lines look like this after adding HISTTIMEFORMAT="%F %T " to the configuration:

kuser@pleasejustwork:$ history 6
 1150  02/02/23 18:03:32 history
 1151  02/02/23 18:03:45 vim .bash_history 
 1152  02/02/23 18:05:03 vim .bashrc 
 1153  02/02/23 18:05:22 source .bashrc
 1154  02/02/23 18:05:26 ls
 1155  02/02/23 18:05:30 history

You can adjust the format with the following placeholders:

    %d: day
    %m: month
    %y: year
    %H: hour
    %M: minutes
    %S: seconds
    %F: full date (Y-M-D format)
    %T: time (H:M:S format)
    %c: complete date and timestamp (day-D-M-Y H:M:S format)

Re-run commands

!! is a variable for the previous command and, for example, can be used to run the last command as 'sudo' .

kuser@pleasejustwork:$ whoami
kuser
kuser@pleasejustwork:$ sudo !!
[sudo] password for kuser: 
root

! can be used to re-run the last command starting with a chosen term.

kuser@pleasejustwork:$ history 5
   41  ping ittavern.com         # !ping
   42  whoami                    # !whoami
   43  nmap -sP 10.10.10.0/24    # !nmap
   44  vim .bashrc               # !vim
   45  history
kuser@pleasejustwork:$ !who      # runs immediately and auto-completes
kuser

!n would run the 'n' command in the history, and !-n refers to the current command minus 'n'

kuser@pleasejustwork:$ history 5
   41  ping ittavern.com         # !41 / !-5
   42  whoami                    # !42 / !-4
   43  nmap -sP 10.10.10.0/24    # !43 / !-3
   44  vim .bashrc               # !44 / !-2
   45  history                   # !45 / !-1 / !!
kuser@pleasejustwork:$ !-4       # runs immediately
kuser

modify and re-run previous command #

With the following syntax, you can replace keywords from the previous command and run it again.

^OLD KEY WORD^NEW KEY WORD OR PHRASE^

Example:

kuser@pleasejustwork:$ sudo nmap -T3 10.10.22.0/24 -p 80,443
kuser@pleasejustwork:$ ^22^50^            # the command will be executed immediately
kuser@pleasejustwork:$ sudo nmap -T3 10.10.50.0/24 -p 80,443`

Use a backslash \ as an escape character if you need to find or replace a ^.