Linux之SSH远程登陆服务

  • SSH:

ssh是基于http的c/s架构服务。

安装包:
        yum install openssh-client    //客户端软件包
        yum install openssh-server    //服务器端软件包
        
安装查询:
        yum list installed | grep ssh
常用命令:
    ssh-keygen:密钥生成器
    ssh-copy-id:将公钥传输至远程服务器
    scp:跨主机安全复制工具
        
启动服务:
        service sshd status   //centos 6
        systemctl start sshd.service   //centos 7
    
配置文件详解
        /etc/ssh/sshd_config               //配置文件
       
#   ForwardAgent no
#   ForwardX11 no
#   RhostsRSAAuthentication no
#   RSAAuthentication yes
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   GSSAPIKeyExchange no
#   GSSAPITrustDNS no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/identity
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22
#   Protocol 2
#   Cipher 3des
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,[email protected],hmac-ripemd160
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no  :设置为 no :阻止任何人以root身份进行远程登陆
#   VisualHostKey no
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h

 

  • 使用ssh远程登陆
1、ssh 用户名@ip地址
    例:ssh [email protected]
2、ssh -l USERNAME HOST
    例: ssh -l root 192.168.1.1
3、远程主机执行命令:ssh USERNAME@HOST 'COMMAND'
    例: shh [email protected] 'ifconfig'
  • SCP命令:
1、scp USERNAME@HOST:/path/to/somefile  /path/to/local 
    将远程主机somefile文件拷贝到本地local文件下。
2、scp /path/to/local  USERNAME@HOST:/path/to/somefile
    将本地local文件拷贝到远程主机somefile文件下
3、-r 递归的复制整个文件夹
  • 基于密钥(无密码)的身份验证
1、ssh-keygen                   //生成密钥家目录下.ssh文件下
    -f /path/to/key_file :指定生成密码文件存放路径
    -p '' :指定加密私钥密码
    例:ssh-keygen -f ~/.ssh/id_rsa.pub -p ' ' 

2、scp ~/.ssh/id_rsa.pub [email protected]:~/.ssh/ //将公钥拷贝至远程主机.ssh文件下,如不存在需手动创建.ssh文件。

3、ssh-copy-id -i ~/.ssh/id_rsa.pub 目标用户@IP地址             //将密钥复制到目标主机.ssh文件下 

            

  • rsync
  网络备份工具
        参数:    
            rsync -avl 用户@IP地址:源文件 目标文件
            -a:递归存档
            -v:详细信息
            -l:复制符号链接


         

  • 简易安全设置:
主要分三个方面进行:

      1、服务器软件本身的设置强化:/etc/ssh/sshd_config

      (1)禁止root账号使用sshd服务

      (2)禁止nossh组的用户使用SSHD服务

      (3)禁止testssh用户使用SSHD服务

          vim /etc/ss/ssd_config

          其中 PermiRootLogin  no      :禁止root登陆

          DenyGroup    nossh  

          DenyUsers    testssh

      2、TCP Wrapper的使用:/etc/host.allow , /etc/host/deny

        设置黑名单和白名单

        vim /etc/host.allow

        sshd: 127.0.0.1 192.168.1.0/24  192.168.100.0/255.255.255.0

        deny

        sshd:ALL

      3、iptables数据包过滤防火墙
  • dropbear:嵌入式系统专用的ssh服务端和客户端工具
1.Dropbear介绍
    dropbear:小型的嵌入式系统专用的SSH服务端和客户端工具
        服务器端:dropbear、dropbearkey
        客户端:dbclient
    dropbear默认使用nsswitch实现名称解析(账户名称)
    dropbear会在用户登录时检查其默认shell是否为安全shell,/etc/shells中的shell一般称为安全shell

2.Dropbear的安装编译
    下载链接:https://matt.ucc.asn.au/dropbear/
    下载完成并解压后执行如下命令进行编译安装
    dropbear默认编译方式:
    ./configure
    make && make install
    
3.主机秘钥配置
    默认情况下,dropbear的配置文件存放在/etc/dropbear目录下(如果没有,则需要手动创建)
    为dropbear生成配置文件:
        dropbearkey -t TYPE -f dropbear_TYPE_host_key -s SIZE
        
        -t TYPE: 秘钥配置文件的类型,一般有rsa,dss,ecdsa等
        -f dropbear_TYPE_host_key: 指定该TYPE加密类型的配置文件的存放路劲
        -s SIZE:指定加密的位数,默认情况下rsa为1024,最多4096 只要是8的倍数即可,ecdsa默认为256,长度限制为112-571
    一般情况下只需要为常用的加密类型生成其配置文件即可

4.使用SSH登录该主机
    ssh USERNAME@HOST

5.使用dbclient登录其他主机
    dbclient USERNAME@HOST

 

你可能感兴趣的:(Linux)