ssh service introduction and configuration

A, ssh introduction

1. What is ssh

ssh is an acronym for Secure Shell, is a secure remote management protocol at the application layer of the establishment. ssh is the more reliable transport protocol designed to provide security for remote login session, and other network services. Ssh protocol can effectively prevent the use of remote management in the process of information disclosure issue. ssh available for most UNIX and UNIX-like operating system, a character can be managed remotely login interface, it uses the default port 22, in the form of ciphertext data transmission network, with respect to the Telnet protocol transmitted in clear text, a more high security.

2, ssh login verification

ssh is provided based on the account password (password) of the two keys and the authentication mode, both the ciphertext data is transmitted through.

Password verification:

Log Format
ssh 用户名@IP地址
Key verification:
  • First you need to create a pair of keys on the Client, and the public on the need Server need to access.
  • When the Client needs to connect Server, Client-side software will send a login request to Server-side request using a key pair of public key security verification
  • Server After receiving the request, queries the public key file in the user's home directory, sent to take over the public under the Client's public key and compare their own home directory
  • If two public agreement, Server will use public key encryption "challenge (question)," and send it to the Client Software. After the Client receives an encrypted content, using the local private key to decrypt, and then send the results to decrypt Server end, the Server-side validation is successful, login is allowed.

Two, ssh service

1, environment preparation

Ready two Linux operating system of the host, and change it to a different host name for easy key behind the login.
centos6 change the hostname

[root@centos ~]# vim /etc/sysconfig/network
HOSTNAME=centos-11

Temporarily turn off protection:

 iptables -F #清空防火墙规则 
 setenforce 0 #临时关闭SELinux 

Permanent closure of protection:

 chkconfig iptables off         #设置防火墙开机不自启动 
 sed -i ‘7s/enforcing/disabled/’ /etc/selinux/config  #永久关闭SELinux 
 #注意:以上两条命令执行后,需要重启服务器才能生效
2, the key to the login configuration
1, between linux host key pair Login

1、客户端生成密钥对文件 ssh-keygen -t rsa -b 2048
-t 指定加密类型(rsa/dsa等)
-b 指定密钥对加密长度
询问1:执行过程中会询问保存位置,一般默认保存在当前用户家目录下的.ssh/目录下
询问2:是否对密钥文件进行加密
加密:若加密,则在调用密钥文件时需要先验证密钥的密码,密码正确才能使用密钥文件
不加密:若不加密,则密钥文件可以直接被调用,整个登录验证过程无需输入任何密码,即为免密登录

[root@centos-11 ~]# ssh-keygen -t rsa -b 2048

2、将公钥文件上传至服务器端 ssh-copy-id 用户名@服务器IP地址

[root@centos-11 ~]# ssh-copy-id [email protected]
#该用户名和要用来登录服务器的用户名一致

3、客户端尝试登录服务器 ssh 用户名@服务器IP地址

[root@centos-11 ~]# ssh [email protected]
Last login: Fri Dec 20 20:41:00 2019 from 10.10.10.1
#密钥对验证优先级大于账户密码验证 
2、windows 密钥对登录 linux主机

由于Windows不能直接生成密钥对,所以需要借助工具。我使用的是xshell
1、使用Xshell自带的密钥对生成向导生成密钥对

一直下一步,其中有一步是,设置密码为本地私钥。看情况进行选择。最后一步复制公钥无需保存。

2、将公钥导入Linux主机的指定用户下的指定公钥配置文件内 后面用哪个用户登录就放在谁的用户家目录下,找到 .ssh 目录,然后在里面创建 authorized_keys 文件,并且将公钥写入。

[root@centos-12 ~]# vim .ssh/authorized_keys
#粘贴你刚才复制的公钥信息,另起一行

3、使用windows尝试登录指定用户

3、禁止root用户远程登录

root 在系统中是一个可以为所欲为的角色,我们可以在平时的操作中用普通用户操作,在有需要修改一些系统设置的时候再从普通用户切换到 root 用户,这样可以最大限度的避免因为误操作而对系统造成破坏,同时也可以避免黑客在暴力破解后直接使用 root 用户登录系统,一般在远程登录管理上我们会禁止直接使用 root 用户登录。

配置文件:/etc/ssh/sshd_config 
选项:  
PermitRootLogin no 
4、修改默认端口
修改默认端口:ssh 作为一个用来远程管理服务器的工具,需要特别的安全,默认情况下使用TCP的22端口,若不进行修改,很容易被利用遭到攻击,所以我们一般都会修改端口,尽量修改一个高位端口(范围1-65535)
配置文件:/etc/ssh/sshd_config 
 Port 55235
linux 主机登录
    ssh -p 55235 [email protected]
xshell 登录
    ssh [email protected]:55235
5、ssh服务相关服务
scp:安全的远程文件复制命令

scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,类似于命令有cp,scp传输是加密的,所以可能会稍微影响一点速度。另外,scp还非常不占资源,不会提高多少系统负荷。

格式:
scp 本地文件 用户名@服务器IP:目录 
scp test.txt [email protected]:/tmp    
 -P 端口   #若端口不是默认22,则需要使用此格式指定端口 
sftp:安全的文件传输协议

sftp是Secure FileTransferProtocol的缩写,安全文件传送协议。sftp与ftp有着几乎一样的语法和功能。由于这种传输方式使用了加密/解密技术,所以sftp比ftp更安全一些,但传输效率比普通的FTP要低得多.

格式:
sftp 用户名@服务器IP  
-oPort=端口  #若端口不是默认22,则需要使用此格式指定端口 
交互命令: 
help:查看在交互模式下支持哪些命令 
pwd/lpwd:pwd是查看服务器所在路径;lpwd是查看客户端所在路径 
ls/lls:ls是查看服务器当前目录下的文件列表;lls是查看客户机当前所在路径的所有文件列表 
put:将客户机中的指定文件上传到服务器端 
get:将服务器端的指定文件下载到客户机的当前所在目录 
rm:删除掉服务器端的指定文件 
quit:退出sftp的交互模式,断开和服务器之间的连接 
6 TCP Wrappers(简单防火墙)
1、介绍

TCP_Wrappers是一个工作在第四层(传输层)的的安全工具,对有状态连接(TCP)的特定服务进行安全检测并实现访问控制,界定方式是凡是调用libwrap.so库文件的的程序就可以受TCP_Wrappers的安全控制。它的主要功能就是控制谁可以访问,常见的程序有rpcbind、vsftpd、sshd,telnet。
简单来说 TCP Wrappers 只对走tcp协议的一些服务起到控制作用。
判断方式:

1. 查看对应服务命令所在位置 which sshd 
2. 查看指定命令执行时是否调用libwrap.so文件 ldd /usr/sbin/sshd | grep libwrap.so
[root@centos-12 ~]# which sshd
/usr/sbin/sshd
[root@centos-12 ~]# ldd /usr/sbin/sshd | grep libwrap.so
    libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fd38c0d1000)
2、工作原理

以ssh为例,每当有ssh的连接请求时,先读取系统管理员所设置的访问控制文件,符合要求,则会把这次连接原封不动的转给ssh进程,由ssh完成后续工作;如果这次连接发起的ip不符合访问控制文件中的设置,则会中断连接请求,拒绝提供ssh服务。

3、配置

TCP_Wrappers的使用主要是依靠两个配置文件/etc/hosts.allow, /etc/hosts.deny,以此实现访问控制,默认情况下,/etc/hosts.allow,/etc/hosts.deny什么都没有添加,此时没有限制 。

service_list@host: client_list 
配置文件编写规则: 
    service_list: 是程序(服务)的列表,可以是多个,多个时,使用,隔开
    @host:设置允许或禁止他人从自己的哪个网口进入。这一项不写,就代表全部 
    client_list:是访问者的地址,如果需要控制的用户较多,可以使用空格或,隔开
    内置ACL:ALL(所有主机)、LOCAL(本地主机)

以ssh服务代码示例:

拒绝单个 IP 使用 ssh 远程连接: 
配置文件: 
    hosts.allow:空着 
    hosts.deny:sshd:10.10.10.1
拒绝某一网段使用 ssh 远程连接: 
    hosts.allow:空着 
    hosts.deny:sshd:10.10.10. 
仅允许某一 IP 使用 ssh 远程连接: 
    hosts.allow:sshd:10.10.10.11
    hosts.deny:sshd:ALL

Guess you like

Origin www.cnblogs.com/hjnzs/p/12075596.html