linux hosts.allow 只允许adsl动态ip登录

简介:

【场景】公司采用ADSL拨号上网,即上网获得是动态IP。

服务器安全策略升级,只允许公司内可以访问服务器。

实现过程:

服务器指定固定IP可以访问服务器,其实很容易,一般有以下三下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
方法一:
/etc/hosts .allow中添加允许 ssh 登陆的ip或者网段    
sshd:192.168.1.2:allow 或者
sshd:192.168.1.0 /24 :allow 
/etc/hosts .deny添加不允许 ssh 登陆的IP
sshd:ALL            #ALL表示除了上面允许的,其他的ip 都拒绝登陆ssh
方法二:
使用iptables。   
iptables -A INPUT -p tcp -s 192.168.1.2 --destination-port 22 -j ACCEPT 
iptables -A INPUT -p tcp --destination-port 22 -j DROP 
方法三:
修改 ssh 配置文件
vi  /etc/ssh/sshd_config 
添加一行: 
allowusers xxx@192.168.1.2  
注:xxx为你用来登入服务器的用户名。

我以方法一实现,限制ADSL动态IP进行登录,

方法简单:通过花生壳或者到kmdns注册账户,这样就会得到一个域名,我们在公司内网登录这个账户,

在服务器上解析得到IP就可以了。

我用的是TPLINK的路由器本身支持动态域名账户登录,好了,拿来直接用了。

在服务器用脚本实现

先配置hosts.allow文件,按以下格式配置

1
sshd:13.18.4.36:allow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@Web: /var/scripts # vi /etc/hosts.allow 
# /etc/hosts.allow: list of hosts that are allowed to access the system.
#                   See the manual pages hosts_access(5) and hosts_options(5).
#
# Example:    ALL: LOCAL @some_netgroup
#             ALL: .foobar.edu EXCEPT terminalserver.foobar.edu
#
# If you're going to protect the portmapper use the name "portmap" for the
# daemon name. Remember that you can only use the keyword "ALL" and IP
# addresses (NOT host or domain names) for the portmapper, as well as for
# rpc.mountd (the NFS mount daemon). See portmap(8) and rpc.mountd(8)
# for further information.
#
sshd:13.18.4.36:allow

编写获得动态域名IP并替换allow文件中内容脚本,

dig +short直接解析域名得到IP很简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@Web: /var/scripts # vi getip.sh
  
#!/bin/bash
 
#解析得到myku.kmdns.net动态域名IP
getip=` dig  +short myku.kmdns.net`
 
#得到原来allow文件中的IP
oldip=` cat  /etc/hosts .allow| grep  sshd | awk  -F ':'  '{print $2}' | head  -n1`
 
 
if  [ $getip != $oldip ]
 
     then
     sed  -i  "s/$oldip/$getip/g"  /etc/hosts .allow
  
else
    exit
 
fi
 
root@Web: /var/scripts # chmod 777getip.sh

然后加入到自动任务中,每分钟检测一次

1
2
3
4
5
6
7
#allow myku ip to login server
 
* /1  * * * *  /var/scripts/getip .sh  >  /dev/null  2>&1
 
 
别忘了 /etc/hosts .deny添加不允许 ssh 登陆的IP
sshd:ALL            #ALL表示除了上面允许的,其他的ip 都拒绝登陆ssh

这样子就实现了hosts.allow 只允许adsl动态ip登录功能。

同理,我们也可以用另两个方法,这里就不多讲了。


本文转自 jackjiaxiong 51CTO博客,原文链接:http://blog.51cto.com/xiangcun168/1699264


相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
28天前
|
Linux 网络安全
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
27 1
|
6天前
|
Ubuntu 网络协议 Linux
|
7天前
|
JSON 网络协议 Linux
Linux ip命令:网络的瑞士军刀
【4月更文挑战第25天】
11 1
|
9天前
|
网络协议 Linux 开发工具
|
11天前
|
监控 Linux 网络安全
Linux服务器如何查询连接服务器的IP
【4月更文挑战第17天】Linux服务器如何查询连接服务器的IP
13 1
|
12天前
|
Ubuntu Linux
Linux(Ubuntu)系统临时IP以及静态IP配置(关闭、启动网卡等操作)
请注意,以上步骤是在临时基础上进行配置的。如果要永久保存静态IP地址,通常还需要修改 `/etc/network/interfaces`文件,以便在系统重启后保持配置。同时,确保备份相关配置文件以防止出现问题。
27 1
|
13天前
|
Linux 网络安全
【Linux】如何修改为固定ip和增加端口
【Linux】如何修改为固定ip和增加端口
8 1
|
13天前
|
安全 Unix Linux
基于MobaXterm工具的Linux远程SSH登录
本文介绍了如何使用 MobaXterm 进行 Linux 远程登录。首先,从官方网站下载并安装 MobaXterm。启动后,创建新的远程会话,选择 SSH 类型,输入服务器 IP、主机名和端口。接着,提供用户名和密码进行连接。成功后,即可使用远程终端执行命令。此外,文章还提到了其他常用的 Linux 远程登录工具,如 PuTTY、OpenSSH、SecureCRT 和 Xshell,强调了选择工具时应考虑个人偏好和安全需求。
|
14天前
|
Linux
【Linux】虚拟机ip端口如何固定
【Linux】虚拟机ip端口如何固定
9 0
|
14天前
|
存储 Linux Android开发
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
38 0