redhat9.0 限制IP 登陆的困惑 ![摘]

redhat9.0 限制IP 登陆的困惑?

我是个LINUX 新手 如何限制IP登陆这个问题一直困扰这我。我把问题讲一下,大家帮忙一下。

我的系统是REDHAT9 远程登陆服务器的方式是 ssh,我查看乐客户端配置文件ssh_config 和服务器端的配置文件 sshd_config 都没有找到哪个 参数能限定只允许某个固定的IP能登陆服务器的(?? 这是我一个疑问)

我想到可能要装TCP wrapper ,利用hosts.allow 和 hosts.deny 来只允许,某个固定的IP SSH登陆,其他的IP都不能登陆。我网上查了下只有老版本的inetd.conf 这个配置文件和tcp wrapper 结合 而 新的xinetd.d里文件的配置如何操作(这是我一个疑问??,xinetd.d 目录下没有ssh,是不是自己要重建?)

sshd 是独立于 xinetd deamon 的,如何自身限制IP登陆??

请大家指点迷津,先谢了!


--------------------------------------------------------------------------------
網中人 回复于:2005-05-22 21:16:44
sshd 不經 xinetd , 但仍經過 tcpd 啊.

你就在 hosts.deny 設:
sshd: ALL EXCEPT 1.2.3.4 4.3.2.1

--------------------------------------------------------------------------------
bluce-c 回复于:2005-05-23 17:09:52
我参照你的,在hosts.deny 添加了,还是不起作用.

?请求支援!

--------------------------------------------------------------------------------
網中人 回复于:2005-05-24 07:38:08
那你分開寫吧:
在 /etc/hosts.allow 寫:
sshd: 1.2.3.4
在 /etc/hosts.deny 寫:
sshd: ALL

如果還是不行, 那可能你的 sshd 沒支援 tcpwrapper,
你可找文件看看如何打開支援.
要不, 就用 iptables 也行:
iptables -I INPUT -p tcp --dport 22 -j DROP
iptables -I INPUT -p tcp --dport 22 -s 1.2.3.4 -j ACCEPT

--------------------------------------------------------------------------------
網中人 回复于:2005-05-24 08:17:34
剛好, 前幾天弄了個小玩意, 給有興趣的朋友參考一下.

* 需求:
某一多人管理的 server (假設為 www.server.name) 原本並沒有限制 ssh 的來源.
現想加入特定來源控制讓某些管理員能夠遠端登錄, 其餘一蓋不允許.

* 難點:
由於個別管理員使用撥接方式上網, ip 不固定. 因此清單難以掌握.

* 構思:
1) 開發一個 php 網頁, 密碼以 ssl 連線保護, 以即時加入 client 端 IP 進 allow list 裡.
2) 再執行 firewall script , 開啟 ssh 連線.
3) 然後定期清理清單.

* 實作:
1) 寫一個 getip.php 至於某一網頁目錄底下(如 getip), 用來收集 client 端 browser ip 記錄於 allow_ssh.txt (單一記錄, 每次蓋寫). 內容如下:
[code:1:df143b7244]<?
//Set dir path for ip list
$dir_path=".";

//Set filename for ip list
$ip_list="allow_ssh.txt";

//Get client ip
$user_ip = getenv ("REMOTE_ADDR");

//Put client ip to a file
if(@!($file = fopen("$dir_path/$ip_list","w+")))
{
echo "Permission denied!!.......Pls Check your rights to dir $dir_path o
r file $ip_list";
}
else
{
fputs($file,"$user_ip");
fclose($file);
echo "client ip($user_ip) has put into $dir_path/$ip_list";
}
?>[/code:1:df143b7244]

[注意]: ssl 及 htpasswd 網頁密碼部份這裡從略. 請另行參考 web server 文件.

2) 修改 /etc/services , 增加項目:
[code:1:df143b7244]myssh 222/tcp[/code:1:df143b7244]

3) 用 xinetd 監聽 port 222 , 並啟動 /root/iptables/sshadd.sh.
可新編 /etc/xinetd.d/myssh , 內容如下:
[code:1:df143b7244]service myssh
{
socket_type = stream
protocol = tcp
wait = no
user = root
server = /root/iptables/sshadd.sh
disable = no
}
[/code:1:df143b7244]

4) sshadd.sh 會設定 iptables , 打開 allow_ssh.txt 清單的 ssh 連線.
編輯 /root/iptables/sshadd.sh, 內容如下:
[code:1:df143b7244]#!/bin/bash

list_dir=/var/www/html/getip
list_file=$list_dir/allow_ssh.txt
chain_name=ssh_rules
mail_to=root
# clear chain if exits, or create chain.
/sbin/iptables -L -n | /bin/grep -q "^Chain $chain_name" && {
/sbin/iptables -F $chain_name
true
} || {
/sbin/iptables -N $chain_name
/sbin/iptables -I INPUT -j $chain_name
}

# clear chain on demand
[ "$1" = clear ] && {
/sbin/iptables -F $chain_name
exit 0
}

# do nothing while list is empty
[ -s $list_file ] || exit 1

# add rule
/sbin/iptables -A $chain_name -p TCP --dport 22 -s $(cat $list_file) -j ACCEPT && \
echo "ssh opened to $(cat $list_file) on $(date)" | mail -s "sshadd" $mail_to
[/code:1:df143b7244]

5) 寫 crontab 每 3 分中清理 iptables 關於 ssh 連線的規則.
[code:1:df143b7244]*/3 * * * * root /study-area/iptables/sshadd.sh clear[/code:1:df143b7244]
這並不影響既有連線, 若逾時則再連.

* 操作步驟:
1) 用 browser 開啟 http://www.server.name/getip/getip.php
2) 用 terminal 執行 telnet www.server.name 222
3) 最後用管理員自己的帳戶連接登錄 ssh www.server.name

* 注意:
1) iptables 預設規則並沒開啟任何 ssh 連線, 若有固定 ip 可用, 可另行配置預設規則.
2) tcpwraper 不可設定 deny 規則, 否則上述無效.

--------------------------------------------------------------------------------
好好先生 回复于:2005-05-24 09:54:46
用iptables限制只允许某个IP连接,修改sshd_config使用非常规端口会更让你省心一些.


例:vi sshd_config 把port 22前的注释去掉改成port 你允许的端口.service sshd restart
然后/sbin/iptables -A INPUT -s w.x.y.z -p tcp --dport 2234 -j ACCEPT(把w.x.y.z换成你要允许连接的IP)

try it.good luck!

--------------------------------------------------------------------------------
網中人 回复于:2005-05-24 15:35:03
改 port 沒用的,
隨便一掃都出來了...

早上我提的主要是解決 ip 來源不固定問題.

--------------------------------------------------------------------------------
不想发言 回复于:2005-05-24 16:07:21
[quote:25ffe6bc5c="網中人"]改 port 沒用的,
隨便一掃都出來了...

早上我提的主要是解決 ip 來源不固定問題.[/quote:25ffe6bc5c]

如果是我,我会改了ssh的版本号,并且改了他的端口.他扫了也不知所云 :mrgreen:

--------------------------------------------------------------------------------
好好先生 回复于:2005-05-24 16:22:49
[quote:60e800370d="網中人"]改 port 沒用的,
隨便一掃都出來了...

早上我提的主要是解決 ip 來源不固定問題.[/quote:60e800370d]

谢谢网大哥.你的很实用啊......学习ing........ :oops:

--------------------------------------------------------------------------------
網中人 回复于:2005-05-24 16:30:56
[quote:e5bb9b2a8c="不想发言"]

如果是我,我会改了ssh的版本号,并且改了他的端口.他扫了也不知所云 :mrgreen:[/quote:e5bb9b2a8c]

好一點的嗅探工具, 不是看版本號碼的...
這個想法有點單純啦.

--------------------------------------------------------------------------------
wangfeixue 回复于:2005-05-25 08:54:01
用pam限制login的使用权限??

--------------------------------------------------------------------------------
我自逍遥 回复于:2005-05-25 09:00:52
可以在iptables下设置哪个地址的22端口连接可以,那个不可以。

--------------------------------------------------------------------------------
我自逍遥 回复于:2005-05-25 09:11:04
这个脚本可以只允许你许可的IP连接。
$LAN=允许的IP
$EXT_IF=你的网卡IP
$PORT=你的端口号,如22。

for PORT in $TRUSTED_LOCAL_TCP_PORT; do
iptables -A INPUT -s ! $LAN -i $EXT_IF -p tcp --dport $PORT -m state --state NEW -j DROP
done


for PORT in $TRUSTED_LOCAL_UDP_PORT; do
iptables -A INPUT -s ! $LAN -i $EXT_IF -p udp --dport $PORT -m state --state NEW -j DROP
done

--------------------------------------------------------------------------------
西山晴雪 回复于:2005-05-31 17:10:45
sshd2_config
里有个参数
AllowHost
你把这个注释去掉,然后在后面加上你所允许的ip就可以了。

--------------------------------------------------------------------------------
youke 回复于:2005-06-04 01:48:51
限制ip, 还不如限制账号, 修改/etc/security/access文件即可. 由于现在手头没有redhat, 所以具体修改内容记不清了, 不好意思..

--------------------------------------------------------------------------------
cefs99 回复于:2005-06-08 13:34:15
[quote:9142aa0069="網中人"]改 port 沒用的,
隨便一掃都出來了...

早上我提的主要是解決 ip 來源不固定問題.[/quote:9142aa0069]

这个方法非常实用,谢谢!

有一个实际中的问题是,

我的服务器现在已经使用配置文件来通过iptables来管理端口。譬如

这是有关ssh的
-A INPUT -p tcp -m tcp --dport 2210 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 2210 -j ACCEPT

我尝试了一下你的方法, iptables -L后看到的结果是
Chain ssh_rules (1 references)
target prot opt source destination
ACCEPT tcp -- 61.49.23.189 anywhere tcp dpt:2210


但是这个新增的chain似乎没有起作用。是否需要把握原有的rules关掉,只使用ssh_rules才可以?

因为我的服务器在异地,不能测试,烦请确认。

谢谢

--------------------------------------------------------------------------------
水中风铃 回复于:2005-06-08 13:53:24
[quote:932396c0c4="youke"]限制ip, 还不如限制账号, 修改/etc/security/access文件即可. 由于现在手头没有redhat, 所以具体修改内容记不清了, 不好意思..[/quote:932396c0c4]


改sshd_config就可以只让充许的用户登录了.

--------------------------------------------------------------------------------
網中人 回复于:2005-06-08 14:25:27
回 cefs99:

將 -A 改為 -I 試試?
還有, OUTPUT 那行, --dport 改為 --sport .

--------------------------------------------------------------------------------
llzqq 回复于:2005-06-08 15:17:29
现在开始喜欢用KEY验证了,不怕有人暴力破解

你可能感兴趣的:(PHP,redhat,ssh,配置管理,ext)