This is a kind of follow-up question to this question: ssh, start a specific shell, and run a command on the remote machine?, except more-specifically regarding ash, and focused on the fact that sourcing a .bashrc file as part of starting the shell in the ssh session doesn't work for me.
I'm on an embedded Linux device which has a busybox version of sh and ash, but there is no bash. We share a common username (root), and a common execution environment.
I'd like to have a few custom aliases and a custom PS1 Prompt String 1 variable, however.
Here is an example normal ssh session. Note that I'm using sshpass to pass my password to ssh, with my password stored in the ~/pw file.
You can see that the default shell is -sh and there are no aliases. The default PS1 prompt string also shows no path.
$ sshpass -f ~/pw ssh root@192.168.0.2 [root@device]$ echo $0 -sh [root@device]$ alias [root@device]$
The device has no bash, but it does have ash, which apparently is bash-like. So, I want to make that my shell as I ssh in. I also want to copy Ubuntu's default ~/.bashrc file, located in /etc/skel/.bashrc on the Ubuntu machine I'm ssh-ing from, to the device at /tmp/.bashrc so I can source it, and I want my ssh command to do that. I can't copy to ~/.bashrc on the remote device because the home dir is read-only.
I tried the following, but get the following error:
cmd:
sshpass -f ~/pw scp /etc/skel/.bashrc root@192.168.0.2:/tmp/.bashrc \
&& sshpass -f ~/pw ssh -t root@192.168.0.2 '. /tmp/.bashrc'
result:
Connection to 192.168.0.2 closed.
No ssh session was established.
I then tried the following command, with the following result, showing that sourcing did not work, although the ash shell was entered.
$ sshpass -f ~/pw scp /etc/skel/.bashrc root@192.168.0.2:/tmp/.bashrc \ && sshpass -f ~/pw ssh -t root@192.168.0.2 '. /tmp/.bashrc; ash' ~ # echo $0 ash ~ # alias ~ #
I can run . /tmp/.bashrc manually now though, and it works fine. Notice how after sourcing, which works, I have an improved prompt string and new aliases:
~ # . /tmp/.bashrc ash: /tmp/.bashrc: line 16: shopt: not found ash: /tmp/.bashrc: line 24: shopt: not found ash: /tmp/.bashrc: line 111: shopt: not found root@device:~# alias l='ls -CF' alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '"'"'s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'"'"')"' la='ls -A' ll='ls -alF'
You also see the ash errors from line 16, 24, and 111 due to shopt not being found. Even if I comment those out in the .bashrc file I send over, the results as shown above are still the same.
How can I get my above command to work? I'd like it to ssh in, set the shell to something more like bash, and source my /tmp/.bashrc file I scp'ed over.
On a similar embedded device which does have bash installed, this cmd works perfectly, as I document in my repo here:
sshpass -f ~/pw scp /etc/skel/.bashrc root@192.168.0.2:/tmp \
&& sshpass -f ~/pw ssh -t root@192.168.0.2 'bash --rcfile /tmp/.bashrc'
For bash, using bash --rcfile /tmp/.bashrc at the end of the ssh cmd is what I need. But, for the device withOUT bash, I need help getting this behavior with ash or similar.
argv[0]to find out what installable module and options for the module to use. So /bin/sh is likely "run Busybox ash shell with the more-POSIX flag" – Ben Voigt Nov 11 '21 at 23:03ashwhich mentionsENVvariable: https://linux.die.net/man/1/ash – Gabriel Staples Nov 11 '21 at 23:24ENVworked, as you said! Here is my final alias. I put this in~/.bash_aliaseson my Ubuntu machine and can now usegs_sshto ssh into the device with the environment that I want. I customized the .bashrc file to my needs. Final alias and command:alias gs_ssh="sshpass -f ~/pw scp path/to/custom/.bashrc root@192.168.0.2:/tmp && sshpass -f ~/pw ssh -t -o 'ServerAliveInterval 60' root@192.168.0.2 'export ENV=/tmp/.bashrc; ash'". Note that usingashvsash -iat the end seems to make no difference, perhaps because the shell is already interactive since we are usingssh -t? – Gabriel Staples Nov 11 '21 at 23:31Why are you running ash?I suspect @BenVoigt is right in his comment above. When I runbusybox --helpto see a list of all functions inside it, I seeashandshseparately. My understanding is thatashis a lightweight substitute forbash, whereasshis not. – Gabriel Staples Nov 11 '21 at 23:43shis ash, it's available under both names, and I don't think BusyBox cares which name you use. – Gilles 'SO- stop being evil' Nov 12 '21 at 00:17ashorsh, or any mention of a “more-POSIX” mode. What makes you think they behave differently? – Gilles 'SO- stop being evil' Nov 12 '21 at 00:19SH_IS_HUSHconfiguration for that. – Nov 12 '21 at 00:44