- Ssh Config Default User
- Ssh Client Config
- Ssh Config Default Username
- Linux Default Ssh Config File
- Ssh Config File Location
- Default /etc/ssh/sshd_config
Specifies that ssh(1) should only use the authentication identity files configured in the sshconfig files, even if ssh-agent(1) offers more identities. The argument to this keyword must be 'yes' or 'no'. This option is intended for situations where ssh-agent offers many different identities. The default is 'no'. The sshconfig client configuration file has the following format. Both the global /etc/ssh/sshconfig and per-user /ssh/config have the same format. Empty lines and lines starting with '#' are comments. G Causes ssh to print its configuration after evaluating Host and Match blocks and exit. By calling ssh -G host you will get options used for connecting to specific host, which can be helpful for debugging conditional matches in sshconfig. Also setting more verbose log level (-vvv) can help with debugging config.
If you are aware of the SSH basics, you already know that SSH uses port 22 by default.
When you connect to a server via SSH, most of the time you don't provide any port information. And in such cases, your connection goes to the port 22 of the SSH server.
You can change the default port from 22 a port number of your choice using the following steps:
- Open the
/etc/ssh/sshd_config
file for editing. - Locate the line that has
Port 22
(if it is commented out with #, remove the # as well). - Change the line to Port 2522 (or any number of your choice between 1024 and 65535).
- Make sure that the new port is allowed by the firewalls (if you have any).
- Restart ssh daemon with
sudo systemctl restart sshd
. - From now onwards, you'll have to specify the port to make the ssh connection
ssh user@ip_address_of_server -p 2522
.
Let me show you the steps in details and also tell you why you may consider changing the
Why change the default SSH port?
One of the most elementary tricks for securing SSH server is to change the default SSH port number 22.
Why? Because a number of bot scripts try the brute force attacks on the default port 22. Most of these scripts don't always scan for open ports, and they target the default ports for various known services like SSH.
Changing the default SSH port reduces number of such attacks. There are other ways to improve the security of your SSH server. If interested, please follow these actionable tips for improving SSH server security.
Now that you know why you would change the default SSH port, let's see how to do it.
Allow traffic on the new port by changing the firewall settings
If you have a firewall set or custom ipconfig or ifconfig or if you are using selinux, you must allow the new ssh port before making the changes. Otherwise you may lock yourself out without an SSH access.
Now this part depends upon what kind of firewall or routing you are using.
If you are using UFW, you can use the following command to allow port 2522:
If you are using iptables, you should use this command:
On Fedora, CentOS, Red Hat, the firewall is managed by firewalld and you can use this command:
On CentOS and Red Hat, you may also have to change the SELinux rules:
Now that you have put the correct firewall settings, let's move on to changing the SSH port.
Changing the default SSH port
Usually, the ssh configuration file is located at /etc/ssh/sshd_config
. You'll have to use a terminal-based editor like Vim or Nano or Emacs to edit the file.
Distributions like Ubuntu have Nano installed by default so you can use it for opening the file in edit mode like this:
As you can see, you'll have to be a sudo user or root to edit the ssh configuration.
Scroll down a bit and you'll see a line with Port 22
. If it starts with #
, it means the line is commented out. The commented out lines gives you the default settings.
So if you see # Port 22
, it means that default port is 22.
Change this line with a port number of your choice. In Linux, port number 0-1023 are usually reserved for various services. It will be good to avoid using anything between 0 and 1023 to avoid conflicts.
You can use any other port number between 1024 and 65535. I am using 2522 in the example. Make sure to remove the #
before the Port line.
Save your changes and exit the editor. If you are using Nano, use Ctrl+X to save and exit.
The next step is to restart the ssh service. Most modern system use systemd services so you can use the following command:
Now if you want to access the SSH server, you'll have to specify the port number:
Was it helpful?
I hope you find this tutorial helpful in changing the SSH port. Now that you have changed the port, you'll have to use it all the time you want to connect to the server via SSH and that could be annoying.
This is why I recommend using SSH config file to save the settings for easy and quick access.
Become a Member for FREE
Join the conversation.
If you are even a tad bit familiar with SSH, you know that you can use it to connect to remote Linux systems.
Using SSH to connect to remote system is simple. All you need to do is to use a command like this:
This connects to the default SSH port 22. You may specify the port as well if you want.
Now this is all plain and simple if you just have one server. Even if you don’t remember the server’s IP address, you can perform a reverse search to the history using the famous terminal keyboard shortcut Ctrl+R and find the SSH command you used in the past.
But things get complicated when you have several servers to manage. I have around ten servers that I connect to from time to time. Some are production servers and some are test servers.
Now keeping a track of these servers is not easy. Even if I can find the SSH commands from the history, it is difficult to guess which IP belongs to which server.
Of course, I can open my dashboards on Linode, UpCloud, DigitalOcean and Google Cloud to get the IP or keep a list on my local system.
A better and easier way is to use SSH config file.
Ssh Config Default User
Using SSH config file for easily connecting to remote servers
The SSH config file allows you to create different profiles for different host configurations. There is no limit to such profiles and you may add as many as possible.
So, if you connect to multiple remote systems via SSH, creating SSH profiles will be a good move to save your time.
Let me show you how to use it.
Step 1: Create the SSH config file
When you install SSH, you’ll have a ~/.ssh directory created automatically. This direct contains your public key, private key a known_hosts file. Your config is also stored here.
At least on Ubuntu, the SSH config file is not created by default. You can easily create this file using the touch command like this:
Step 2: Add an SSH profile in the config file
Ssh Client Config
Now that you have the SSH config file, you can edit it using Vim or Nano. Let me show you an example of the syntax which you should follow.
Let’s say you connect to a server with IP 275.128.172.46. Your username is Alice and the server is used for hosting your website. To harden SSH security, you use port 1500 instead of the default SSH port 22.
You can add all this information in the following manner in your ~/.ssh/config file:
Just save the information in the file. No need to restart any service.
Now, instead of writing a long command like this:
You can just use this command (tab completion works as well):
When you run the above command, ssh looks for a Host named website in the ~/.ssh/config. If it finds a host with that name, it gets all the information related and used it for making an SSH connection.
You might wonder about a few things, so I’ll mention it here:
- There is no space or tab indention restriction while entering the host information. Space or tab indention are used for making the config file easily understandable.
- The Hostname can be the IP address of the server or a hostname that can be resolved on your network.
- All the parameters like hostname, user and port are optional. However, I personally advise keeping at least hostname because that’s what you need (and you forget) most of the time.
- If your SSH config file is wrongly configured, it will result in an error when you try to use it for SSH connection.
- You cannot save passwords in SSH config. I advise adding your public SSH key to the server for easy access.
Ssh Config Default Username
Step 3: Adding multiple profiles in SSH config file
The previous step gave you an idea about how to add an SSH profile. Let’s take it to the next step by adding multiple profiles in it.
Here’s what the SSH config file looks like now:
This time, I have added four different SSH profiles in it.
Did you notice the Host * entry at the end of the file? You can use this entry to for adding a parameter common to all profiles if that parameter hasn’t been mentioned for the profile explicitly.
So if I try to use the main-server SSH profile, it will automatically take root user.
ssh main-server = ssh root@275.128.172.49
Order of the SSH configuration
The ssh configuration follows the following order:
- command-line options
- user’s configuration file (~/.ssh/config)
- system-wide configuration file (/etc/ssh/ssh_config)
This means that the priority is given to the command you enter and then it looks into ~/.ssh/config and then in /etc/ssh/ssh_config.
So, if you want to override a profile, you can do that using the -o option of the ssh command.
For example, if I use this command:
It will take user bob instead of the user alice as defined in the ~/.ssh/config (in the previous step).
There’s a lot more to SSH config
To be honest, there is so much more to SSH config file that cannot be covered in a single article. You can use name/IP matching, subnets and what not.
Linux Default Ssh Config File
The scope of this article was to introduce you to SSH config and help you create SSH profiles for easily connecting to various remote Linux systems.
You can always refer to the man page of ssh_config to know more about the parameters you can use while creating your SSH config file.
Ssh Config File Location
I hope this SSH tip was helpful to you. If you already use SSH config file and have a some nifty tip with you, do share it with the rest of us in the comment section.
Become a Member for FREE
Default /etc/ssh/sshd_config
Join the conversation.