限制IP访问oracle数据库

  设置oracle数据库IP访问限制(IP地址白名单黑名单)
 

可以通过修改sqlnet.ora文件来限制某些IP地址的访问,修改完成后,可能需重启监听生效。(可以lsnrctl reload)
如果$ORACLE_HOME/network/admin/目录下没有sqlnet.ora文件,可以从samples目录下拷贝个sqlnet.ora或者直接vi
###########二选一##############
#允许访问的IP
TCP.INVITED_NODES=(192.168.1.103,ip2,ip3,..,..本地IP..)若使用白名单,必须本地IP,否则监听会起不来
#不允许访问的IP
TCP.EXCLUDED_NODES=(192.168.1.102)
#检测上述参数的设置
TCP.VALIDNODE_CHECKING=yes (这个参数是必须的)

#允许访问的ip tcp.invited_nodes =(ip1,ip2,……) #不允许访问的ip tcp.excluded_nodes=(ip1,ip2,……)
TCP.INVITED_NODES:设置允许访问数据库的IP地址列表,当这个参数和TCP.EXCLUDED_NODES设置的地址相同的时候将覆盖TCP.EXCLUDED_NODES设置。

当不允许访问的客户端连入时,会出现下列错误
sys/oracle@approd.test as sysdba
ERROR:
ORA-12537: TNS:connection closed


Warning: You are no longer connected to ORACLE.


需要注意的问题:
1、 需要设置参数为YES,这样才能激活。
2、 建议设置允许访问的IP,因为IP地址有可能被随意修改,就不能起到自己的目的。
3、 TCP当参数TCP.INVITED_NODES和TCP.EXCLUDED_NODES设置的地址相同的时候将覆盖TCP.EXCLUDED_NODES设置。
4、 需要重启监听器才能生效。
5、 这个方式只是适合TCP协议。
6、 这个配置适用于9i以上版本。在9i之前的版本使用文件protocol.ora。
7、 在服务器上直接连接数据库不受影响。
8、 这种限制方式事通过监听器来限制的。
 9、 这个限制只是针对IP检测,对于用户名检测事不支持的。
10、使用触发器(通过监听器的限制,通常属于轻量级的,比在数据库内部通过触发器进行限制效率要高)

<!--
管理员在2009年8月13日编辑了该文章文章。
-->



在oracle数据库上设置限制IP地址访问以及注意事项

近期应客户要求,需要对访问生产环境的Oracle数据库的ip做一些限制,即:只有通过审核的ip才能访问数据库,其他ip一律禁止访问数据库。
在oracle中可以通过sqlnet.ora文件的设置或者通过触发器可以实现对特定ip的限制访问。
1、修改sqlnet.ora文件:
步骤:
a)测试在未设置前某一客户端的登录数据库情况:
C:\Documents and Settings\ThinkPad>sqlplusapp/app@uat17
SQL*Plus: Release 9.2.0.1.0 - Production on Thu Aug 30 11:02:21 2012
Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select name from v$database;
NAME
---------
PROD
SQL>
--说明连接成功
b)通过对文件:$ORACLE_HOME/network/admin/sqlnet.ora文件添加:
tcp.validnode_checking=yes
tcp.invited_nodes=(192.168.1.61)
tcp.excluded_nodes=(192.168.1.90)
说明:
第一行的含义:开启IP限制功能;
第二行的含义:允许访问数据库的IP地址列表,多个IP地址使用逗号分开,此例中我们写入数据库服务器的IP地址;
第三行的含义:禁止访问数据库的IP地址列表,多个IP地址使用逗号分开,此处我们写入欲限制的IP地址192.168.1.90。

c)重新启服务器端listener,或者lsnrctl reload方式使刚才的修改在监听中生效。
[oracle@APPDB-PROD admin]$ lsnrctl reload
LSNRCTL for Linux: Version 10.2.0.4.0 - Production on 30-AUG-2012 10:17:43
Copyright (c) 1991, 2007, Oracle.  All rights reserved.
Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
The command completed successfully

d)验证客户端(192.168.1.90)登录情况:
C:\Documents and Settings\ThinkPad>sqlplusapp/app@uat17
SQL*Plus: Release 9.2.0.1.0 - Production on Thu Aug 30 11:12:46 2012
Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
ERROR:
ORA-12537: TNS:connection closed

Enter user-name:
报:ORA-12537: TNS:connection closed错误,说明限制成功。
注意事项:
1).上文中添加的第一项必须要写,任何平台都可以,但是只适用于TCP/IP协议。
2).第二行和第三行可以任意写一行,如果tcp.invited_nodes与tcp.excluded_nodes都存在,以tcp.invited_nodes为主.
3).一定要许可或不要禁止服务器本机的IP地址,否则通过lsnrctl将不能启动或停止监听,因为该过程监听程序会通过本机的IP访问监听器,而该IP被禁止了,但是通过服务启动或关闭则不影响,如果是rac最好将物理ip和虚拟ip都需要准许访问。
2.通过触发器实现:
conn /as sysdba
create or replace trigger  check_ip
after logon on app.schema
declare
ipaddr VARCHAR2(30);
begin
select sys_context('userenv', 'ip_address') into ipaddr from dual;
if ipaddr like ('192.168.1.90') then
raise_application_error('-20001', 'you can not logon by app');
end if;
 end ;
/
但是这个只能是针对某一用户做限制的。



oracle RAC 上创建IP地址白名单

由于业务需要,需要使用白名单限制用户登录数据库的地址,决定使用数据库白名单功能!
一般但实例数据库启动白名单只需要在sqlnet.ora中添加以下内容就可:
TCP.VALIDNODE_CHECKING=yes  (开启IP限制功能),
TCP.INVITED_NODES=(192.168.1.103,ip2,ip3,..,..本地IP..)--白名单,必须本地IP,否则监听会起不来,
TCP.EXCLUDED_NODES=(192.168.1.102) --黑名单
由于本环境为RAC环境,操作稍有不同。
1 Oracle用户下的$ORACLE_HOME/NETWORK/ADMIN目录下没有sqlnet.ora文件,必须使用GUID用户登录进入$ORACLE_HOME/NETWORK/ADMIN下的sqlnet.ora文件,添加修改。
2 添加白名单时必须添加本地IP,因为是RAC环境,必须把连个节的真实IP,私有IP,VIP,SCAN IP全部添加进白名单。
3 启动时不能像单机一样使用lsnrctl reload,需使用srvctl stop listener -n 节点1,srvctl start listener -n 节点1,可以一个节点修改好后,再修改另一个节点,防止影响业务。






限制IP访问oracle数据库

如果要灵活的实现限制ip访问oracle数据库,最好使用登录触发器的方式来实现。

下面的方式对限制单个ip访问oracle数据库比较方便。因为这种方法需要把ip地址一个一个的列出来。

通过SQLNET.ORA添加如下的语句即实现ip的限制。

    tcp.validnode_checking = yes //开启IP限制

  tcp.invited_nodes=() //允许访问的IP列表,各IP之间用逗号分隔

  tcp.excluded_nodes=() //限制访问的IP列表,各个IP之间用逗号分隔

   本次测试的环境说明如下:

  数据库服务器在我WINDOWS PC机器上,IP地址为10.223.18.116,监听端口为1521,实例名为yansp。

  远程有2台AIX服务器10.192.39.72,10.192.39.76用作测试客户端。

正常情况下10.192.39.72是可以正常访问数据库的,如下:

10.192.39.72 > sqlplus yansp/yansp@10.223.18.116/yansp

  SQL*Plus: Release 10.2.0.4.0 - Production on Wed Jan 11 10:18:03 2012

  Copyright (c) 1982, 2007, Oracle. All Rights Reserved.

  Connected to:

  Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production

  With the Partitioning, OLAP and Data Mining options

  SQL> select sysdate from dual;

  SYSDATE

  -------------------

  2012-01-11 10:19:08

  SQL>

1,修改sqlnet.ora 增加如下2行

  tcp.validnode_checking = yes

  tcp.excluded_nodes=(10.192.39.72,10.192.39.72),

2,在cmd中相应的目录下对sqlnet.ora 文件执行type命令。

  C:\u01\oracle\product\10.2.0\db_1\NETWORK\ADMIN>type sqlnet.ora

显示sqlnet.ora文件中的内容:

  # This file is actually generated by netca. But if customers choose to

  # install "Software Only", this file wont exist and without the native

  # authentication, they will not be able to connect to the database on NT.

  SQLNET.AUTHENTICATION_SERVICES = (NTS)

  NAMES.DIRECTORY_PATH=(TNSNAMES,EZCONNECT)

  SQLNET.EXPIRE_TIME=1

  tcp.validnode_checking = yes

  tcp.excluded_nodes=(10.192.39.72)

  C:\u01\oracle\product\10.2.0\db_1\NETWORK\ADMIN>

3,重启启动监听器:通过cmd 命令,先关闭监听。

  C:\u01\oracle\product\10.2.0\db_1\NETWORK\ADMIN>lsnrctl stop

  LSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Production on 11-1月 -2012 10:2

  4:36

  Copyright (c) 1991, 2005, Oracle. All rights reserved.

  正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.223.18.116)(PORT=1521)(I

  P=FIRST)))

  命令执行成功

 

通过cmd命令开启监听

    C:\u01\oracle\product\10.2.0\db_1\NETWORK\ADMIN>lsnrctl start

  LSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Production on 11-1月 -2012 10:2

  4:40

  Copyright (c) 1991, 2005, Oracle. All rights reserved.

  启动tnslsnr: 请稍候…

  TNSLSNR for 32-bit Windows: Version 10.2.0.1.0 - Production

  系统参数文件为C:\u01\oracle\product\10.2.0\db_1\network\admin\listener.ora

  写入C:\u01\oracle\product\10.2.0\db_1\network\log\listener.log的日志信息

  监听: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.223.18.116)(PORT=1521)))

  正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.223.18.116)(PORT=1521)(I

  P=FIRST)))

  LISTENER 的 STATUS

  ------------------------

  别名 LISTENER

  版本 TNSLSNR for 32-bit Windows: Version 10.2.0.1.0 - Produ

  ction

  启动日期 11-1月 -2012 10:24:45

  正常运行时间 0 天 0 小时 0 分 5 秒

  跟踪级别 off

  安全性 ON: Local OS Authentication

  SNMP OFF

  监听程序参数文件 C:\u01\oracle\product\10.2.0\db_1\network\admin\listen

  er.ora

  监听程序日志文件 C:\u01\oracle\product\10.2.0\db_1\network\log\listener

  .log

  监听端点概要…

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.223.18.116)(PORT=1521)))

  服务摘要

  服务 "auxdb" 包含 1 个例程。

  例程 "auxdb", 状态 UNKNOWN, 包含此服务的 1 个处理程序…

  命令执行成功

 

 C:\u01\oracle\product\10.2.0\db_1\NETWORK\ADMIN>sqlplus / as sysdba

  SQL*Plus: Release 10.2.0.1.0 - Production on 星期三 1月 11 10:24:49 2012

  Copyright (c) 1982, 2005, Oracle. All rights reserved.

  连接到:

  Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production

  With the Partitioning, OLAP and Data Mining options

  SQL> alter system register;

  系统已更改。

  SQL> exit

从 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production With the Partitioning, OLAP and Data Mining options 断开

  C:\u01\oracle\product\10.2.0\db_1\NETWORK\ADMIN>

 

  再次在10.192.39.72连接数据库就会报ORA-12537错误。

  10.192.39.72 > sqlplus yansp/yansp@10.223.18.116/yansp

  SQL*Plus: Release 10.2.0.4.0 - Production on Wed Jan 11 10:34:59 2012

  Copyright (c) 1982, 2007, Oracle. All Rights Reserved.

  ERROR:

  ORA-12537: TNS:connection closed

  Enter user-name:

  ERROR:

  ORA-01017: invalid username/password; logon denied

  Enter user-name:

  ERROR:

  ORA-01017: invalid username/password; logon denied

  SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus

  10.192.39.72 > oerr ora 12537

  12537, 00000, "TNS:connection closed"

  // *Cause: "End of file" condition has been reached; partner has disconnected.

  // *Action: None needed; this is an information message.

 

  10.192.39.72 >

  而此时10.192.39.76还是可以登录数据库的:

  10.192.39.76 > sqlplus yansp/yansp@10.223.18.116/yansp

  SQL*Plus: Release 10.2.0.4.0 - Production on Wed Jan 11 10:39:03 2012

  Copyright (c) 1982, 2007, Oracle. All Rights Reserved.

  Connected to:

  Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production

  With the Partitioning, OLAP and Data Mining options

  SQL> select sysdate from dual;

  SYSDATE

  -------------------

  2012-01-11 10:39:17

  SQL>

 

  如果将参数tcp.excluded_nodes=(10.192.39.72) 修改为 tcp.invited_nodes=(10.192.39.72) 那么只允许

  10.192.39.72可以访问数据库。

  修改SQLNET.ORA的内容如下:

  SQLNET.AUTHENTICATION_SERVICES = (NTS)

  NAMES.DIRECTORY_PATH=(TNSNAMES,EZCONNECT)

  SQLNET.EXPIRE_TIME=1

  tcp.validnode_checking = yes

  tcp.invited_nodes=(10.192.39.72,10.223.18.116)

  并且重启监听器。

  注意:在使用参数invited_nodes的时候,一定要将数据库服务器的IP地址列入,否则启动监听器回报如下错误:

  C:\Documents and Settings\shoupeng.yan>lsnrctl start

  LSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Production on 11-1月 -2012 10:4

  7:37

  Copyright (c) 1991, 2005, Oracle. All rights reserved.

  启动tnslsnr: 请稍候…

  TNSLSNR for 32-bit Windows: Version 10.2.0.1.0 - Production

  系统参数文件为C:\u01\oracle\product\10.2.0\db_1\network\admin\listener.ora

  写入C:\u01\oracle\product\10.2.0\db_1\network\log\listener.log的日志信息

  监听: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.223.18.116)(PORT=1521)))

  正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.223.18.116)(PORT=1521)(I

  P=FIRST)))

  TNS-12537: TNS: 连接关闭

  TNS-12560: TNS: 协议适配器错误

  TNS-00507: 连接已关闭

  现象将会是72可以正常连接数据库,76不可以连接数据库。

  10.192.39.72 > sqlplus yansp/yansp@10.223.18.116/yansp

  SQL*Plus: Release 10.2.0.4.0 - Production on Wed Jan 11 10:49:58 2012

  Copyright (c) 1982, 2007, Oracle. All Rights Reserved.

  Connected to:

  Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production

  With the Partitioning, OLAP and Data Mining options

  SQL>

  10.192.39.76 > sqlplus yansp/yansp@10.223.18.116/yansp

  SQL*Plus: Release 10.2.0.4.0 - Production on Wed Jan 11 10:50:01 2012

  Copyright (c) 1982, 2007, Oracle. All Rights Reserved.

  ERROR:

  ORA-12537: TNS:connection closed

  Enter user-name:

  ERROR:

  ORA-01017: invalid username/password; logon denied

  Enter user-name:

  ERROR:

  ORA-01017: invalid username/password; logon denied

  SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus

  10.192.39.76 >

  注意:如果同时制定了参数tcp.invited_nodes和参数tcp.excluded_nodes那么将优先使用参数tcp.invited_nodes。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24162410/viewspace-1820641/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24162410/viewspace-1820641/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值