Linux server via SSH connection under Windows without password

First, configure the environment

1, the present system: Windows 10 Pro (64-bit)
2, server: Tencent cloud CentOS 7.2 (64-bit)
. 3, the SSH connection software: Xshell 5

Second, configure SSH without password login procedure

1, generate public and private keys in the personal PC,
a public key 2, the resulting uploaded to the server, import the public key to the certificate file, change the permissions
3, import the private key by Xshell 5, perform SSH connection

Third, preparation

1, Linux server newly created username and password, you can connect via a user name and password, click the New button to open the Xshell 5



2, a new connection



After 3, enter the server's user name and password to connect to the server through a user name and password, and configure SSH connection is prohibited by password

4. Click OK, and select the connection you just configured to connect information



5, before performing SSH configuration, the need to upload and download a Linux server installation tool, after the installation is complete Xshell can upload files to the server, or download files from the server machine to the machine, connected to the server via the Xshell, by command: rz- to upload files, sz- for file downloads
yum install -y lrzsz

6, to see whether the server is installed SSH service SSH want to connect to a server, you must install the SSH service on the server and start the service

rpm -qa | grep ssh   #查看是否安装了SSH服务

If not installed, enter

yum install openssh-server #安装openssh服务

To start the service after the installation

service sshd restart        #重启SSH服务
service sshd start          #启动服务
service sshd stop           #停止服务
netstat -antp | grep sshd   #查看是否启动22端口
chkconfig sshd on           #允许SSH开机启动 chkconfig sshd off #禁止SSH开机启动 

Fourth, the SSH connection configuration

1. Create a user on the server for SSH connection, we do not have the root user SSH

① create a user group admin

groupadd admin

② add a new user

useradd -d /home/用户名 -s /bin/bash -m 用户名   #创建用户,用户名自取,参数d表示指定用户的主目录,参数s指定用户的shell,参数m表示如果该目录不存在,则创建该目录
passwd 用户名    #给用户设置密码,然后输入两次密码就好

③ The adminSSH added to user groups in admin

usermod -a -G admin adminSSH

④ set sudo permissions for users adminSSH

visudo

visudo command to open the file / etc / sudoers, find the following line

root ALL=(ALL) ALL

Add a row

adminSSH(用户) ALL=(ALL) NOPASSWD: ALL

上面的NOPASSWD表示,切换sudo的时候,不需要输入密码,我喜欢这样比较省事。如果出于安全考虑,也可以强制要求输入密码。
<strong>用户创建好后切换到adminSSH用户连接</strong>
2、本地生成公钥和私钥,这边是使用git 中 shell产生的,其他类Unix系统可以直接在终端生成

ssh-keygen -t rsa #该命令产生的密钥是通过rsa算法加密生成的
ssh-keygen -t dsa #该命令产生的密钥是通过dsa算法加密生成的

生成密钥过程中可以一路回车,不给定密钥文件名字和密码,使用默认的密钥文件名称和空密码


3、将刚刚生成的公钥(带.pub结尾的文件)ssh_rsa.pub上传到Linux服务器刚刚新建的用户目录下(即/home/adminSSH目录下),此时我们就可以用rz命令了


4、将上传的公钥文件的内容追加到authorized_keys文件,使用如下命令:

cat ssh_rsa.pub > ~/.ssh/authorized_keys  #如果~/.ssh目录不存在,则自己新建

</strong>
5、修改SSH配置文件/etc/ssh/sshd_config

sudo vi /etc/ssh/sshd_config

在配置文件中找到 #Port 22,修改默认的端口,范围可以从1025到65536,此处改为Port 6983

修改如下设置并确保去除了#号

Protocol 2                     #选择的 SSH 协议版本,可以是 1 也可以是 2 
PermitRootLogin no                #禁止root用户登录
PubkeyAuthentication yes       #是否允许 Public Key ?当然允许啦!只有 version 2
PasswordAuthentication no    #禁止使用密码登录,密码验证是否需要看个人需求
PermitEmptyPasswords no    #否允许以空的密码登入:若上面那一项如果设定为 yes 的话,这一项就最好设定为 no 

最后,在配置文件的末尾添加一行用来指定可以登录的用户

AllowUsers adminSSH

保存退出后,修改authorized_keys和.ssh的文件权限

sudo chmod 700 ~/.ssh/
sudo chmod 600 ~/.ssh/authorized_keys

重启SSH服务

sudo service sshd restart

6、使用Xshell连接,此时如果在用用户名和密码连接服务器已发现连接不上了,因为刚在设置了禁止用密码登录
①打开Xshell 5—工具—用户密钥管理者—导入—将生成的私钥导入—关闭
②打开刚刚的用用户名和密码登录的连接属性,或者新建一个连接,修改端口号


确定—连接—成功通过SSH免密码连接服务器


五、登录失败问题解决######

出现这一情况主要考虑两个方面,一个是SELInux,另一个是防火墙
<strong>SELinux问题解决</strong>
①查看登录日志

sudo tail -n 20 /var/log/secure

如果发现日志中有

fatal: Cannot bind any address.
error: Bind to port 6983 on 0.0.0.0 failed: Permission denied.
error: Bind to port 6983 on :: failed: Permission denied.

表示数据端口绑定失败,这是因为Centos7中开启了SELinux
②关闭SELinux
修改SELinux配置文件

sudo vi /etc/selinux/config

找到SELINUX=enforcing,修改为

# SELINUX=enforcing SELINUX=disabled

重启服务器

sudo reboot

<strong>防火墙问题排查</strong>
如果再次连接发现还是不行
①查看日志

sudo tail -n 20 /var/log/secure

发现没有失败的日志输出
②查看防火墙是否开启

systemctl status firewalld

如果开启了,则原因就是刚刚设置的ssh端口6983并没有添加到防火墙中
③添加端口到防火墙

sudo firewall-cmd --zone=public --permanent --add-port=6983/tcp

④重启防火墙

sudo systemctl restart firewalld

查看端口是否添加成功,执行

sudo firewall-cmd --list-all

如果出现以下输出,则证明添加成功

public (default, active)
interfaces: enp0s3
sources:
services: dhcpv6-client ssh
ports: 6983/tcp
masquerade: no
forward-ports:
icmp-blocks:
rich rules:



Guess you like

Origin www.cnblogs.com/littlehb/p/10941706.html
Recommended