How to configure SSH Client in Linux ?
Last Updated :
28 Mar, 2022
At times we may need to access multiple remote systems with different IP addresses/hostnames, usernames, non-standard-ports with various command-line options. One way is to create a bash alias for each remote connection. But we have an easy and much better solution to this problem. OpenSSH allows to create per-user configuration file to store different SSH options for each remote server. In this article, we will see the basics of SSH Client configuration with some examples.
Requirement
Linux or macOS system with OpenSSH Client installed.
File Location
The SSH client-side configuration file is named config which is stored in the .ssh directory under the user's home directory. By default, the config file will not be present and the user needs to create it using the touch command. This file should be readable and writable only by the user and should not be accessed by others.
Create config fileConfig File Syntax
SSH config file takes the following syntax,
Host [Alias1]
Option1 [value]
Option2 [value]
Option3 [value]
Host [Alias2]
Option1 [value]
Option2 [value]
Host [Alias3]
Option1 [value]
SSH Client reads the configuration file stanza by stanza. If more than one pattern matches, then the options from the first matching stanza take precedence. Hence, the host-specific declaration should come first and the generic declaration should be kept last.
SSH Client does not care about indentation but it's recommended for readability.
There is numerous number of ssh options available which can be found by typing man ssh_config in Linux terminal or can check in /etc/ssh/ssh_config file for reference.
SSH Config File Example
When a user wants to connect to a remote server through ssh, then he should mention remote username followed by IP address or hostname and port (default port 22 can be ignored). Consider below example,
Access server
Now, we can have the below lines in ~/.ssh/config file,
Sample Config
If we simply type ssh server, the ssh client will read the configuration file and use the options mentioned to connect to the remote server.
Access server via client configSSH Config Patterns
The host directive mentioned in syntax can contain one pattern or space-separated list of patterns.
Host nancy server
Working Patterns
nancy, server
Patterns can contain one of the following specifiers as well,
- * - Matches zero or more characters.
Host nancy*
Working Patterns
nancy,nancy123,nancy-server,etc
- ? - matches exactly one character.
Host nancy?
Working Patterns
nancy1, nancy2, nancy3, etc
- ! - When used at the start of a pattern, it negates the match.
Host nan* !nancy
Working Patterns
nan-1, naneo, etc [nancy will not work]
SSH Config Precedence
Let's consider the below example to understand more about the precedence in ssh options when multiple hosts are defined,
Host nancy
HostName 10.21.43.150
Compression no
Host jancy
HostName 10.2.33.58
PermitLocalCommand no
Host trial
HostName 10.2.33.57
PermitLocalCommand yes
Host *cy !jancy
User woot
Port 22
Host *
User root
Port 22
Compression yes
- When a user types ssh nancy, the ssh client applies the options from the first match which is Host nancy. Then it checks for the next matching pattern which is Host *cy !jancy. Then the next matching pattern is Host *, here none of the options are considered as they got over-ridden in the earlier matched patterns. The full list of options are,
Host nancy
HostName 10.21.43.150
User woot
Port 22
Compression no

- When a user types ssh jancy, the matching patterns are Host jancy and Host *. The options used are,
Host jancy
HostName 10.2.33.58
Port 22
User root
Compression yes
PermitLocalCommand no

- When the user types ssh trial, the matching patterns are Host trial and Host *,
Host trial
HostName 10.2.33.57
PermitLocalCommand yes
User root
Port 22
Compression yes

- For all other remote connections, the ssh client will use Host *cy !jancy and Host *.
Override SSH Config File Options
SSH client reads the configuration file in the following order,
- Options are mentioned in the command line.
- Options mentioned in the ~/.ssh/config file.
- Options mentioned in the /etc/ssh/ssh_config file.

Now if the user wants to connect to amp150.arubathena.com but with a different user, then it can be over-ridden in the command line as below,
Similar Reads
CCNA Tutorial for Beginners This CCNA Tutorial is well-suited for the beginner as well as professionals, and It will cover all the basic to advanced concepts of CCNA like Components of Computer Networking, Transport Layer, Network Layer, CCNA training, Cisco Networking, Network Design, Routing and Switching, etc. which are req
8 min read
Basics of Computer Networking
Components of Computer Networking
NIC Full Form - Network Interface CardNIC stands for Network Interface Card. NIC is additionally called Ethernet or physical or network card. NIC is one of the major and imperative components of associating a gadget with the network. Each gadget that must be associated with a network must have a network interface card. Even the switches
4 min read
What is a Network Switch and How Does it Work?The Switch is a network device that is used to segment the networks into different subnetworks called subnets or LAN segments. It is responsible for filtering and forwarding the packets between LAN segments based on MAC address. Switches have many ports, and when data arrives at any port, the destin
9 min read
What is Network Hub and How it Works?Hub in networking plays a vital role in data transmission and broadcasting. A hub is a hardware device used at the physical layer to connect multiple devices in the network. Hubs are widely used to connect LANs. A hub has multiple ports. Unlike a switch, a hub cannot filter the data, i.e. it cannot
6 min read
Introduction of a RouterNetwork devices are physical devices that allow hardware on a computer network to communicate and interact with one another. For example Repeater, Hub, Bridge, Switch, Routers, Gateway, Router, and NIC, etc. What is a Router?A Router is a networking device that forwards data packets between computer
12 min read
Types of Ethernet CableAn ethernet cable allows the user to connect their devices such as computers, mobile phones, routers, etc, to a Local Area Network (LAN) that will allow a user to have internet access, and able to communicate with each other through a wired connection. It also carries broadband signals between devic
5 min read
Transport Layer
Transport Layer responsibilitiesThe transport Layer is the second layer in the TCP/IP model and the fourth layer in the OSI model. It is an end-to-end layer used to deliver messages to a host. It is termed an end-to-end layer because it provides a point-to-point connection rather than hop-to-hop, between the source host and destin
5 min read
Introduction of Ports in ComputersA port is basically a physical docking point which is basically used to connect the external devices to the computer, or we can say that A port act as an interface between the computer and the external devices, e.g., we can connect hard drives, printers to the computer with the help of ports. Featur
3 min read
What is TCP (Transmission Control Protocol)?Transmission Control Protocol (TCP) is a connection-oriented protocol for communications that helps in the exchange of messages between different devices over a network. It is one of the main protocols of the TCP/IP suite. In OSI model, it operates at the transport layer(Layer 4). It lies between th
5 min read
TCP 3-Way Handshake ProcessThe TCP 3-Way Handshake is a fundamental process that establishes a reliable connection between two devices over a TCP/IP network. It involves three steps: SYN (Synchronize), SYN-ACK (Synchronize-Acknowledge), and ACK (Acknowledge). During the handshake, the client and server exchange initial sequen
6 min read
User Datagram Protocol (UDP)User Datagram Protocol (UDP) is a Transport Layer protocol. UDP is a part of the Internet Protocol suite, referred to as UDP/IP suite. Unlike TCP, it is an unreliable and connectionless protocol. So, there is no need to establish a connection before data transfer. The UDP helps to establish low-late
10 min read
Network Layer
IPv4 Addressing
Subnetting
Data Link Layer
Physical Layer
Cisco Networking Devices
Network Devices (Hub, Repeater, Bridge, Switch, Router, Gateways and Brouter)Network devices are physical devices that allow hardware on a computer network to communicate and interact with each other. Network devices like hubs, repeaters, bridges, switches, routers, gateways, and brouter help manage and direct data flow in a network. They ensure efficient communication betwe
9 min read
Collision Detection in CSMA/CDCSMA/CD (Carrier Sense Multiple Access/ Collision Detection) is a media access control method that was widely used in Early Ethernet technology/LANs when there used to be shared Bus Topology and each node ( Computers) was connected by Coaxial Cables. Nowadays Ethernet is Full Duplex and Topology is
7 min read
Collision Domain and Broadcast Domain in Computer NetworkPrerequisite - Network Devices, Transmission Modes The most common network devices used are routers and switches. But we still hear people talking about hubs, repeaters, and bridges. Do you ever wonder why these former devices are preferred over the latter ones? One reason could be: 'because they ar
5 min read
Difference between layer-2 and layer-3 switchesA switch is a device that sends a data packet to a local network. What is the advantage of a hub? A hub floods the network with the packet and only the destination system receives that packet while others just drop due to which the traffic increases a lot. To solve this problem switch came into the
5 min read