How To Create SSH Config File For OpenSSH in Linux - NixCraft
How To Create SSH Config File For OpenSSH in Linux - NixCraft
examples
We can set up a global or local configuration file for SSH clients can create
shortcuts for sshd servers, including advanced ssh client options.
Tutorial details
Root privileges No
Category Terminal/ssh
You can configure your OpenSSH ssh client using various files as follows to save
time and typing frequently used ssh client command-line options such as port,
user, hostname, identity-file, and much more to increase your productivity from
Linux/macOS or Unix desktop:
You can configure your OpenSSH ssh client to save typing time for frequently
used ssh client command-line options such as port number, user name,
hostname/IP address, identity file, and much more. In addition to that it will
increase your productivity from Linux/macOS or Unix desktop.
One config parameter per line is allowed in the configuration file with the
parameter name followed by its value or values. The syntax is:
config value
config1 value1 value2
You can use an equal sign (=) instead of whitespace between the parameter
name and the values.
config=value
config1=value1 value2
All empty lines and lines starting with the hash (#) are ignored are ignored.
Please note that all values are case-sensitive, but parameter names are not.
Note: If this is a brand new Linux, macOS/Unix box, or if you have never
used ssh before create the ~/.ssh/ directory first using the following
syntax:
mkdir -p $HOME/.ssh
chmod 0700 $HOME/.ssh
Examples
$ ssh -i /nfs/shared/users/nixcraft/keys/server1/id_rsa -p
4242 [email protected]
OR
$ ssh -i /nfs/shared/users/nixcraft/keys/server1/id_rsa -p
4242 -l nixcraft server1.cyberciti.biz
See how much I need to type. I need to remember the remote hostname/IP, port
number, the path to ssh key, username, etc. Too much typing and is not
increasing my productivity. But fear not, there is an easy way out.
You can avoid typing all of the ssh command parameters while logging into a
remote machine and/or for executing commands on a remote machine. All you
have to do is create an ssh config file. Open the Terminal application and create
your config file by typing the following command:
vi ~/.ssh/config
OR
vi $HOME/.ssh/config
Add/Append the following config option for a shortcut to server1 as per our
sample setup:
Host server1
HostName server1.cyberciti.biz
User nixcraft
Port 4242
IdentityFile /nfs/shared/users/nixcraft/keys/server1/id_rsa
Save and close the file in vi/vim by pressing Esc key, type :w and hit Enter
key. To open your new SSH session to server1.cyberciti.biz by typing the
following command:
$ ssh server1
Host nas01
HostName 192.168.1.100
User root
IdentityFile ~/.ssh/nas01.key
$ ssh nas01
Host *.sweet.home
Hostname 192.168.2.17
User vivek
IdentityFile ~/.ssh/id_ed25519.pub
The following pattern would match any host in the 192.168.2.[0-9] network
range:
Host 192.168.2.?
Hostname 192.168.2.18
User admin
IdentityFile ~/.ssh/id_ed25519.pub
$ ssh [email protected]
$ vim ~/.ssh/authorized_keys
Update it as follows:
Here is my sample ~/.ssh/config file that explains and create, design, and
evaluate different needs for remote access using ssh client:
### default for all ##
## Set override as per host ##
Host server1
HostName server1.cyberciti.biz
User nixcraft
Port 4242
IdentityFile /nfs/shared/users/nixcraft/keys/server1/id_rsa
## Login to internal lan server at 192.168.0.251 via our public uk office ssh based gateway usin
## $ ssh uk.gw.lan ##
Host uk.gw.lan uk.lan
HostName 192.168.0.251
User nixcraft
ProxyCommand ssh [email protected] nc %h %p 2> /dev/null
Host : Defines for which host or hosts the configuration section applies.
The section ends with a new Host section or the end of the file. A single *
as a pattern can be used to provide global defaults for all hosts.
HostName : Specifies the real host name to log into. Numeric IP addresses
are also permitted.
IdentityFile : Specifies a file from which the user’s DSA, ECDSA or DSA
authentication identity is read. The default is ~/.ssh/identity for protocol
version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and ~/.ssh/id_rsa for protocol
version 2. The IdentityFile option in SSH config or at the CLI refers to the
private key file, which must be kept confidential.
Multiplexing is nothing but send more than one ssh connection over a single
connection. OpenSSH can reuse an existing TCP connection for multiple
concurrent SSH sessions. This results into reduction of the overhead of creating
new TCP connections. Update your ~/.ssh/config:
Host server1
HostName server1.cyberciti.biz
ControlPath ~/.ssh/controlmasters/%r@%h:%p
ControlMaster auto
## ~/.ssh/config ##
Host internal
HostName 192.168.1.100
User vivek
ProxyCommand ssh [email protected] -W %h:%p
ControlPath ~/.ssh/controlmasters/%r@%h:%p
ControlMaster auto
2. ~/.ssh/config option
3. /etc/ssh/ssh_config options
Now want to use all other options from ~/.ssh/config but to connect using
admin user instead of vivek, then:
WARNING! This bash shell aliased based setup may work out for you.
However, I recommend that you use ~/.ssh/config file for better results in a
long run. SSH config file is more advanced and elegant solutions. The alias
command only used here for demo purpose and it is here due to historical
reasons.
An alias is nothing but shortcut to commands and you can create the alias use
the following syntax in your ~/.bashrc file:
$ server1
Conclusion
This page explained the ssh client configuration file syntax and examples to
increase your productivity at Linux, macOS, or Unix shell. See the following
resources or read it using the man command:
$ man 5 ssh_config
Also see:
This entry is 3 of 23 in the Linux/Unix OpenSSH Tutorial series. Keep reading the rest of the
series:
10. OpenSSH Server connection drops out after few minutes of inactivity
14. Reuse SSH Connection To Speed Up Remote Login Process Using Multiplexing
20. SSH ProxyCommand example: Going through one host to reach server
22. Install / Append SSH Key In A Remote Linux / UNIX Servers Authorized_keys
Hi! 🤠
I'm Vivek Gite. I write about Linux, IT, and open source. Subscribe to my
RSS feed or email newsletter for updates. This site is self-funded and
ad-free 🙌 . Want to support nixCraft? You can help through Patreon,
PayPal, or merchandise store.
Patreon ➔ PayPal ➔
↩ ∞
Andrew McGlashan • Aug 18, 2020 @ 9:03
As you can see from the following, if you define something, then it cannot be
redefined later. You need to “*” grouping at the end of the file to catch things that
aren’t yet defined for a “Host” entry.
Two example config files and attempts to use them shown below demonstrate
this fact.
$ cat /tmp/configx
Host *
Port 24
Protocol 2
Host sadsack
Port 333
Hostname aaa
andrewm@mx-hvk-1:/tmp
$ cat /tmp/configy
Host sadsack
Port 333
Hostname aaa
Host *
Port 24
Protocol 2
andrewm@mx-hvk-1:/tmp
↩ ∞
↩ ∞
↩ ∞
Hi,
id_ecdsa.pub
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/nisargam/.ssh/id_ecdsa.pub' are too o
It is required that your private key files are NOT accessible by
This private key will be ignored.
Load key "/home/nisargam/.ssh/id_ecdsa.pub": bad permissions
[email protected]: Permission denied (publickey,keyboar
In the end I experienced that I only have to give the identities name, and openssh
knows what is the private key and what’s the public part.
Greetings
Nisargam
↩ ∞
Ref: SSH: WARNING: UNPROTECTED PRIVATE KEY FILE! Error and Solution
↩ ∞
I was looking for information on config files and I found the information I
needed. Thank you.
↩ ∞
For each parameter, the first obtained value will be used. The configuration files
contain sections separated by ”Host” specifications, and that section is only
applied for hosts that match one of the patterns given in the specification. The
matched host name is the one given on the command line.
Since the first obtained value for each parameter is used, more host-specific
declarations should be given near the beginning of the file, and general defaults
at the end.
Therefore, under “### default for all ###”, the `User nixcraft` will be enforced for
all hosts, regardless of the more host-specific rules further down.
↩ ∞
← Older Comments
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment *
Name
Post Comment
Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by
Next FAQ: Linux Change Password Using passwd Command Over SSH
Previous FAQ: SSH Into Google Cloud Compute Engine Instance Using Secure
Shell Client
🤓 nixCraft SHOP 👇
🔎 SEARCH
🔥 FEATURED ARTICLES
➔ RSS/Feed
➔ About nixCraft
➔ nixCraft Shop
➔ Mastodon