一、概述
本文將給大家介紹如何限制某個ip或某個ip段才能訪問Oracle數(shù)據(jù)庫
- 通過sqlnet.ora
- 通過/etc/hosts.deny和/etc/hosts.allow
- 通過iptables
二、正式實驗
本次實驗環(huán)境是centos6.10 + Oracle 11.2.0.4單實例,數(shù)據(jù)庫服務(wù)器ip地址為192.168.31.71
1. 通過sqlnet.ora
a. 關(guān)閉數(shù)據(jù)庫服務(wù)器上的防火墻,修改sqlnet.ora文件
該文件放在$ORACLE_HOME/network/admin下,如果沒有就在該目錄下創(chuàng)建一個即可 添加以下兩行
tcp.validnode_checking = yes
tcp.invited_nodes = (192.168.31.71, 192.168.31.77)
這里需要注意的是必須把本機(jī)ip地址加進(jìn)來(不能寫成localhost和127.0.0.1),否則監(jiān)聽啟動會報錯
b. 重啟監(jiān)聽,讓sqlnet.ora的修改生效
lsnrctl stop
lsnrctl start
設(shè)置之后就只有這兩個ip地址192.168.31.71, 192.168.31.77能訪問數(shù)據(jù)庫,其它ip地址訪問會報ORA-12547: TNS:lost contact錯誤
tcp.invited_nodes的意思是開通白名單,不在白名單中的一律拒絕訪問,它也可以寫成(192.168.31.*, 192.168.31.0/24)等方式,表明這個網(wǎng)段都能訪問
另外還有個參數(shù)tcp.excluded_nodes,表示黑名單,這里不做介紹,有興趣的可以自己去做做實驗。
2. 通過/etc/hosts.deny和/etc/hosts.allow
sqlnet.ora屬于數(shù)據(jù)庫層面的限制,但如果一個ip能夠使用root或者oracle,ssh到這臺數(shù)據(jù)庫服務(wù)器的話,那么它依然能夠訪問數(shù)據(jù)庫。為了避免這種情況,這時就需要通過/etc/hosts.allow和/etc/hosts.deny去限制某個ip或者ip段才能ssh訪問數(shù)據(jù)庫服務(wù)器
先刪除前面實驗添加的sqlnet.ora,然后重啟監(jiān)聽
lsnrctl stop
lsnrctl start
a. 修改/etc/hosts.deny
在文件尾部添加一行
all:all:deny
第一個all表示禁掉所有使用tcp_wrAppers庫的服務(wù),舉例來說就是ssh,telnet等服務(wù)
第二個all表示所有網(wǎng)段
b. 修改/etc/hosts.allow
在前面一步中我禁掉所有的網(wǎng)段,所以在這一步中要開通指定的網(wǎng)段
修改/etc/hosts.allow,在文件尾部添加
all:192.168.31.71:allow
all:192.168.31.47:allow
格式與hosts.deny一樣,第一行表示把本機(jī)放開,第二行表示給.47開通白名單
下面用我另外一臺機(jī)器(即不在allow中的)ssh或telnet連接71這個機(jī)器,就會出現(xiàn)如下報錯
[oracle@oracle19c1 ~]$ ssh 192.168.31.71
ssh_exchange_identification: read: Connection reset by peer
[oracle@oracle19c1 ~]$ telnet 192.168.31.71 22
Trying 192.168.31.71...
Connected to 192.168.31.71.
Escape character is '^]'.
Connection closed by foreign host.
連數(shù)據(jù)庫卻不受影響,因為數(shù)據(jù)庫服務(wù)不歸hosts.deny和hosts.allow管
[oracle@oracle19c1 ~]$ sqlplus sys/[email protected]:1521/orcltest as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Sun Aug 16 23:12:49 2020
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
其中ip地址也可以換成以下的寫法
通配符的形式 192.168.31.*表示192.168.31這個網(wǎng)段
網(wǎng)段/掩碼 192.168.31.0/255.255.255.0也表示192.168.31這個網(wǎng)段
3. 通過iptables
...
? 接下來內(nèi)容請訪問原文(https://www.modb.pro/db/29270?YYF)進(jìn)行查看~
更多數(shù)據(jù)庫相關(guān)內(nèi)容,可訪問墨天輪(https://www.modb.pro/?YYF)進(jìn)行瀏覽。






