Comments on Some Tips To Make SSH/SCP Usage More Convenient
Some Tips To Make SSH/SCP Usage More Convenient I guess many of us rely heavily on ssh/scp to access/maintain remote hosts. In this short article I would like to share some experiences I find useful for ssh/scp usage.
9 Comment(s)
Comments
Why not just create a .ssh/config file with the following contents:
Host host1 Hostname 192.168.0.1 Host host2 Hostname 192.168.0.2 User root Host host3 Hostname 192.168.0.3 User admin Port 222
Then you can just type "ssh host1" and "scp host1". Bash is also clever enough to tab complete hostnames from your .ssh/config file.
Hi all, thanks for your excellent comments. I used this method with other applications in the old days (rsh, telnet, ftp) and was not aware that with ssh, all of this can be done easier via .ssh/config. I wanted to share something and in turn I learned much more from your comments. I hope I will receive similar comments for my next articles :)
You can put a lot of custom settings per host in the .ssh/config file as well:
Host host_name: the following settings are for this host.
User your_remote_user_name: use this user as default for the above host.
ForwardX11 yes: can be used to run X over SSH (I use this a lot).
Compression yes: If you run SSH over the internet, this may speed up things. Do not use compression over LANs.
CompressionLevel 9: if you need even more compression.
Cipher blowfish: should speed up the connection as well.
On some systems (Ubuntu, for instance), a known host is automatically used in autocompletion.
Most of the things discussed in this tutorial are better configured in the ssh_config. Most likely residing at $HOME/.ssh/config
See the man page ssh_config.
You can easily configure ports and users for certain hosts or domains and all sort of other config options.
Have you tried using the config file? I beleive it would be more simple.
create a config file for you user, usually ~/.ssh/config
In it you can put the following
host=host1
user=root
host=host2
user=admin
port=23230
You can also use wildcards
host=*domain.com
port=12345
Personnaly I have to connect to many servers as the user admin, so I have the following:
host *
user=admin
another useful option is
ControlMaster auto
ControlPath /tmp/%h
this creates a control file in /tmp. This lets you open new sessions on the same host without reauthenticating yoruself. I put this in the host * block.
Since scp/rsync etc use ssh, these settings apply to them also. In addition these specify "defaults". For example even though I specify admin as the user for all hosts, if I run ssh root@host it will overwrite the setting and conenct as root.
Hope these help :)
I think you can accomplish the same thing a bit more easily with a config file (~/.ssh/config per-user, or /etc/ssh/ssh_config for all users). You can also set tonnes of other options there. See man ssh_config.
I like the enable-ssh script, though.
There is a built in version of that script in most distributions called "ssh-copy-id", it achieves the same thing.
Why not use .ssh/config?
I would say that a much better approach is to use the .ssh/config file in your home dir. (create it if it's missing). Then you have all the settings together in one place, and you don't need extra scripts or root privileges for instance to change the hosts file.
An example of a host entry in the config file could be:
Host server #Name of the host Hostname 192.168.10.20 #IP address or DNS name Port 2222 #Custom port number User root #Username to use
In order to connect to the host, we just type "ssh server" and all the settings defined in the file will be used. It also works with scp.
You don't have to be familiar with shell scripting in order to use this, and it is easier to copy the one file around between several clients where you would use it.
The best part is that it also works with tab completion when you have typed "ssh " :)