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.
Good luck and be safe! :)
Top comments (9)
You can also use the clipboard, via pbpaste on macOS and xclip on Linux (not sure how to do that on Windows).
Example:
Then if you call
history
, you only getIt works wonders for injecting secrets copied from a web browser (tokens, API keys, obtained from an admin panel) in the environment without revealing anything.
Intresting, thanks!
Thanks for this! It’s what I was looking for
?
Kidding aside, you're on the right track with
histignore
, you just want to expand on it. You can specify multiple patterns tohistignore
by colon-separating each pattern. Thus, you could do:Clear the history before the logout, is geniuous, thanks :D
Yeah, but might piss off your IA guys. Some security-orgs see that a sign of nefarious motives (sinçe you're "trying to hide history").
That said, in addition to configuring filtered recording of history with a multi-pattern
histignore
definition, a couple other tricks I've used over the year have been:Set up your
${HOME}/.<SHELL_INIT>
file to try to store its history in either/dev/null
, or something like/tmp/.$( tty | sed 's#/.*/##' ).${LOGNAME}
:/tmp
or/tmp
is ontmpfs
.Another useful thing (when you want to keep history), is to set
HISTTIMEFORMAT='%F@%T '
. Doing so means that your history is time-stamped so that you know when an action was executed (useful for correlating with logs in case you need to figure out who screwed something up)".You have to put those somewhere, a system users env variables should be a good safe place afaik.
But in case they aren’t, maybe there’s some key managers out there, or you could keep them outside env variables and instead input the keys via terminal commands in your CI/deployment-system?
I often face the situation than I just need to login via SSH to the client server and do some quick Mysql queries or something similar, so I have to disappear without any clues :D
It might be me being drunk on a Thursday, but those kinda practices has gotten me in trouble before :D