0

I use the authorized-key to log into my remote ubuntu server. I need to use scp frequently to cp files from my local machine to my remote machine. I want to enable the autocompletion as I did with my local machine by bash-autocompletion.

How could I do that?

Updated

I tried zsh(https://github.com/robbyrussell/oh-my-zsh), the scp works fine with auto-completion. How could I do it with my bash?

steveyang
  • 673
  • 4
  • 10
  • 16

2 Answers2

1

Ubuntu and Debian to my knowledge bring with the bash-completion package exactly the functionality that you ask. However if the remote server has a scheme implemented to thwart break-in attempts (e.g. by limiting the number of connections within a given time interval and blocking if a client exceeds it) you will run into problems.

Check /etc/bash_completion on your system and /etc/bash_completion.d/ssh. If it doesn't exist, make sure to apt-get install bash-completion. Once that's done, make sure that your .bashrc or .profile loads that global auto-completion file.

This brings usually functionality to both auto-complete the remote hostname from your $HOME/.ssh/known_hosts (grep _known_hosts_real /etc/bash_completion) and auto-completion for remote folders on scp.

One note of caution: if the remote host outputs extra stuff upon login, the auto-completion tends to break. One way to work around this can be (but I found this not to work everywhere) to bail out without any output on the remote machine:

[ -z "$SSH_TTY" ] && exit

... i.e. show no banner or so. You include this line in /etc/sshd/sshrc on the remote box or at the top (below hashbang) of whatever script you are executing to show the ASCII art form of the fingerprint. Note that the above line is bourne-shell syntax, adjust it of needed. It checks whether the SSH_TTY env. variable is empty and if so exits with exit code 0.

0xC0000022L
  • 1,516
  • 2
  • 22
  • 42
  • But it doesn't even work once... How should I config the `sshd` on the server side to enable this? – steveyang Feb 09 '12 at 15:22
  • Sheesh - talk about log pollution on the remote server. – EEAA Feb 09 '12 at 15:23
  • @yangchenyun: nothing to configure on the remote machine other than using `ssh-copy-id` (or manually copying your public key). – 0xC0000022L Feb 09 '12 at 15:25
  • @ErikA: sorry, no idea what you are talking about, but this does in no way require installation of anything on the remote server, other than `sshd` and `sed`, I think ... no "packages" required there at least. – 0xC0000022L Feb 09 '12 at 15:26
  • 1
    @STATUS_ACCESS_DENIED By "log pollution", I'm referring to the fact that, if this does what I think it does, bash-completion is going to authenticate to the remote server every time you hit tab so it can get the directory listing. The SNR of the logs will just go down significantly. Not a huge deal, but just an observation. – EEAA Feb 09 '12 at 15:28
  • The bash_completion works fine here. I use `brew install bash-completion` on mac, and I load it with my `.bash_profile` properly. The **one note of caution** might be the causes of problem. I ask the ssh to print to RSA-key img to verify the server identification. Where to use the line `[ -z "$SSH_TTY" ] && exit`? – steveyang Feb 09 '12 at 15:29
  • @ErikA: gotcha. Fair point, can't argue with that :) – 0xC0000022L Feb 09 '12 at 15:29
  • @yangchenyun: edited my answer to include (hopefully) all info required to include that line or the functionality it provides. – 0xC0000022L Feb 09 '12 at 15:34
  • +1 for outputting "extra stuff upon login" – puk Sep 18 '12 at 06:19
0

I guess not, since you don't have access to the remote filesystem until you actually hit enter and authenticate when you are doing SCP.

bash-autocompletion kicks in only after getting successfully logged in to remote server or after loading bash profile which sources bash-completion script in the case of local login.

you might want to look into sshfs which mounts remote filesystem locally and then you can do auto-completion on that.

kaji
  • 2,528
  • 16
  • 17
  • nope, that's not correct ;) ... on Debian/Ubuntu please install the package `bash-completion` and check out the file `/etc/bash_completion.d/ssh` for an example implementation. – 0xC0000022L Feb 09 '12 at 15:28
  • If you use key authorization, it could work. As the `ssh-agent` will take care of the verification. Also `zsh` could archive this effect but I want to have it on my bash. – steveyang Feb 09 '12 at 15:31
  • I am aware of that ...please read my answer carefully(2nd paragraph).The guy is asking for autocompletion on remote machine folder while doing SCP from his computer ! Autocomplete works on his computer folders / files but will not work on remote computer's filesystem – kaji Feb 09 '12 at 15:34
  • @kaji: and the comment I made was to point out that his local auto-completion can be harnessed to complete file and folder names on `scp` to and from the remote machine (and yes, with the remote file/folder names). – 0xC0000022L Feb 09 '12 at 15:36
  • @STATUS_ACCESS_DENIED thanks i was just defending my answer ;) I will try to see if i could make it work with bash here as you explained – kaji Feb 09 '12 at 15:42