Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

History command is not working in a non-interactive SSH command from another Linux machine

I have a little quirk with my Linux machines and I suspect it’s due to the fact that I define my history file in my .bashrc file like this:

export HISTFILE=~/.history

From another machine I like to run a non-interactive ssh command to see things I’ve done on that other machine. I often like to see the history of commands on some other machine so I run something like this, using a predefined SSH configuration for the other machine named "vps" in this example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ssh vps "history"

and I get no response.

I can run

ssh vps "ls -al"

or any other command and see the results, but the history command is the only one that doesn’t work.

If I run this I can see the environment that I’m getting from the non-interactive shell:

$ ssh vps "env"
SHELL=/bin/bash
HISTCONTROL=ignoredups:erasedups:ignorespace
HISTSIZE=5000
PWD=/home/myname
LOGNAME=myname
XDG_SESSION_TYPE=tty
MOTD_SHOWN=pam
HOME=/home/myname
LANG=C.UTF-8
HISTFILE=/home/myname/.history
XDG_SESSION_CLASS=user
USER=myname
SHLVL=0
XDG_SESSION_ID=1167
XDG_RUNTIME_DIR=/run/user/1002
PATH=.:/home/myname/.local/bin:/home/myname/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
HISTIGNORE=&:ls:ll:l:lt:la:h:hh:pwd:exit:..:duh:duh2:cd
HISTFILESIZE=5000
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1002/bus
_=/usr/bin/env

My .bashrc has these things along with some other aliases:

PATH=.:$HOME/.local/bin:$PATH
shopt -s checkwinsize
shopt -s cmdhist
shopt -s cdspell
shopt -s histappend
shopt -s histverify
export HISTFILE=~/.history
export HISTIGNORE="&:ls:ll:l:lt:la:h:pwd:exit:..:hh:h *"
export HISTCONTROL=ignoredups:erasedups:ignorespace
export HISTSIZE=5000
export HISTFILESIZE=5000
alias h='history'
alias hh='history 50'
alias hgrep='history | grep '
alias hr='history | sort -rn | less '
alias ll='ls -Aoh'

My .bash_profile is the usual:

if [ -f ~/.bashrc ]; then
       . ~/.bashrc
fi

>Solution :

history is disabled by default for non-interactive Bash. You can do like this:

ssh host 'set -o history; history'

# or

ssh host 'HISTFILE=~/.history; set -o history; history'

# or

ssh host '. ~/.bashrc; set -o history; history'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading