ssh, start a specific shell (ash), and source your environment on the remote machine
1
vote
2
answers
2037
views
This is a kind of follow-up question to this question: https://unix.stackexchange.com/q/20729/114401 , 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](https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/home/.ssh#command) :
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.
Asked by Gabriel Staples
(2972 rep)
Nov 11, 2021, 10:28 PM
Last activity: Nov 11, 2021, 10:52 PM
Last activity: Nov 11, 2021, 10:52 PM