风影子
作者风影子·2011-02-11 23:40
数据库管理员·深圳

TELNET/SSH服务的配置

字数 19641阅读 7196评论 0赞 1

来源于:http://chenbin.blog.51cto.com/945033/324263

TELNET/SSH服务的配置。

TELNET简介:

telnet,最早是用于连接BBS的工具。它在网络中传输数据时使用的是明文方式,这带来了安全隐患,由于它是以明文方式传输,所以渐渐被ssh代替。ssh就是在telnet基础上增加了安全性的远程登录软件,ssh在网络中传输的数据是经过加密的。

TELNET基本配置:

安装软件:

[root@rhel ~]# rpm -ivh /misc/cd/Server/telnet-server-0.17-39.el5.i386.rpm

 

启动telnet

最简单的方法是使用ntsysv命令调出图形界面把telnet选择即可。

[root@rhel ~]# ntsysv

 

第二种方法:编辑配置文件:

 

[root@rhel ~]# vi /etc/xinetd.d/telnet

service telnet

{

        flags           = REUSE

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/sbin/in.telnetd

        log_on_failure  += USERID

        disable         = no        //只需修改这里即可。

}

 

[root@rhel ~]# service xinetd restart    //telnetxinetd的一个子服务。

测试:(关掉服务器的防火墙)

 

C:>telnet 192.168.10.10

Red Hat Enterprise Linux Server release 5.3 (Tikanga)

Kernel 2.6.18-128.el5 on an i686

login: root

Password:

Login incorrect

 

默认情况下root用户是不能登录的,出于安全考虑。那么在服务器上新建一个用户试试。

[root@rhel ~]# useradd rhel

[root@rhel ~]# passwd rhel

Changing password for user rhel.

New UNIX password:

BAD PASSWORD: it is based on a dictionary word

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

 

C:>telnet 192.168.10.10

Red Hat Enterprise Linux Server release 5.3 (Tikanga)

Kernel 2.6.18-128.el5 on an i686

login: rhel

Password:

[rhel@rhel ~]$

已经成功登录。这里要注意的是如果一个用户没有设置密码是不能登录的。当然,没有密码的用户在linux本地也是不能登录的。那怎么让root用户能登录呢?

 

[root@rhel ~]# mv /etc/securetty /etc/securetty.bak  //重命名

Red Hat Enterprise Linux Server release 5.3 (Tikanga)

Kernel 2.6.18-128.el5 on an i686

login: root

Password:

Last login: Sat Apr 10 19:39:08 on :0

[root@rhel ~]# whoami

root

 

其实根本没有必要允许telnet用户登录,因为使用普通用户登录后可以使用su来切换身份。

Red Hat Enterprise Linux Server release 5.3 (Tikanga)

Kernel 2.6.18-128.el5 on an i686

login: rhel

Password:

Last login: Mon Apr 12 11:26:16 from 192.168.10.254

[rhel@rhel ~]$ su root

Password:

[root@rhel rhel]# whoami

root

[root@rhel rhel]#

 

限制特定用户使用telnet

service telnet

{

        flags           = REUSE

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/sbin/in.telnetd

        log_on_failure  += USERID

        disable         = no        //是否禁用telnet

        protocol        = tcp              //指定协议

        port            = 23              //指定端口号

        bind            = 192.168.10.10  //绑定到某个主机(也可是接口,但要先指定协议和端口。)

        access_times    = 12:00-13:00 //指定允许访问的时间

        only_from       = 192.168.10.0/24     //可以访问的地址

        no_access       = 192.168.10.[20,30]              //不能访问的地址

        instances       = 2                                     //客户机最大并发连接数

        banner          = /banner1                        //在连接时显示的信息,/banner1是文件路径。

        banner_success  = /banner2                       //在成功连接时显示的信息,同样跟文件路径。

              banner_fail     = /banner3                          //在不能连接时显示的信息,同样跟文件路径。

 

}

 

[root@rhel ~]# touch /banner1 /banner2 /banner3;vi /banner1

welcome

[root@rhel ~]# vi /banner2

welcome to rhel

[root@rhel ~]# vi /banner3

you are no authentication

 

[root@rhel ~]# date -s "20100421 12:22:38"    //修改时间

[root@rhel ~]# service xinetd restart

 

测试。(略)

如果要更改telnet端口号,可以在/etc/services更改。

 

SSH基本配置:

linux系统一般默认安装ssh,查看本机是否安装ssh.

[root@rhel ~]# rpm -qa | grep ssh

openssh-4.3p2-29.el5                            //ssh核心文件。

openssh-server-4.3p2-29.el5          //ssh服务器软件包。

openssh-clients-4.3p2-29.el5          //ssh客户端软件包。

openssh-askpass-4.3p2-29.el5        //支持对话框的显示,一个基于X系统的密码诊断工具。

 

ssh配置文件:

有两个配置文件,/etc/ssh/ssh_config为客户端配置文件,/etc/ssh/sshd_config为服务器端的配置文件。

 

客户端配置文件:

[root@rhel2 ~]# vi /etc/ssh/ssh_config

# Host *       //设定配置文件生效的主机,*代表所有主机。

#   ForwardAgent no              //如果存在验证代理,可以设置连接是否经过验证代理转发给远程计算机。

#   ForwardX11 no          //设置X11连接是否被自动重定向到安全的通道和显示集。

#   RhostsRSAAuthentication no   //设置是否使用RSA算法的基于rhosts安全验证。

#   RSAAuthentication yes            // 设置是否使用RSA算法进行安全验证。

#   PasswordAuthentication yes    //设置是否使用口令认证。

#   HostbasedAuthentication no    //基于主机的认证。

#   BatchMode no                         //如果设置为yespassphrase/password的提示将被禁止。当不能交互式输入口令的时候,这个选项对脚本文件和批处理任务非常有用。

#   CheckHostIP yes                            //设置ssh是否查看连接到服务器的主机ip来防止DNS欺骗。

#   AddressFamily any                   //指定ssh应当使用哪种地址簇。

#   ConnectTimeout 0                   //设置连接超时。

#   StrictHostKeyChecking ask      //如果设置为yesssh就不会自动把计算机的密匙加入$home/.ssh/known_hosts文件,并且一旦计算机的密匙发生变化,就拒绝连接。

#   IdentityFile ~/.ssh/identity        //设置从哪个文件读取用户的RSA安全验证标识。

#   IdentityFile ~/.ssh/id_rsa

#   IdentityFile ~/.ssh/id_dsa

#   Port 22                                     //设置连接到远程主机的端口。

#   Protocol 2,1                           //设置客户端使用的ssh版本。

#     Cipher 3des                             //设置加密方式。

#   Ciphers aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc                                    //指定ssh-2允许使用的加密算法。

#   EscapeChar ~                          //设置escape字体。

#   Tunnel no                                //设置穿越不被信任的网络。

#   TunnelDevice any:any

#   PermitLocalCommand no

 

服务器端配置文件:

[root@rhel2 ~]# vi /etc/ssh/sshd_config

#Port 22                     //指定ssh监听的端口

#Protocol 2,1             //设定ssh版本

Protocol 2

#AddressFamily any

#ListenAddress 0.0.0.0            //设置服务器绑定的ip地址,0.0.0.0表示监听所有地址

#ListenAddress ::

 

# HostKey for protocol version 1

#HostKey /etc/ssh/ssh_host_key           //设置包含私匙的文件

# HostKeys for protocol version 2

#HostKey /etc/ssh/ssh_host_rsa_key

#HostKey /etc/ssh/ssh_host_dsa_key

 

# Lifetime and size of ephemeral version 1 server key

#KeyRegenerationInterval 1h

#ServerKeyBits 768          //定义服务器密匙长度

 

# Logging

# obsoletes QuietMode and FascistLogging

#SyslogFacility AUTH        //设置在记录来自sshd的消息时是否给出facility code

SyslogFacility AUTHPRIV

#LogLevel INFO               //设置记录sshd日志消息的层次

 

# Authentication:

 

#LoginGraceTime 2m        //如果用户不能成功登录,在切断连接之前服务器需要等待的时间

#PermitRootLogin yes       //设置root是否能够使用ssh登录

#StrictModes yes                     //ssh在接收登录请求之前是否检查用户家目录和rhosts文件的权限和所有权,防止目录和文件设置成任何人都有写的权限

#MaxAuthTries 6

 

#RSAAuthentication yes    //设置是否允许只有RSA安全验证

#PubkeyAuthentication yes

#AuthorizedKeysFile     .ssh/authorized_keys

 

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts

#RhostsRSAAuthentication no        //设置是否允许使用rhosts/etc/hosts.equiv加上RSA进行安全验证

# similar for protocol version 2

#HostbasedAuthentication no

# Change to yes if you don't trust ~/.ssh/known_hosts for

# RhostsRSAAuthentication and HostbasedAuthentication

#IgnoreUserKnownHosts no

# Don't read the user's ~/.rhosts and ~/.shosts files

#IgnoreRhosts yes                          //设置验证的时候是否使用rhostsshosts文件

 

# 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

 

# GSSAPI options

#GSSAPIAuthentication no

GSSAPIAuthentication yes

#GSSAPICleanupCredentials yes

GSSAPICleanupCredentials 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 mechanism.

# Depending on your PAM configuration, this may bypass the setting of

# PasswordAuthentication, PermitEmptyPasswords, and

# "PermitRootLogin without-password". If you just want the PAM account and

# session checks to run without PAM authentication, then enable this but set

# ChallengeResponseAuthentication=no

#UsePAM no

UsePAM yes

 

# 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

#AllowTcpForwarding yes

#GatewayPorts no

#X11Forwarding no

X11Forwarding yes                  //设置是否允许x11转发

#X11DisplayOffset 10

#X11UseLocalhost yes

#PrintMotd yes                         //设置sshd是否在用户登录时显示/etc/motd中的信息

#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

#PermitTunnel no

 

# no default banner path

#Banner /some/path

 

# override default of no subsystems

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

 

 

TELNET综合配置实例。

 

配置telnet服务器,要求如下

服务器IP为:192.168.100.209

绑定到eth0接口的5000端口上

能让root登录

允许192.168.100.0网段的访问

不允许192.168.100.100-192.168.100.110机器访问

不允许192.168.100.200-192.168.100.210机器访问

最大连接数为100,每客户的最大连接为10

限制客户最大连接速度为50,当超过50时连接中断20s

设置管理服务的用户为root,组为root

设置访问时间为8:00-12:00,14:00-18:00

禁止访问时间为00:00-8:00,12:00-14:00,18:00-24:00

设置telnet服务占用服务器的最大内存为16M,占用cpu最高为30

设置登录失败日志记录内容为客户端主机,用户名

设置登录成功日志记录内容为客户端进程ID,主机,用户名

设置客户机连接时显示的欢迎信息,'welcome to RHEL 5'

设置客户机成功登录时显示的信息,'You successfully login to the Telnet Server'

设置客户机登录失败时显示的信息,'You login failed,Please enter the correct user name and password'

 

配置步骤:

 

[root@rhel2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

IPADDR=192.168.100.209

NETMASK=255.255.255.0

 

[root@rhel2 ~]# rpm -ivh /misc/cd/Server/telnet-server-0.17-39.el5.i386.rpm

[root@rhel2 ~]# vi /etc/services

telnet          5000/tcp

telnet          5000/udp

 

[root@rhel2 ~]# mv /etc/securetty /etc/securetty.bak

[root@rhel2 ~]# vi /etc/xinetd.d/telnet

service telnet

{

        flags           = REUSE

        socket_type     = stream

        wait            = no

        user            = root

        group           = root

        protocol        = tcp

        port            = 5000

        server          = /usr/sbin/in.telnetd

        log_on_failure  += USERID

        disable         = no

        only_from       = 192.168.100.0/24

no_access       = 192.168.100.{100,101,102,104,105,106,107,108,109,110,200,201,202,203,204,205,206,207,208,209,210}      

这里应该有简写的方法,但我尝试了很多方法就是不行,以后知道了再改

        access_times    = 8:00-12:00 14:00-18:00      

              有了允许时间,也就不需要写拒绝时间了

        log_on_failure  = host userid

        log_on_success  = pid host userid

        rlimit_cpu      = 30

        rlimit_as       = 16M

        banner          = /banner1

        banner_success  = /banner2

        banner_fail     = /banner3

        instances       = 100

per_source      = 10

        cps             = 50 20

}

 

[root@rhel2 ~]# touch /banner1 /banner2 /banner3; vi /banner1

welcome to RHEL 5

[root@rhel2 ~]# vi /banner2

You successfully login to the Telnet Server

[root@rhel2 ~]# vi /banner3

You login failed, Please enter the correct user name and password

 

[root@rhel2 ~]# service xinetd restart

接下来,允许客户端访问。

[root@rhel2 ~]# iptables -I INPUT -p tcp --dport 5000 -j ACCEPT

 

上面配置文件里的不能访问地址也可以使用/etc/hosts.allow/etc/hosts.deny两个文件来实现。配置如下:

允许192.168.100网段访问而100101102103不能访问。

[root@rhel2 ~]# vi /etc/hosts.allow

in.telnetd:192.168.100. EXCEPT 192.168.100.100

in.telnetd:192.168.100. EXCEPT 192.168.100.101

 

这样编写这一个文件就可以了,也可以编写两个文件,如:

 

[root@rhel2 ~]# vi /etc/hosts.allow

in.telnetd:192.168.100.111

[root@rhel2 ~]# vi /etc/hosts.deny

in.telnetd:ALL

 

/etc/hosts.allow里面存放的是允许访问的主机,而/etc/hosts.deny里面放的是拒绝访问的主机,如果这个文件里写成ALL:ALL那就是所有人都不能访问所有服务了,这时就要在/etc/hosts.allow中添加允许访问的主机,只要没有出现在允许访问列表中的主机一律拒绝,这样也增强了安全性。如果这两个文件存在岐义,就以/etc/hosts.allow中的优先,举例说明:如果在允许列表中允许一台主机访问,以在拒绝列表中拒绝了这台主机的访问,那么这台主机是可以访问的。

 

完成,测试。

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

1

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广