Hide the Exported ENV Variables From the History

Written by epranka | Published 2020/02/04
Tech Story Tags: linux | bash | tips | ubuntu | programming | coding | staging-environments | local-dev-environment

TLDR When working with the Linux server, sometimes you export some environment variables. The problem is if anyone accesses the server bash and enters the history command it will see the secrets. This method is not only for environment variables, but it can also hide any bash command, even if you leave the space before any Bash command, it will be ignored in history. The method is also used to hide any Bash commands, even though it can be used to work with Linux history control. For example, the Mysql password will now be ignored.via the TL;DR App

Hi, happy to join your community. This is my first post, so I start from the simple one, but useful. Maybe you don't know about it yet.
When you work with the Linux server, sometimes you export some environment variables. Some envs can be neutral like
NODE_ENV=production 
or something else, but sometimes it must be safe like 
GITHUB_API_KEY
 or 
MYSQL_PASSWORD
.
The problem is if anyone accesses the server bash and enters the history command it will see the secrets:
...
1989  export MYSQL_PASSWORD=my_secret_mysql_password
...
2000  history
To be safe, before working with the bash, export Linux history control environment variable which is called 
HISTCONTROL
.
$ export HISTCONTROL=ignorespace
ignorespace means that if you leave the space before any bash command, it will be ignored in history.
So while exporting the secret environment variable, enter the space before export
$ export HISTCONTROL=ignorespace
# keep in mind space before export
$  export MYSQL_PASSWORD=my_secret_mysql_password
$ history
So now, the Mysql password will be ignored in history
...
1999  export HISTCONTROL=ignorespace
2000  history
This method is not only for environment variables, but it can also hide any bash command, even 
export HISTCONTROL=ignorespace
 itself.
Follow on TwitterGitHub, and let’s connect on LinkedIn
Good luck and be safe! :)
Previously published at https://dev.to/epranka/hide-the-exported-env-variables-from-the-history-49ni

Written by epranka | Software engineer
Published by HackerNoon on 2020/02/04