鸟哥的服务器《五》限制连接端口

  1. port 有关的总结
    1 . 服务器端启动的监听端口所对应的服务是固定得
    2 . 客户端启动程序时,随机启动一个大于1024以上的端口
    3 . 一台服务器可以同时提供多种服务
    4 . 共 65535 个 port
    5 . 建立可靠的连接服务需要使用 TCP 协议,也就需要三次握手
    6 . 通信协议可以启动在非正规的 port,比如WWW主机可以更改端口

  2. 端口的查看

    1 . netstat:在本机上以自己的程序检测自己的port

    
    # 列出正在监听的网络服务
    
    [root@CentOS ~]# netstat -tunl
    
    
    # 列出已连接的网络连接状态
    
    [root@CentOS ~]# netstat -tun
    
    
    # 将已经建立,或者是正在监听当中的网络服务关闭
    
    [root@CentOS ~]# netstat -tunp
    [root@CentOS ~]# kill -9 2846
    

    2 . nmap

    
    # 使用默认参数扫描本机所启动的 port (TCP)
    
    [root@CentOS ~]# nmap localhost
    
    
    # 同时扫描本机的 TCP/UDP 端口
    
    [root@CentOS ~]# nmap -sTU localhost
    
    
    # 通过 ICMP 数据包的检测,分析局域网内有几台主机是启动的
    
    
    [root@CentOS ~]# nmap -sP 192.168.1.0/24
    
    
    # 将局域网内各个主机的端口检测一遍
    
    [root@CentOS ~]# nmap 192.168.1.0/24
    
  3. 端口与服务的启动/关闭及开机时状态设定
    port是在执行某软件之后被软件激活的,所以要关闭某些port 时,可直接将某个程序关闭就好了
    1 . 服务的启动与管理分两种 (当次有效)
    (1)stand alone:直接执行该服务的可执行文件,让该执行文件直接加载到内存当中运行。目录:/etc/init.d。 重启服务:/etc/init.d/sshd restart
    (2)super daemon:用一个超级服务作为总管来统一管理某些特殊的服务、目录:/etc/xinetd.d。 重启服务:/etc/inetd.d restart

    
    # 关闭111端口,需要先找出程序
    
    [root@CentOS ~]# netstat -tnlp | grep 111  #找到程序
    tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1576/rpcbind        
    tcp        0      0 :::111                      :::*                        LISTEN      1576/rpcbind        
    [root@CentOS ~]# which rpcbind  #找到文件
    /sbin/rpcbind
    [root@CentOS ~]# rpm -qf /sbin/rpcbind  #找出软件
    rpcbind-0.2.0-12.el6.x86_64
    [root@CentOS ~]# rpm -qc rpcbind | grep init  #找出服务
    /etc/rc.d/init.d/rpcbind
    [root@CentOS ~]# /etc/init.d/rpcbind stop
    Stopping rpcbind:                                          [  OK  ]
    
    
    # 也可以直接用kill
    
    [root@CentOS ~]# kill -9 1576
    
    
    # 尝试启动telnet
    
    [root@CentOS ~]# rpm -qa | grep telnet-server  #查询是否存在服务
    [root@CentOS ~]# yum isntall telnet-server  #安装服务
    [root@CentOS ~]# vim /etc/xinetd.d/telnet  #因为是super daemon,修改配置文件
    service telnet
    {
            flags           = REUSE
            socket_type     = stream
            wait            = no
            user            = root
            server          = /usr/sbin/in.telnetd
            log_on_failure  += USERID
            disable         = no   #修改“yes”为“no”
    }
    [root@CentOS ~]# /etc/init.d/xinetd restart  #重启服务
    [root@CentOS ~]# netstat -tnlp  #查看服务是否开启
    

    2 . 默认启动的服务 (重启有效)
    runlevel启动的数据都放置在/etc/re.d/re[0-6].d , 可以使用chkconfig命令

    
    # 查阅rpcbind 这个程序是否开机就启动
    
    [root@CentOS ~]# chkconfig --list | grep rpcbind
    rpcbind         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    
    
    # 如果开机启动,将其改为开机不启动
    
    [root@CentOS ~]# chkconfig --level 35 rpcbind off
    
    
    # 立刻关闭这个服务
    
    [root@CentOS ~]# /etc/init.d/rpcbind stop
    
  4. 关闭网络服务端口

    
    # 找出目前系统上正在运行的服务,并且找到相对应的启动脚本(/etc/init.d/ 内的文件名)
    
    [root@CentOS ~]# netstat -tlunp
    [root@CentOS ~]# rpm -qc $(rpm -qf $(which rpc.statd) ) | grep init
    
    
    # 编写脚本关闭不需要的端口
    
    [root@CentOS ~]# vim closedaemon.sh
    for daemon in nfs nfslock rpcgssd rpciamapd rpcsvcgssd xinetd rpcbind
    do
            chkconfig $daemon off
            /etc/init.d/$daemon stop
    done
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值