I notice that when doing one-liners via ssh
into a Linux server, the value of ~
remains the value of the local user's home directory, not the remote user's home directory. Witness local user dotancohen
and remote user atwood
:
$ whoami
dotancohen
$ ssh beta whoami
atwood
$ ssh beta ls ~/.ssh
ls: cannot access /home/dotancohen/.ssh: No such file or directory
$ ssh beta ls $HOME/.ssh
ls: cannot access /home/dotancohen/.ssh: No such file or directory
$ ssh atwood@beta ls ~/.ssh
ls: cannot access /home/dotancohen/.ssh: No such file or directory
$ ssh beta ls /home/atwood/.ssh
authorized_keys
$ ssh beta ls .ssh
authorized_keys
Note that even specifying the username in the connection details, and even using $HOME
in place of ~
did not help. Not that I expected them to, but it was worth trying.
I have no problem looking in .ssh/config
for the username when I'm doing one-liners if need be, but the inability to use ~
in scripts is rather inconvenient. Other than parsing ~/.ssh/config
for the username, is there any way around this limitation? The last example shows that I can assume that we're in his home directory and reference things relative to that, but I would prefer to explicitly call the directory if possible.
How can I reference the user's home directory via SSH oneliners, without parsing ~/.ssh/config
locally for it?
Tested on local system Kubuntu 14.04 and remote systems Ubuntu Server 12.04, 14.04, and RHEL 6.5.
You just need to escape it so that your local shell doesn't expand it:
or
That should work.