• Linux服务 sshd


    ssh:Secure Shell Protocol
        ssh通过数据包加密技术将待传输的数据加密后再通过网络传输到指定位置,相对于Telnet的明文传输,ssh传输的是密文,会比较安全,就算数据被截获也不容易被破解;
        ssh协议提供的功能:
            ssh提供类似Telnet的远程连接功能
            ssh还提供了一个类似ftp服务的sftp-server服务,可用于传输文件;
        加密机制:
            非对称加密系统,非对称指的是使用两个不一样的秘钥来加密和解密,我们称之为公钥(公开的,用于加密)和私钥(私有的,用于解密);
        客户端与服务器端的连接过程:
            1.服务器端启动sshd服务,会检测/etc/ssh/ssh_host*文件,如果是系统刚安装完成,尚且没有这些文件,则sshd会主动去计算这些文件,同时生成私钥文件并且监听在22号端口等待客户端的连接;
            2.客户端通过ssh客户端程序主动请求连接服务器,服务器接收请求,把公钥传送给客户端;
            3.客户端记录/比对服务器的公钥数据,并且计算生成自己的公私钥文件,若客户端是第一次连接此服务器,则会将服务器的公钥记录到正在使用的用户的家目录~/.ssh/known_hosts(会询问用户是否接受此公钥)。如果以前记录过此服务器的公钥,则客户端会比对当前的收到的额公钥与之前记录的是否有差异;
            4.客户端会发送自己的公钥给服务器端,此时客户端具有服务器的公钥和自己的私钥,服务器端具有客户端的公钥,和自己的私钥;
            5.服务器和客户端分别使用对方的公钥加密数据传输,双方收到后使用自己的私钥解密数据;
        
        命令使用:
            ssh [options…] user_name@ip_address [command]
                例子:ssh root@192.168.10.1
                -f:不登录目标主机,而是直接传送命令在目标主机上执行;
                -p #:端口号,连接时使用的不是22这个标准端口可以使用此参数另加指定;
        配置文件:/etc/ssh/sshd_config
            #Port 22                                                          默认端口号
            #AddressFamily any                                     
            #ListenAddress 0.0.0.0                                  监听的地址
            #ListenAddress ::
            
            # Disable legacy (protocol version 1) support in the server for new
            # installations. In future the default will change to require explicit
            # activation of protocol 1
            Protocol 2                                                        ssh协议版本
            
            # HostKey for protocol version 1
            #HostKey /etc/ssh/ssh_host_key
            # HostKeys for protocol version 2
            #HostKey /etc/ssh/ssh_host_rsa_key           ssh v2版本使用的私钥
            #HostKey /etc/ssh/ssh_host_dsa_key
            
            # Lifetime and size of ephemeral version 1 server key
            #KeyRegenerationInterval 1h
            #ServerKeyBits 1024
            
            # Logging
            # obsoletes QuietMode and FascistLogging
            #SyslogFacility AUTH                                       
            SyslogFacility AUTHPRIV                                产生的日志记录在/var/log/secure
            #LogLevel INFO                                                 所产生的日志的等级
            
            # Authentication:
            
            #LoginGraceTime 2m
            #PermitRootLogin yes                                        是否允许以root身份登录,生产环境中建议为no
            #StrictModes yes                                                  是否让sshd去检测用户的主目录,和相关文件的权限
            #MaxAuthTries 6
            #MaxSessions 10
            
            #RSAAuthentication yes
            #PubkeyAuthentication yes                                 使用允许用户自行使用成对的秘钥系统进行登录
            #AuthorizedKeysFile    .ssh/authorized_keys     指定公钥的存放位置
            #AuthorizedKeysCommand none
            #AuthorizedKeysCommandRunAs nobody
            
            # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
            #RhostsRSAAuthentication no
            # similar for protocol version 2
            #HostbasedAuthentication no
            # Change to yes if you don't trust ~/.ssh/known_hosts for
            # RhostsRSAAuthentication and HostbasedAuthentication
            #IgnoreUserKnownHosts no                               是否忽略用户主目录中~/.ssh/known_hosts这个文件中记录的内容
            # Don't read the user's ~/.rhosts and ~/.shosts files
            #IgnoreRhosts yes
            
            # To disable tunneled clear text passwords, change to no here!
            #PasswordAuthentication yes
            #PermitEmptyPasswords no                                    是否允许以空密码登录
            PasswordAuthentication yes                                    是否启用密码验证
            
            # Change to no to disable s/key passwords
            #ChallengeResponseAuthentication yes
            ChallengeResponseAuthentication no
            
            # Kerberos options
            #KerberosAuthentication no
            #KerberosOrLocalPasswd yes
            #KerberosTicketCleanup yes
            #KerberosGetAFSToken no
            #KerberosUseKuserok yes
            
            # Set this to 'yes' to enable PAM authentication, account processing,
            # and session processing. If this is enabled, PAM authentication will
            # be allowed through the ChallengeResponseAuthentication and
            # PasswordAuthentication.  Depending on your PAM configuration,
            # PAM authentication via ChallengeResponseAuthentication may bypass
            # the setting of "PermitRootLogin without-password".
            # If you just want the PAM account and session checks to run without
            # PAM authentication, then enable this but set PasswordAuthentication
            # and ChallengeResponseAuthentication to 'no'.
            #UsePAM no
            UsePAM yes                                                    是否使用PAM认证
            
            # Accept locale-related environment variables
            AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
            AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
            AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
            AcceptEnv XMODIFIERS
            
            #AllowAgentForwarding yes
            #AllowTcpForwarding yes
            #GatewayPorts no
            #X11Forwarding no
            X11Forwarding yes                                         是否允许窗口的数据通过ssh来传送
            #X11DisplayOffset 10
            #X11UseLocalhost yes
            #PrintMotd yes                                                登陆以后是否显示上次登录的信息,生产环境中建议为no
            #PrintLastLog yes                                             同上
            #TCPKeepAlive yes
            #UseLogin no
            #UsePrivilegeSeparation yes
            #PermitUserEnvironment no
            #Compression delayed
            #ClientAliveInterval 0
            #ClientAliveCountMax 3
            #ShowPatchLevel no
            #UseDNS yes
            #PidFile /var/run/sshd.pid
            #MaxStartups 10:30:100
            #PermitTunnel no
            #ChrootDirectory none
            
            #DenyUsers                                                          设置被限制的用户
            #DenyGroups                                                        设置被限制的组
            # no default banner path
            #Banner none
            
            # override default of no subsystems
            Subsystem    sftp    /usr/libexec/openssh/sftp-server
            
            # Example of overriding settings on a per-user basis
            #Match User anoncvs
            #    X11Forwarding no
            #    AllowTcpForwarding no
            #    ForceCommand cvs server
            
        实作:
            首先启动sshd服务
                service sshd start
            查看sshd的监听端口,确定已经监听在22号端口
                netstat -ltnp | grep ssh
            1.不用密码直接登录主机
                客户端:
                    ~]#ssh-keygen                                         生成秘钥对  默认会将公钥和私钥都存放在~/.ssh/目录中
                    ~]#scp ~/.ssh/id_rsa.pub guowei@192.168.10.30:~     将客户端用户的公钥发送给服务器        
                服务器端:
                    ~]#cat id_rsa.pub >> .ssh/authorized_keys      
                        将客户端的公钥追加到.ssh/authorized_keys 文件中,默认不存在,需要创建且权限为644;
                Note:在客户端就可以使用此账号免密登录服务器端名为guowei的账号了;
            2.非标准端口启动SSH
                服务器端:
                    ~]#vim /etc/ssh/sshd_config
                        Port 22
                        Port 23                                       非标准端口
                    ~]#/etc/init.d/sshd restart             重启sshd服务
                    这时一般会会出现SElinux的错误,需要我们自行定义selinux规则;
                    ~]#cat /var/log/audit/audit.log | grep AVC | grep ssh | audit2allow -m sshlocal > sshlocal.te
                    ~]#grep sshd_t /var/log/audit/audit.log | audit2allow -M sshlocal
                    ~]#semodule -I sshlocal.pp             加载模块到selinux中
                    ~]#/etc/init.d/sshd restart
                    ~]#netstat -tlunp | grep ssh
                    
                
                客户端:
                    ~]#ssh -p 23 root@192.168.10.50
                    ~]#netstat -tnp | grep 23
                    

    注:根据鸟哥的Linux私房菜做的学习笔记,如有错误,欢迎指正;侵删

  • 相关阅读:
    docker PXC MYSQL集群节点启动失败/节点顺序消失/只剩一个节点存在问题的解决
    springgateway
    rabbitMQ重复消费(结合死循环重发那一篇看)
    rabbitMq可靠性投递之手动ACK
    3表查询,1:多:多,根据1查多再查多
    tp后台注册登录配置项
    volist/foreach下,点击循环中的一个进行操作
    生成随机订单号
    省市县的下拉列表
    银行下拉列表
  • 原文地址:https://www.cnblogs.com/guowei-Linux/p/11072886.html
Copyright © 2020-2023  润新知