Telnet、openssh和dropbear



telnet:

    C/S架构

    23/TCP

    

    由于传输是明文的,被废弃


服务进程:守护进程的一种

    独立守护进程

    超级守护进程:xinetd,/etc/xinetd.d/

        瞬时守护进程:

                telent

    #chkconfig  --list    底部有显示那些是瞬时守护程序,如:

    xinetd based services:

    chargen-dgram: off

    chargen-stream:off

    daytime-dgram: off

    daytime-stream:off

    discard-dgram: off

    discard-stream:off

    echo-dgram:    off

    echo-stream:   off

    rsync:         on

    tcpmux-server: off

    telnet:        off

    time-dgram:    off

    time-stream:   off


    #vim /etc/xinetd.d/telnet      disable = yes  改为disable = no 开机启动,等同:

    #chkconfig telnet on

    #service xinetd start  超级守护进程启动后,瞬时进程也就启动了

    #ss -tnlp |grep :23


    注意:Telnet不允许root直接登录



SSH secure shell协议

openssh是ssh协议的实现    (软件)

C/S架构

    22/TCP

    

    

openssh

    client:

        scp、ssh、sftp、slogin

        配置文件:/etc/ssh/ssh_config

    

    server:

        配置文件:/etc/ssh/sshd_config

                    参数    值

        服务脚本:/etc/rc.d/init.d/sshd    在/etc/rc.d/init.d/可为服务创建脚本,即可使用service来控制

        

主机密钥:

    V1:host_key,已经不用第一版

    V2:RSA或DSA,成对儿的密钥对, /etc/ssh/ 下可以看到


获取配置文件的帮助:

    man sshd_config


服务器端配置文件/etc/ssh/sshd_config:

在/etc/ssh/sshd_config中,有一项关于日志的:

SyslogFacility AUTHPRIV    AUTHPRIV是调用了/etc/rsyslog.conf  配置中authpriv.*  /var/log/secure

所以ssh的日志是记录在/var/log/secure中


LogLevel  日志有7个级别

LoginGraceTime  2m    登录宽限时长,2分钟;  登录窗口不输入用户名和密码,放着不动2分钟后超时

PermitRootLogin  yes    允许root直接登录

StrictModes  yes    严格模式,可忽略

MaxAuthTries 6    最大尝试次数

MaxSessions 10    最大会话连接数量


跟基于密钥的认证,相关:(注释掉就是使用默认值)

#RSAAuthentication yes

#PubkeyAuthentication yes

#AuthorizedKeysFile     .ssh/authorized_keys

#AuthorizedKeysCommand none

#AuthorizedKeysCommandRunAs nobody

 

跟基于口令认证的相关:

PasswordAuthentication yes

#PermitEmptyPasswords no    禁止使用空密码    


UsePAM yes      可使用PAM机制来做用户认证

    PAM  可插拔认证模块  (Pluggable Authentication Modules )

UseDNS    yes改为no  禁止DNS反解客户端IP和主机名,默认被注释

X11Forwarding yes    运行图形化程序,在客户端显示

Subsystem       sftp    /usr/libexec/openssh/sftp-server

子系统            以sftp用户身份运行        运行的程序:/usr/libexec/openssh/sftp-server





客户端配置文件:/etc/ssh/ssh_config   较少改动,一般默认即可

StrictHostKeyChecking ask    ask可改成no,作用:在ssh主机时,提示是否接受公钥,改为no后不提示




ssh命令用法:

ssh  [option]  username@server_ip   [command]   

        COMMAND  如果有空格要引起来,如

                ssh  [email protected]  'ls  /root'

ssh  username@servier_ip  =  ssh  -l username  server_ip


如果省略username,则用当前使用的用户来远程对方相同用户名的用户登录


    -p    PORT    指明要连接的端口

    -X    enable  x11 forwarding,即转发X界面的请求

    -x    disable x11 forwarding,禁用

    -Y    启用信任的主机的forwarding



    

基于密钥的认证:

1、客户端,即本地端

    ssh-kengen  生成一对密钥

#ssh-kengen  [options]

    -t    {rsa|dsa}

    -f    /PATH/TO/OUTPUT_KEYFILE

    -b #    密钥长度

    -p  ‘’指明加密密钥的密码,留空则表示为不设置密码


2、服务器端,即要远程的主机

把公钥部分复制到登录的远程主机的特定用户(可不同于本地用户)的家目录下,保存至.ssh/下authorized_keys文件中(追加保存)

       ssh-copy-id [-i [identity_file]] [user@]machine

       



windows上常用的SSH协议客户端

    xshell(xmanager)

    securecrt

    putty

    等


    

scp:远程复制工具:

scp [OPTIONS]  SRC...   DST


    push:

        scp  [options]  /path/to/somefile   user@server:/path/to/dest


    pull

         scp  [options]    user@server:/path/to/dest   /path/to/somefile


常用选项:

    -r    recursive  递归复制,用于复制目录

    -p    保存源文件的元属性信息,如果属主、属组及权限

    -q    静默模式

    -P  port    指明端口



sftp:连接openssh服务器开启的subsystem(sftp-server)

    sftp [USER]@SERVER

    sftp>help    可获取常用帮助命令





服务器端程序:

    sshd最佳实践:   /etc/ssh/sshd_config

1、不要使用默认端口      Port  22

2、不要使用version1      protocol 2(默认即是如此)

3、限制可登陆用户:  详细情况可man sshd_config

    AllowUsers    允许登陆的用户白名单,多个用户使用空格分割

    AllowGroups    允许登陆的组的白名单

    DenyUsers

    DenyGroups

4、设置空闲会话超时时长

    

5、利用防火墙设置ssh访问策略

    限定ssh仅允许×××分配的有限的IP访问


6、仅监听特定的IP地址(多网卡)


7、使用强密码策略

    #tr -dc A-Za-z0-9 < /dev/urandom |head -c 30|xargs


8、使用基于密钥的认证


9、禁止使用空密码


10、禁止管理员直接登录  #PermitRootLogin yes   修改为no


11、限制ssh访问的访问频度

12、做好日志、经常做日志分析

    /var/log/secure





另一款SSH协议的开源实现


服务程序:dropbear ==>sshd

客户端程序:dbclient ==> ssh


编译安装:

#  ./configure --prefix=/usr/local/dropbear  --sysconfigdir=/etc/dropbear  --disable-pam

#make

#make PROGRAMS="dropbear="dropbear dbclient dropbearkey dropbearconvert scp"   

#make PROGRAMS="dropbear="dropbear dbclient dropbearkey dropbearconvert scp"    install



vim /etc/profile.d/dropbear.sh

export PATH=/usr/local/dropbear/bin:/usr/local/dropbear/sbin:$PATH


启动dropbear

#dropbear

关闭dropbear

#killall dropbear


编译安装的没有service   可以写个脚本 start 就运行dropbear ,stop-->killall dropbear

脚本放置位置/etc/rc.d/init.d/  下




================================实验部分=================================

1、基于密钥的ssh登录

两台机器:172.20.1.34、172.20.1.33

在172.20.1.34:

    [root@localhost .ssh]# ssh-keygen 

    Generating public/private rsa key pair.

    Enter file in which to save the key (/root/.ssh/id_rsa): 

    Enter passphrase (empty for no passphrase): 

    Enter same passphrase again: 

    Your identification has been saved in /root/.ssh/id_rsa.

    Your public key has been saved in /root/.ssh/id_rsa.pub.

    The key fingerprint is:

    f5:39:c4:dd:2b:1f:c6:7e:66:61:93:8d:fb:e0:db:1a [email protected]

    The key's randomart p_w_picpath is:

    +--[ RSA 2048]----+

    |                 |

    |           . . . |

    |          . o . .|

    |         . o ...+|

    |        S   +..X.|

    |             .*.+|

    |              Eo+|

    |             . B.|

    |              +oo|

    +-----------------+

    [root@localhost .ssh]# ll

    total 12

    -rw-------  1 root root 1675 Dec 28 17:44 id_rsa

    -rw-r--r--  1 root root  408 Dec 28 17:44 id_rsa.pub

    多出两个文件,私钥和公钥,私钥需妥善保存

    [root@localhost .ssh]# ssh-copy-id [email protected]

    The authenticity of host '172.20.1.33 (172.20.1.33)' can't be established.

    RSA key fingerprint is 59:db:7d:d0:e3:8d:3a:8b:93:46:1a:3c:e1:d6:e7:53.

    Are you sure you want to continue connecting (yes/no)? yes

    Warning: Permanently added '172.20.1.33' (RSA) to the list of known hosts.

    [email protected]'s password: 

    Now try logging into the machine, with "ssh '[email protected]'", and check in:


      .ssh/authorized_keys


    to make sure we haven't added extra keys that you weren't expecting.

在172.20.1.33中可以看出家目录多处的文件:

    [root@localhost .ssh]# ll

    total 8

    -rw------- 1 root root 408 Dec 28 17:47 authorized_keys  多出的文件,属主和权限都已自动设定好

    -rw-r--r-- 1 root root 393 Nov 26 21:48 known_hosts

登录:

    [root@localhost .ssh]# ssh [email protected]

    Last login: Mon Dec 28 17:47:50 2015 from 172.20.1.34  不需要输入密码即可登录

    [root@localhost ~]# 



编译安装dropbear:

http://matt.ucc.asn.au/dropbear/dropbear-2015.71.tar.bz2 截止目前最新的版本

#tar -xf dropbear-2015.71.tar.bz2

drwx------  5 wkp  games    12288 Dec 25 11:16 dropbear-2015.71

-rw-r--r--  1 root root   1616280 Dec 25 09:31 dropbear-2015.71.tar.bz2

#cd  dropbear-2015.71

#./configure --prefix=/usr/local/dropbear --sysconfigdir=/etc/dropbear --disable-pam

#make

#make PROGRAMS="dropbear="dropbear dbclient dropbearkey dropbearconvert scp"    install



注意:

可使用./configure --help来查看一些帮助

更详细的安装介绍可查看INSTALL文档

编译安装前需要注意安装上开发库

    #yum groupinstall "Development Tools" "Desktop Platfrom Development" "Server platfrom Development"





[root@localhost dropbear-2015.71]# dropbear --help

Invalid option --

Dropbear server v2015.71 https://matt.ucc.asn.au/dropbear/dropbear.html

Usage: dropbear [options]

-b bannerfileDisplay the contents of bannerfile before user login

(default: none)

-r keyfile  Specify hostkeys (repeatable)

defaults: 

dss /etc/dropbear/dropbear_dss_host_key

rsa /etc/dropbear/dropbear_rsa_host_key

ecdsa /etc/dropbear/dropbear_ecdsa_host_key

-RCreate hostkeys as required

-FDon't fork into background

-ELog to stderr rather than syslog

-mDon't display the motd on login

-wDisallow root logins

-sDisable password logins

-gDisable password logins for root

-BAllow blank password logins

-jDisable local port forwarding

-kDisable remote port forwarding

-aAllow connections to forwarded ports from any host

-p [address:]port

Listen on specified tcp port (and optionally address),

up to 10 can be specified

(default port is 22 if none specified)

-P PidFileCreate pid file PidFile

(default /var/run/dropbear.pid)

-iStart for inetd

-W (default 24576, larger may be faster, max 1MB)

-K  (0 is never, default 0, in seconds)

-I  (0 is never, default 0, in seconds)

-V    Version


有三个文件默认是没有的,要手动创建:

[root@localhost dropbear-2015.71]# dropbearkey -h

Usage: dropbearkey -t -f [-s bits]

-t typeType of key to generate. One of:

rsa

dss

ecdsa

-f filename    Use filename for the secret key.

               ~/.ssh/id_dropbear is recommended for client keys.

-s bitsKey size in bits, should be a multiple of 8 (optional)

           DSS has a fixed size of 1024 bits

           ECDSA has sizes 256 384 521 

-yJust print the publickey and fingerprint for the

private key in .


[root@localhost dropbear]# dropbearkey -t rsa -f dropbear_rsa_host_key

Generating key, this may take a while...

Public key portion is:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCVdBbjAGswFSPohPIqamrXaFxW0NtMSorZkBn4bldT9nQM40EKvSKXvS4pBvpAjcNlPWfEQ3WL3iAm/mOTD/njc78M6S823TQaDqC9uo5Nx6oRbQV+x86sNlaaT3nLmwxLfMBXyNuOVKDXqI5HChnnh/JlSWWgJgjtZB/bt2v22W6j2XIBlhG1cAFkHryxQDJ9dwWJAjNbzgiPC7nF0hxqwDv/QGXrCH6JpD3wqI7k44tFwsCp23yLEhpDh5+HEGjmbiDTe3ddanwz4bIENJTXncTy2eCSI/F+GaYf8dSV6swgYG60pj5RGngNlqyuJfSr7ynky5qBaaBF3tFztn0B [email protected]

Fingerprint: md5 97:78:12:ae:31:dd:de:c4:92:80:f5:e9:f8:fc:e7:26

[root@localhost dropbear]# dropbearkey -t dss -f dropbear_dss_host_key

Generating key, this may take a while...

Public key portion is:

ssh-dss AAAAB3NzaC1kc3MAAACBALHza3FntJ+YKLTLxiuv0PJ5EYa4+IYPP/VWtFqewws79DGXQZYUPmhTacyhUkr3IQCokHn5AAwX4UuKMIWWV92vv6BWxmswQV2yivCLkzV33GlNUSOV463JPAQLoKQyaIsof5TP2XbyGgyXTk/BkK/SGxRJxHa8jzg8NoZyFpErAAAAFQDqqI/ewPOJ2kVHBfBRXmzvh6XE0QAAAIBoK9IASaLZFNtQ7CZ1XhgvtvJJcc+Fomnq3Yx+1UritQDG09oirgpZ5FkN2MLU0rVErSzmyfPuPj4RM1gxwyf21KTieioKmIXJ6SribYgrq6clEihmlFbuiaOB1yF03/j7CDfIORVT6BxnacaWSFnoV/OMCpqRNVL6dCFbfxX90QAAAIA6dpGLycEHQBw6yTAbMBYivd2dJfMX/IM1Iu00U5ZM39/c1LVfusyRjxrh0bjMnArPwCt00c5/3nsGm4xg/B36ncDIZKpQEsVrpz61x0vr2TfnM0r9ob472ASe4r2TIpq8/cT2NP1ruIx5O03qewnn7oFYmq3OWR060Or2+VfN0Q== [email protected]

Fingerprint: md5 d6:77:66:4b:21:9a:c5:28:1f:09:42:e5:53:f5:5d:3d

[root@localhost dropbear]# dropbearkey -t ecdsa -f dropbear_ecdsa_host_key

Generating key, this may take a while...

Public key portion is:

ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAHI03le5vBKvd63S2un86ryicIEIsCr1H7vcs4UljzqKYRfLpglHIHLor7IAyXMr3ay7DPbdv7agp0LC6TwRznRvgCKxYOj7oHH388YbRO0Opwoe4ksh6gbnj9NUkRIYDMA9SXvkhaRymPnyQ9GveApumIGpUp/ls71xY8M5dkg/CXcPg== [email protected]

Fingerprint: md5 bb:c6:de:79:71:35:05:2b:ed:c8:5b:61:6c:c5:78:f4

[root@localhost dropbear]# 


生成的文件:

[root@localhost dropbear]# pwd

/etc/dropbear

[root@localhost dropbear]# ll

total 12

-rw------- 1 root root 458 Dec 28 18:48 dropbear_dss_host_key

-rw------- 1 root root 242 Dec 28 18:48 dropbear_ecdsa_host_key

-rw------- 1 root root 805 Dec 28 18:47 dropbear_rsa_host_key

[root@localhost dropbear]# 



将dropbear的安装目录添加环境变量(开机自动执行)

#vim /etc/profile.d/dropbear.sh

    内容:export PATH=/usr/local/dropbear/bin:/usr/local/dropbear/sbin:$PATH

    属性:-rw-r--r--  1 root root   67 Dec 25 16:17 dropbear.sh




启动dropbear

#dropbear

关闭dropbear

#killall dropbear




编译安装的没有service支持   可以写个脚本 start 就运行dropbear ,stop-->killall dropbear

脚本放置位置/etc/rc.d/init.d/  下

[root@localhost init.d]# ll dropbear 

-rwxr-xr-x 1 root root 812 Dec 28 16:51 dropbear


内容:

# chkconfig:   2345 90 10

# description:  dropbear test

 


#!/bin/bash

. /etc/rc.d/init.d/functions

export PATH=/usr/local/dropbear/bin:/usr/local/dropbear/sbin:$PATH

start() {

   dropbear -p 22022 & 

 if ss -tanl |grep ':22022' &> /dev/null ;then

    echo "dropbear start sucessfuly"

 else

   echo "dropbear start failed"

 fi

}


stop() {

  killproc 'dropbear' &> /dev/null


 if ss -tanl|grep ':22022' &>/dev/null;then

   echo "dropbear stop failed"

 else

   echo "dropbear stop sucessfully"

  fi

}


restart() {

  stop 

  start

}


status() {

 if ss -tanl|grep ':22022' &>/dev/null;then

   echo "dropbear is running"

 else

   echo "dropbear is stopped"

 fi

}



case $1 in

start)

  start ;;

stop)

  stop ;;

restart)

  restart ;;

status)

  status ;;

*)

 echo "Usage $0 start|stop|status|restart" && exit 2 ;;

esac




设置开机启动:

[root@localhost init.d]# chkconfig --add dropbear

[root@localhost init.d]# chkconfig dropbear on





安装过程中遇到的问题:

yum groupinstall "Development Tools" "Desktop Platfrom Development" "Server Platfrom Development"安装开发组件

configure:4536: error: *** zlib missing - install first or check config.log ***

yum install -y   zlib-devel  解决



[root@localhost init.d]# service dropbear stop

/sbin/service: line 66: 94045 已杀死               env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" ${OPTIONS}


解决:(参考yum安装后的脚本)

载入functions,然后使用killproc来结束进程



[root@localhost init.d]# chkconfig --add dropbear

service dropbear does not support chkconfig


这两行放入文件头部即可:

# chkconfig:   2345 90 10

# description:  dropbear test