This guide provides an overview of different SSH authentication methods, with a particular focus on SSH key-based authentication. Additionally, this guide will walk you through the steps on how to configure SSH key-based authentication in Linux and Unix-like operating systems.
This Step-By-Step guide covers the following topics:
- Types of SSH Authentication methods,
- Overview of SSH Key-based authentication method,
- Advantages of Key-based authentication method,
- Generating a key pair,
- Copy the public key to the SSH server,
- Disable password-based authentication,
- Test SSH password authentication,
- And finally add new clients to SSH server.
Table of Contents
Types of SSH Authentication Methods
Secure Shell, shortly SSH, is the cryptographic network protocol that allows you to securely communicate/access a remote system over unsecured network, for example Internet.
Whenever you send a data over an unsecured network using SSH, the data will be automatically encrypted in the source system, and decrypted in the destination side.
SSH provides four authentication methods namely password-based authentication,key-based authentication, Host-based authentication, and Keyboard authentication.
The most commonly used authentication methods are password-based and key-based authentication methods.
In password-based authentication, all you need is the password of the remote system's user. If you know the password of remote user, you can access the respective system using "ssh user@remote-system-ip" command.
On the other hand, in key-based authentication, you need to generate SSH key pairs and upload the SSH public key to the remote system in order to communicate it via SSH.
Each SSH key pair consists of a private key and public key. The private key should be kept within the client system, and the public key should be uploaded to the remote systems. You should not disclose the private key to anyone.
What is SSH Key-based Authentication?
SSH key-based authentication is a method of authenticating a user to an SSH server using a pair of cryptographic keys. The keys are generated using a key-pair generator, such as the ssh-keygen command. The private key is kept secret by the user, while the public key is shared with the SSH server.
When the user attempts to connect to the SSH server, the server will first check the user's public key against a list of authorized keys. If the public key is found, the server will then challenge the user to provide the corresponding private key. If the user can successfully provide the private key, they will be authenticated and granted access to the SSH server.
SSH key-based authentication is considered to be more secure than password-based authentication because it does not rely on passwords, which can be easily guessed or stolen. Additionally, SSH key-based authentication can be used to automate logins, which can be useful for tasks such as running scripts or transferring files.
Key-based authentication is the most secure type of SSH authentication. However, it can be more difficult to set up and use than password-based or keyboard-interactive authentication.
Here are the steps on how to set up SSH key-based authentication:
- Generate a key pair on your computer using the ssh-keygen command.
- Copy the public key to the SSH server's authorized_keys file.
- Disable password-based authentication on the SSH server.
- Once you have set up SSH key-based authentication, you can connect to the SSH server without having to enter a password.
Advantages of Key-based Authentication
Here are some advantages of SSH key-based authentication:
- Enhanced Security: SSH key-based authentication is significantly more secure than password-based authentication. It is resistant to brute-force attacks and is highly unlikely to be guessed or cracked.
- Protection Against Password Theft: Since there are no passwords involved, there's no risk of someone intercepting and stealing your password during the authentication process.
- Key Pairs for User and Server: SSH key pairs consist of a public key and a private key. The public key is placed on the server, and the private key is kept on the client machine. This separation adds an extra layer of security, as the private key never leaves your device.
- Convenience: Once set up, SSH key-based authentication allows you to log in without having to remember or type passwords. It's especially useful for frequent logins or automation tasks.
- Automation and Scripting: SSH key-based authentication is widely used in automation and scripting, as it allows processes to securely connect to remote servers without manual password input.
- Multi-Factor Authentication (MFA) Support: SSH key-based authentication can be used in conjunction with MFA, providing an additional layer of security by requiring a second authentication factor (e.g., a passphrase or a hardware token) in addition to the key.
- Revocation and Key Management: If a private key is compromised or needs to be revoked for any reason, you can simply remove the corresponding public key from the authorized keys list on the server. This immediate control over access is advantageous.
- Auditing and Accountability: SSH key-based authentication provides a clear audit trail of who accessed a server, as each user's access is tied to their unique key. This can be beneficial for compliance and security monitoring.
- Compatibility: SSH key-based authentication is widely supported across different operating systems and SSH clients, making it a versatile and cross-platform solution.
- Reduced Password Fatigue: Users don't have to remember or manage complex passwords for multiple servers, reducing the risk of using weak or reused passwords.
- Longer Key Lengths: SSH key pairs use longer cryptographic keys compared to typical passwords, making them more resilient to attacks.
In summary, SSH key-based authentication offers a combination of strong security and convenience, making it a preferred choice for securing remote access to servers and systems.
Hope you got the basic idea about SSH and its authentication methods. Now let us move on to see how to configure SSH key-based authentication in Linux step by step.
Configure SSH Key-based Authentication in Linux
For the purpose of this guide, I will be using two Debian Linux systems. Debian 11 with IP address 192.168.1.101 is the local system and Debian 12 server with IP address 192.168.1.20 acts as remote system i.e. ssh server.
Local system details:
- OS : Debian 11 Cinnamon Desktop
- IP address : 192.168.1.101 /24
Remote system (SSH Server) details:
- OS : Debian 12 Server
- IP address : 192.168.1.20/24
Step 1: Generate SSH Key Pair in Local System
As stated already, in SSH key-based authentication method, the public key should be uploaded to the remote system that you want to access via SSH. The public keys will usually be stored in a file called ~/.ssh/authorized_keysin the remote SSH systems.
Important note: Do not generate key pairs as root, as only root would be able to use those keys. Create key pairs as normal user.
Now, let us create the SSH key pair in the local system. To do so, run the following command in your local client system.
$ ssh-keygen
The above command will create a 3072 bit RSA key pair. Enter the passphrase twice. More importantly, remember your passphrase. You'll need it later.
Sample output:
Generating public/private rsa key pair. Enter file in which to save the key (/home/ostechnix/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/ostechnix/.ssh/id_rsa Your public key has been saved in /home/ostechnix/.ssh/id_rsa.pub The key fingerprint is: SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ostechnix@pvedebian The key's randomart image is: +---[RSA 3072]----+ | Bo..o..o.o | |* =.o.o o . | |.+ + o+.. | |..o =.E+ | |oX + .. S | |O * o | |++ = | |.oX | |*=o+ | +----[SHA256]-----+
In case you have already created the key pair, you will see the following message. Just type "y" to overwrite the existing key .
/home/username/.ssh/id_rsa already exists. Overwrite (y/n)?<strong>y</strong>
Note:
Please be mindful that providing passphrase is optional. If you give one, you'll be asked to enter the password every time when you try to SSH into a remote system unless you are using any SSH agent to store the password.
If you don't want passphrase (not safe though), simply press ENTER key twice when you are prompted to provide the passphrase. However, I recommend you to use a passphrase.
Using a password-less ssh key is generally not a good idea from a security point of view. They should be limited to very specific cases such as services having to access a remote system without the user intervention (e.g. remote backups with rsync, unison, …).
If you already have a ssh key without a passphrase in private file ~/.ssh/id_rsa and wanted to update key with passphrase, use the following command:
$ ssh-keygen -p -f ~/.ssh/id_rsa
Sample output:
Enter new passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved with the new passphrase.
Step 2: Copy SSH Public Key to SSH Server (Remote System)
We have created the key pair in the local system. Next, copy the SSH public key to your remote SSH server using command:
$ ssh-copy-id ostechnix@192.168.1.20
Here, I will be copying the local (Debian 11 Desktop) system's public key to the remote system (Debian 12 Server in my case).
Technically speaking, the above command will copy the contents of local system's ~/.ssh/id_rsa.pub key into remote system's ~/.ssh/authorized_keys file. Clear? Good.
Type yes to continue connecting to your remote SSH server and then enter the sudo (or root user's) password of the remote system.
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys ostechnix@192.168.1.20's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'ostechnix@192.168.1.20'" and check to make sure that only the key(s) you wanted were added.
If you have already copied the key, but want to update the key with new passphrase, use -f option to overwrite the existing key like below.
$ ssh-copy-id -f ostechnix@192.168.1.20
We have successfully added the local system's SSH public key to the remote system.
We can now completely disable the password-based authentication in the remote system. Because, we already configured key-based authentication, so we don't need password-base authentication anymore.
Step 3: Disable SSH Password-based Authentication in Remote SSH Server
You need to perform the following commands as root or sudo user.
To disable password-based authentication, go to your remote system's console andedit /etc/ssh/sshd_config configuration file using any editor:
$ sudo nano /etc/ssh/sshd_config
Find the following line. Uncomment it and set it's value as no.
PasswordAuthentication no
Restart sshd service to take effect the changes.
$ sudo systemctl restart sshd
Step 4: Test SSH Key-based Authentication
To verify if you've correctly configured the key-based authentication, you need to access the remote system from the local system via SSH.
Go to your local system and SSH into your remote server using command:
$ ssh ostechnix@192.168.1.20
You will be prompted to enter the passphrase to login to the remote ssh system. Type the password for the key and hit ENTER key.
If you try to access the remote system from a CLI-only system, you will see the following output:
<strong>Enter passphrase for key '/home/sk/.ssh/id_rsa':</strong> Last login: Thu Sep 7 14:12:11 2023 from 192.168.1.101 ostechnix@debian12:~$
As you noticed, we have logged-in to the remote system's account using passphrase which we created earlier using ssh-keygen command, not using the remote user account's actual password.
If you try to ssh from another client system, you will get this error message. Say for example, I tried to SSH into my Debian 12 server from one of a Ubuntu system using command:
$ ssh sk@192.168.1.20
Sample output:
The authenticity of host '192.168.1.20 (192.168.1.20)' can't be established. ECDSA key fingerprint is 67:fc:69:b7:d4:4d:fd:6e:38:44:a8:2f:08:ed:f4:21. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.1.20' (ECDSA) to the list of known hosts. <strong>Permission denied (publickey)</strong>.
As you see in the above output, I can not SSH into my remote SSH server from other systems.
Step 5: Adding New Client System's Keys to SSH Server
This is very important. Like I said already, you can't access the remote system via SSH, except the one you configured (in our case, it's Debian 11 Desktop).
I want to give permissions to more clients to access the remote SSH server. What should I do? Simple. You need to generate the SSH key pair in all your client systems and copy the ssh public key manually to the remote server that you want to access via SSH.
To create SSH key pair on your client systems, run:
$ ssh-keygen
Enter the passphrase twice. After the key pair is generated, copy the public ssh key (not private key) to your remote server manually.
Display the pub key using command:
$ cat ~/.ssh/id_rsa.pub
You should see an output something like below.
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCt3a9tIeK5rPx9p74/KjEVXa6/OODyRp0QLS/sLp8W6iTxFL+UgALZlupVNgFjvRR5luJ9dLHWwc+d4umavAWz708e6Na9ftEPQtC28rTFsHwmyLKvLkzcGkC5+A0NdbiDZLaK3K3wgq1jzYYKT5k+IaNS6vtrx5LDObcPNPEBDt4vTixQ7GZHrDUUk5586IKeFfwMCWguHveTN7ykmo2EyL2rV7TmYq+eY2ZqqcsoK0fzXMK7iifGXVmuqTkAmZLGZK8a3bPb6VZd7KFum3Ezbu4BXZGp7FVhnOMgau2kYeOH/ItKPzpCAn+dg3NAAziCCxnII9b4nSSGz3mMY4Y7 ostechnix@pvedebian
Copy the entire contents (via USB drive or any medium) and go to your remote server's console. Create a directory called ssh in the home directory as shown below.
$ mkdir -p ~/.ssh
Next, append the your client system's pub key that you generated in the previous step in a file called "~/.ssh/authorized_keys":
$ echo {Your_public_key_contents_here} >> ~/.ssh/authorized_keys
Restart ssh service on the remote system. Now, you'll be able to SSH to your server from the new client.
If manually adding ssh pubkey seems difficult, enable password-based authentication temporarily in the remote system and copy the key using "ssh-copy-id" command from your local system and finally disable the password-based authentication.
Frequently Asked Questions
Here is a list of most commonly asked questions and their answers (FAQ) about SSH Key-based authentication.
Q: What is SSH key-based authentication?A: SSH key-based authentication is a secure method for logging into remote systems or servers over a network. Instead of using a password, it relies on a pair of cryptographic keys – a public key and a private key – to verify your identity.
Q: How does SSH key-based authentication work?A: SSH key-based authentication works by generating a unique pair of keys. The public key is placed on the remote server, while the private key remains securely stored on your local machine. When you connect to the server, it checks whether your private key matches the public key stored on the server. If they match, you gain access without needing a password.
Q: What are the advantages of SSH key-based authentication?A: Here is a list of important features.- Enhanced Security: SSH keys are significantly harder to crack than passwords, making it a more secure authentication method.- Convenience: Once set up, you can log in without entering a password every time.- Automation: SSH keys are commonly used for automated processes, like server-to-server communication or script execution, without user intervention.- Reduced Password Exposure: Since you don't enter passwords, there's no risk of password interception during login.
Q: How do I create SSH keys?A: You can generate SSH keys using the ssh-keygen command on your local machine. It will create a pair of keys – one public and one private. You'll usually find them in your user's .ssh directory.
Q: Can I use the same SSH key pair on multiple servers?A: Yes, you can use the same SSH key pair to access multiple servers. However, it's considered more secure to have a unique key pair for each server.
Q: How do I configure SSH key-based authentication on a remote server?A: You typically append your public key to the ~/.ssh/authorized_keys file on the remote server. This allows the server to recognize your public key when you attempt to connect with your private key.
Q: What happens if I lose my private key?A: If you lose your private key, you won't be able to use it for authentication. You can generate a new key pair and update your authorized keys on the servers you need to access.
Q: Is SSH key-based authentication compatible with all operating systems and SSH clients?A: Yes, SSH key-based authentication is widely supported and can be used with various operating systems (Linux, macOS, Windows with tools like PuTTY) and SSH clients.
Q: Can SSH keys be password-protected?A: Yes, you can add an additional layer of security by password-protecting your private key. This requires entering a password each time you use the private key.
Q: Is SSH key-based authentication suitable for all use cases?A: While SSH key-based authentication is highly secure and convenient, it may not be suitable for all use cases. For example, it might be less practical for scenarios involving a large number of users or temporary access.
Related Read:
- How To Setup Multi-Factor Authentication For SSH In Linux
- Find If A User Is Using Password-based Or Key-based SSH Authentication
- How To Prevent SSH Brute Force Attacks Using Fail2ban In Linux
- SSLH – Share A Same Port For HTTPS And SSH
- ScanSSH – Fast SSH Server And Open Proxy Scanner
Conclusion
SSH Key-based authentication provides an extra layer protection for your Linux servers from brute-force attacks. As you can see, configuring key-based authentication is not that difficult. Using key-based authentication is one of the recommended method to keep your Linux server safe and secure.
以上是如何在Linux中配置基于SSH密钥的身份验证的详细内容。更多信息请关注PHP中文网其他相关文章!

Linux和Windows的安全模型各有优势。Linux提供灵活性和可定制性,通过用户权限、文件系统权限和SELinux/AppArmor实现安全。Windows则注重用户友好性,依赖WindowsDefender、UAC、防火墙和BitLocker保障安全。

Linux和Windows在硬件兼容性上不同:Windows有广泛的驱动程序支持,Linux依赖社区和厂商。解决Linux兼容性问题可通过手动编译驱动,如克隆RTL8188EU驱动仓库、编译和安装;Windows用户需管理驱动程序以优化性能。

Linux和Windows在虚拟化支持上的主要区别在于:1)Linux提供KVM和Xen,性能和灵活性突出,适合高定制环境;2)Windows通过Hyper-V支持虚拟化,界面友好,与Microsoft生态系统紧密集成,适合依赖Microsoft软件的企业。

Linux系统管理员的主要任务包括系统监控与性能调优、用户管理、软件包管理、安全管理与备份、故障排查与解决、性能优化与最佳实践。1.使用top、htop等工具监控系统性能,并进行调优。2.通过useradd等命令管理用户账户和权限。3.利用apt、yum管理软件包,确保系统更新和安全。4.配置防火墙、监控日志、进行数据备份以确保系统安全。5.通过日志分析和工具使用进行故障排查和解决。6.优化内核参数和应用配置,遵循最佳实践提升系统性能和稳定性。

学习Linux并不难。1.Linux是一个开源操作系统,基于Unix,广泛应用于服务器、嵌入式系统和个人电脑。2.理解文件系统和权限管理是关键,文件系统是层次化的,权限包括读、写和执行。3.包管理系统如apt和dnf使得软件管理方便。4.进程管理通过ps和top命令实现。5.从基本命令如mkdir、cd、touch和nano开始学习,再尝试高级用法如shell脚本和文本处理。6.常见错误如权限问题可以通过sudo和chmod解决。7.性能优化建议包括使用htop监控资源、清理不必要文件和使用sy

Linux管理员的平均年薪在美国为75,000至95,000美元,欧洲为40,000至60,000欧元。提升薪资可以通过:1.持续学习新技术,如云计算和容器技术;2.积累项目经验并建立Portfolio;3.建立职业网络,拓展人脉。

Linux的主要用途包括:1.服务器操作系统,2.嵌入式系统,3.桌面操作系统,4.开发和测试环境。Linux在这些领域表现出色,提供了稳定性、安全性和高效的开发工具。

互联网运行不依赖单一操作系统,但Linux在其中扮演重要角色。Linux广泛应用于服务器和网络设备,因其稳定性、安全性和可扩展性受欢迎。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3汉化版
中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。