春江暮客

春江暮客的个人学习分享网站

Five Easy Steps to Achieve Linux Passwordless Login

2019-01-02 Miscellaneous
Five Easy Steps to Achieve Linux Passwordless Login

SSH, which stands for Secure Shell, is a widely used and reliable program for securely executing commands remotely on Linux systems. SCP, used for secure file transfers, is also based on the SSH security protocol.

When you have many servers, frequently typing passwords can be quite troublesome. So, how can you log in without a password while maintaining security? Of course, it’s possible. Here, we’ll outline five steps to achieve passwordless login for Linux servers such as RHEL/CentOS 7.x/6.x/5.x and Fedora.

The important point is that this does not mean “turning security off.” It means replacing password-based login with SSH key-based authentication, which is usually a better operational choice when your private key is handled properly.

Five Easy Steps to Achieve Linux Passwordless Login

Let’s look at the environment:

    SSH Client : 192.168.1.12 ( Mac )
    SSH Remote Host : 192.168.1.11 ( CentOS 7 )

This article will demonstrate passwordless login from a Mac to a remote CentOS 7 server.

1. Generate SSH-Keygen Key Pair on Your Local Mac

First, use the following command in your Mac terminal to generate a key pair:

    ➜  Desktop ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/yourname/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/yourname/.ssh/id_rsa.
    Your public key has been saved in /Users/yourname/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:skotytjPaTzhiuQgyC41DbrFiiwgShtQCw7kEyNgX+M tengbozhang@tengbos-Air
    The key's randomart image is:
    +---[RSA 2048]---+
    |..*Bo.+.        |
    |.o++oo.o        |
    | oo= . .        |
    |  = + ..        |
    |  O o S         |
    | . B            |
    | ooB. o         |
    |.o===.oo        |
    |oo==o=+E        |
    +----[SHA256]-----+

2. Create the .ssh Directory on the Remote Linux Host (CentOS 7)

Log in to CentOS 7 from your Mac terminal and execute the command to create the .ssh directory:

    ➜  Desktop ssh root@192.168.1.11
    root@192.168.1.11's password:
    Permission denied, please try again.
    root@192.168.1.11's password:
    Last failed login: Wed Jan  2 09:35:47 CST 2019 from 192.168.1.12 on ssh:notty
    There was 1 failed login attempt since the last successful login.
    Last login: Wed Jan  2 09:34:52 2019 from 192.168.1.12
    ➜  ~ ll .ssh
    total 4.0K
    -rw-r--r--. 1 root root 368 Dec 17 11:07 known_hosts
    # If it doesn't show, use mkdir .ssh to create it. I already have the directory, so I don't need to create it.

3. Upload the Generated Public Key to the Remote Host (CentOS 7)

Use SSH to upload the public key generated in the first step from your local machine to the authorized_keys file within the .ssh directory on the remote CentOS 7 host:

    ➜  Desktop cat ~/.ssh/id_rsa.pub | ssh root@192.168.1.11 'cat >> .ssh/authorized_keys'
    root@192.168.1.11's password:
    ➜  Desktop

4. Set Permissions for .ssh and authorized_keys on the Remote Host

    ➜  Desktop ssh root@192.168.1.11 "chmod 700 .ssh; chmod 600 .ssh/authorized_keys"
    root@192.168.1.11's password:

Using 600 for authorized_keys is a safer default and matches what many SSH setups expect for authentication files.

5. Mac Passwordless Login to Remote Host (CentOS 7)

    ➜  Desktop ssh root@192.168.1.11
    Last login: Wed Jan  2 09:35:52 2019 from 192.168.1.12
    ➜  ~

With these steps, you’ve successfully achieved passwordless login! If you have multiple servers, you can follow the same method: upload the public key to the authorized_keys file within the user’s .ssh folder on each remote host, set the correct directory and file permissions, and you’ll be able to log in without a password.

A Shorter Option: ssh-copy-id

If your local system already includes ssh-copy-id, steps 3 and 4 can often be replaced with a single command:

ssh-copy-id root@192.168.1.11

It appends your public key to the remote authorized_keys file automatically, which is often more convenient than piping cat manually.

Common Troubleshooting Checks

If the server still asks for a password after you upload the public key, check these items first:

  • Whether the remote .ssh directory permission is 700
  • Whether the authorized_keys permission is 600
  • Whether you are logging in as the same user that owns the uploaded key
  • Whether PubkeyAuthentication is disabled in /etc/ssh/sshd_config

友情链接

其它