Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a CentOS host in my local network that I daily use to open SSH connections to many (over 40) other hosts in both Internet and the local network. I have set up RSA keypairs to skip the interactive authentication and some bash scripts like this:

[root@beta ~]# cat ~/connections/mywebsite.sh
ssh mywebsitefi@virtualserver123.hostingprovider.fi

This works fine when I need to open connections to them, but not when I need to scp files to the same host.

Is it possible to create something equivalent to an alias to replace the username@host part like this?

[root@beta ~]# scp myfile MYWEBSITE:/var/www

The possibility to autocomplete the alias with the tab key would also be awesome.

I understand that bash aliases can only be used as the first word of the command, but is there some other way to achieve this? If not in bash, maybe another shell?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

use the capabilities built into ssh, scp and rsync.

In your .ssh subdir, create a 'config' file. (chmod 0600)

Within this, you can describe hosts and options to be used for particular hosts (or global options).

Using your example of: mywebsitefi@virtualserver123.hostingprovider.fi

Enter the following:

Host MYWEBSITE
  Hostname virtualserver123.hostingprovider.fi
  User mywebsitefi
  Port 22

from this point on, if you use 'MYWEBSITE' as the hostname, it'll automatically supply the real hostname, username, port, options, whatever you specify. (I specify the port number by habit)

I use this myself to specify non-standard port numbers for my internet-facing ssh servers/

You could type:

ssh MYWEBSITE

and the entries in .ssh/config file will fill in the url, username, port and options.

scp MYWEBSITE:/somepath/somefile /home/me/
rsync -vzP /some/files MYWEBSITE:/my/home/

work as you'd expect too.

If you specify multiple names in the Host line, you can call it by any of those names, all being equal.

indentation and capitalization is irrelevant, but I like to break it up like this for clarity. place as many entries as you need in your .ssh/config

.ssh/config must have permissions of 0600 (rw user, none for all others), otherwise ssh will complain and ignore the file.

Of course, more information is available in the ssh(1) and ssh_config(5) man pages.

Anything placed prior to the first 'Host' line is considered a global option and is applied to all the entries you define. Handy to specify connection timeouts, X11 forwarding and others.

share|improve this answer
    
Thanks, this and @eldering's answer combined are exactly what I was looking for! –  Kaivosukeltaja Sep 23 '11 at 14:31

If you install the bash-completion package, then that will add autocompletion of ssh hostnames and aliases based on information that can be found in various places such as .ssh/config, known hosts, etc.

share|improve this answer

If I understand your question, a shell function will suffice: assuming you want to hardcode the user, host and path:

myscp () {
  scp "$@" user@host:path
}

use like

myscp *.html *.css
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.