1. 前期准备

    rshServer192.168.1.198
    rshClient192.168.1.197
  2. 本地域名解析

    [root@rshServer ~]# cat >>/etc/hosts <<EOF

    > 192.168.1.198 rshServer

    > 192.168.1.197 rshClient

    > EOF

    [root@rshServer ~]# tail -2 /etc/hosts

    192.168.1.198 rshServer

    192.168.1.197 rshClient

  3. 所需软件

    [root@rshServer ~]# rpm -qa|grep rsh

    [root@rshServer ~]# yum install rsh*

    rsh.x86_64 0:0.17-64.el6

    rsh-server.x86_64 0:0.17-64.el6

    xinetd.x86_64 2:2.3.14-39.el6_4 

    [root@rshServer ~]# ls /etc/xinetd.d/    

    chargen-dgram   daytime-stream  echo-dgram   rlogin  tcpmux-server

    chargen-stream  discard-dgram   echo-stream  rsh     time-dgram

    daytime-dgram   discard-stream  rexec        rsync   time-stream

  4. 修改rlogin,rsh,rexec配置文件,红色已标出,默认为yes,改为no

    [root@rshServer ~]# cat /etc/xinetd.d/rsh 

    # default: on

    # description: The rshd server is the server for the rcmd(3) routine and, \

    #       consequently, for the rsh(1) program.  The server provides \

    #       remote execution facilities with authentication based on \

    #       privileged port numbers from trusted hosts.

    service shell

    {

            socket_type             = stream

            wait                    = no

            user                    = root

            log_on_success          += USERID

            log_on_failure          += USERID

            server                  = /usr/sbin/in.rshd

            disable                 = no

    }

    [root@rshServer ~]# cat /etc/xinetd.d/rlogin 

    # default: on

    # description: rlogind is the server for the rlogin(1) program.  The server \

    #       provides a remote login facility with authentication based on \

    #       privileged port numbers from trusted hosts.

    service login

    {

            socket_type             = stream

            wait                    = no

            user                    = root

            log_on_success          += USERID

            log_on_failure          += USERID

            server                  = /usr/sbin/in.rlogind

            disable                 = no

    }

    [root@rshServer ~]# cat /etc/xinetd.d/rexec  

    # default: off

    # description: Rexecd is the server for the rexec(3) routine.  The server \

    #       provides remote execution facilities with authentication based \

    #       on user names and passwords.

    service exec

    {

            socket_type             = stream

            wait                    = no

            user                    = root

            log_on_success          += USERID

            log_on_failure          += USERID

            server                  = /usr/sbin/in.rexecd

            disable                 = no

    }

  5. 权限设置

    [root@rshServer ~]# cat >>/etc/securetty <<EOF

    > rsh

    > rlogin

    > rexec

    > EOF

    [root@rshServer ~]# tail -3 /etc/securetty 

    rsh

    rlogin

    rexec

    [root@rshServer ~]# echo "rshClient root" >>.rhosts

  6. 启动并查看512 513 514端口是否已打开

    [root@rshServer ~]# /etc/init.d/xinetd start

    Starting xinetd:                                           [  OK  ]

    [root@rshServer ~]# netstat -lantup|grep 51

    tcp        0      0 :::512                      :::*                        LISTEN      1415/xinetd         

    tcp        0      0 :::513                      :::*                        LISTEN      1415/xinetd         

    tcp        0      0 :::514                      :::*                        LISTEN      1415/xinetd  

    [root@rshServer ~]# chkconfig --list|egrep "rsh|rlogin|rexec|xinetd"

    xinetd          0:off   1:off   2:off   3:on    4:on    5:on    6:off

    xinetd based services:

            rexec:          on

            rlogin:         on

            rsh:            on

  7. 以rshServer为对比,对rshClient进行类似设置即可,登录测试

    [root@rshClient ~]# rlogin rshServer

    Last login: Tue Dec 29 10:06:52 from rshClient

    [root@rshServer ~]# 


原理详解

wKiom1aB_9_BatuRAADdvxtuKlQ500.png

 1. 本地rlogind在本地/etc/passwd文件中寻找远程用户名,没有则拒绝访问。 

 2. /etc/passwd中存在远程用户名,rlogind在/etc/hosts.equiv寻找远程主机名,找到则允许访问。 

 3. /etc/hosts.equiv无远程主机名,rlogind在$HOME/.rhosts寻找远程主机名,找到且该项后无用户名则允许访问,找到且该项后有用户名,若远程用户名位于其中,则允许访问。注意这里的$HOME是与远程用户同名的本机用户的主目录。 

 4. 若通过了/etc/passwd的检查,但没有通过/etc/hosts.equiv或者$HOME/.rhosts的检查,远程用户给出口令可以登录本机,但无法使用rcp、rsh等。反之则可以使用rcp或者rsh。 

 5. /etc/hosts.equiv中的+意味着任意主机,此时与/etc/hosts无关。netterm下rlogin除root外的所有用户可以成功,solaris下简单的rlogin hostname同netterm,但是rlogin -l username hostname不成功。这个事实说明netterm下rlogin的时候,指定的参数实际上是远程主机的当前用户名。还说明hosts.equiv文件不支持rlogin -l username hostname,不支持root的rlogin。

 6. rlogin -l username hostname和rlogin hostname的检查有点细微的差别,首先在/etc/passwd的检查中是以-l的参数为准的,没有-l参数才使用远程主机的当前用户名,所谓当前用户名是考虑了su出来的用户。其次,$HOME也是先以-l参数为准。第三,$HOME/.rhosts文件中的用户名不是针对-l参数来的,而是针对远程主机的当前用户名来的,但是对于这个远程主机的当前用户名,并不要求出现在/etc/passwd文件中。这第6条尤其重要,许多不成功的$HOME/.rhost就是因为对第6条的不了解。

 7. $HOME/.rhosts文件的权限问题,chmod 0都没有影响,不过当时是处理root用户。对于一般用户,一定要保证.rhosts文件对于$HOME的属主是可读的。一般来说,这个文件最好chmod 400。对于root,如果不希望某个用户建立自己的.rhosts或者想替该用户建立.rhosts后不允许该用户自己修改,可以通过chown root .rhosts然后chmod 444 .rhosts实现。除了root,如果$HOME/.rhosts文件的属主和$HOME的属主不一致,检查将失败。

 8. $HOME/.rhosts文件中每行只跟一个用户名,若想多个指定,只好分多行指定用户名。

 9. sco unix下$HOME/.rhosts中若用+代表主机,与其他Unix系统不同,这里的+没有任何特殊的意义,所以检查将失败。但是用户名仍然可以用+代表。并且sco unix下若root的.rhosts文件go+w,则检查失败。