Linux:允许/限制用户的 IP 绑定权限

hex*_*ide 5 linux ip

我在 RHEL (CentOS 6.3) 上运行一台使用多个 IP 地址的专用机器。多个用户也可以使用非超级用户帐户访问机器。我想阻止它们绑定到某些地址。

我知道 Linux 可以限制非 root 用户的端口,就像目前对小于或等于 1024 的端口所做的那样。如果我想阻止访问特定的 IP 地址,例如0.0.0.0,或范围,例如127.0.0.0/8,会这样做有可能吗,如果有,怎么做?

或者相反,我如何拒绝所有访问以绑定到任何 IP 地址,并授予用户对单个地址的访问权限?

fue*_*ero 2

我不会详细介绍如何设置 SELinux 或如何创建 SELinux 策略。可能是熟悉 SELinux 的一个很好的起点。

要解决 SELinux 的问题,请尝试以下操作:

  • 为您想要限制的网络接口分配类型

    # Assign a type to the whole interface
    semanage interface -a -t foo_netif_t eth2
    
    Run Code Online (Sandbox Code Playgroud)
  • 为通过接口的流量分配标签

    netlabelctl unlbl add interface:eth2 address:0.0.0.0/0 label:system_u:object_r:foo_peer_t:s0
    netlabelctl unlbl add interface:eth2 address:::/0 label:system_u:object_r:foo_peer_t:s0
    
    Run Code Online (Sandbox Code Playgroud)

    此示例将类型分配foo_peer_t给所有 IPv4 和 IPv6 流量。

  • 添加规则以允许数据包流

    交通进入

    allow user_t foo_netif_t:netif ingress;
    allow user_t foo_peer_t:node recvfrom;
    
    Run Code Online (Sandbox Code Playgroud)

    交通离开

    allow user_t foo_netif_t:netif egress;
    allow user_t foo_peer_t:node sendto;
    
    Run Code Online (Sandbox Code Playgroud)

    替换user_t为分配给您要限制的用户的类型。

参考: