Oracle 密码文件

Oracle 密码文件

一、密码文件

oracle的口令文件的作用是存放所有以sysdba或sysoper权限连接数据库用户的口令,如果想以sysdba或sysoper权限远程连接数据库,必须使用口令文件,否则不能连接,由于sys用户在连接数据库时必须以sysdba or sysoper方式,也就是说sys用户要想远程连接数据库必须使用口令文件,因此可以认为在数据库中存放sys用户的口令其实没有任何意义,因为你本机其实还是可以登陆的!

使用口令文件的好处是即使数据库不处于open状态,依然可以通过口令文件验证来连接数据库。开始安装完oracle,没有给普通用户授予sysdba或sysoper权限,口令文件中只存放了sys与system的口令,如果之后把sysdba或sysoper权限授予了普通用户,那么此时会把普通用户的口令从数据库中读到口令文件中保存下来,当然这时必须要求数据库处于open状态。

二、Oracle的两种认证方式;

  1. 使用与操作系统集成的身份验证
  2. 使用Oracle数据库的密码文件进行身份认证

三、密码文件的位置

Linux下的存放位置:
$ORACLE_HOME/dbs/orapw$ORACLE_SID
即:ORACLE_HOME/dbs/orapw<sid>

Windows下的存放位置:$ORACLE_HOME/database/PW%ORACLE_SID%.ora

密码文件查找的顺序 —>orapw<sid>—>orapw—>Failure

两种认证方式:
决定在两个参数中

  1. remote_login_passwordfile = none | exclusive |shared 位于$ORACLE_HOME/dbs/spfile$ORACLE_SID.ora参数文件中,通过show parameter remote_login_passwordfile查看
    none : 不使用密码文件认证
    exclusive :使用密码文件认证,自己独占使用(默认值)
    shared :使用密码文件认证,不同实例dba用户可以共享密码文件(如集群)

  2. $ORACLE_HOME/network/admin/sqlnet.ora文件中
    SQLNET.AUTHENTICATION_SERVICES = none | all | ntf(windows)
    none : 表示关闭操作系统认证,只能密码文件认证
    all : 用于linux或unix平台,关闭本机密码文件认证采用操作系统认证,但远程<非本机>可以使用密码文件认证
    nts : 用于windows平台

如果上面两个参数都选择none,那么sys用户无论是本机还是远程均不可登陆,但是非sys用户可登陆

四、演示

查看remote_login_passwordfile参数

SQL> show parameter remote_login_passwordfile;

NAME                            TYPE        VALUE
------------------------------- ----------- -----------------------------
remote_login_passwordfile       string      EXCLUSIVE                 ##使用密码文件认证(默认)

设置sqlnet.ora文件中的SQLNET.AUTHENTICATION_SERVICES参数为none

[oracle@oracle admin]$ cat sqlnet.ora | grep -i auth
SQLNET.AUTHENTICATION_SERVICES = none

使用本地操作系统认证,失败

[oracle@oracle admin]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Tue May 8 21:49:22 2018

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

ERROR:
ORA-01031: insufficient privileges


Enter user-name:

使用密码文件认证,成功

[oracle@oracle admin]$ sqlplus  sys/Oracle_1@oracle as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Tue May 8 21:50:39 2018

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

设置sqlnet.ora文件中的SQLNET.AUTHENTICATION_SERVICES参数为all

[oracle@oracle admin]$ cat sqlnet.ora | grep -i auth
SQLNET.AUTHENTICATION_SERVICES = all

使用本地操作系统用户认证,成功

[oracle@oracle admin]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Tue May 8 21:53:01 2018

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

使用密码文件认证,失败

[oracle@oracle admin]$ sqlplus  sys/Oracle_1@oracle as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Tue May 8 21:53:28 2018

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

ERROR:
ORA-12641: Authentication service failed to initialize


Enter user-name:

但是远程使用密码文件认证将会成功

[oracle@mysql-master-1003306 admin]$ sqlplus  sys/Oracle_1@orcl1 as sysdba              ##此处为远程连接

SQL*Plus: Release 11.2.0.1.0 Production on Wed May 9 07:46:40 2018

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

使用#符号将新增的SQLNET.AUTHENTICATION_SERVICES行注释掉恢复到缺省值,使用缺省值时系统认证及密码文件认证都可用

五、密码文件的建立:orapwd

[oracle@oracle dbs]$ orapwd
Usage: orapwd file=<fname> entries=<users> force=<y/n> ignorecase=<y/n> nosysdba=<y/n>  

where    
file - name of password file (required),    
password - password for SYS will be prompted if not specified at command line,    
entries - maximum number of distinct DBA (optional),    
force - whether to overwrite existing file (optional),    
ignorecase - passwords are case-insensitive (optional),    
nosysdba - whether to shut out the SYSDBA logon (optional Database Vault only).     

There must be no spaces around the equal-to (=) character.

##
file:密码文件的名字,默认为orapw<sid>
password:sys用户的密码,这里仅仅是sys的密码
entries:密码问价那种可存放的最大文件数
force:默认为n,y表示允许覆盖
ignorecase:忽略大小写
nosysdba:是否关闭sysdba登录

重建或者修改sys密码文件

[oracle@oracle dbs]$ orapwd file='/oracle/11g/product/11.2.0/dbhome_1/dbs/orapworcl' password='Oracle' entries=5

OPW-00005: File with same name exists - please delete or rename
[oracle@oracle dbs]$
[oracle@oracle dbs]$ orapwd file='/oracle/11g/product/11.2.0/dbhome_1/dbs/orapworcl' password='Oracle' entries=5 force=y

##将sys用户密码修改为Oracle,也可以用于重建密码文件,密码文件的名字严格按照orapw<sid>来命名

演示将entries 改为5,然后将多个用户设置为sysdba或sysoper

[oracle@oracle dbs]$ orapwd file='/oracle/11g/product/11.2.0/dbhome_1/dbs/orapworcl' password='Oracle' entries=5 force=y

给多个用户赋予sysdba角色

SQL> grant sysdba to test6;
grant sysdba to test7;
grant sysdba to test8;
grant sysdba to test9;
grant sysdba to test10;
Grant succeeded.

SQL>
Grant succeeded.

SQL>
Grant succeeded.

SQL> grant sysdba to test9
*
ERROR at line 1:
ORA-01996: GRANT failed: password file
'/oracle/11g/product/11.2.0/dbhome_1/dbs/orapworcl' is full


SQL>
grant sysdba to test10
*
ERROR at line 1:
ORA-01996: GRANT failed: password file
'/oracle/11g/product/11.2.0/dbhome_1/dbs/orapworcl' is full

会出现错误: ORA-01996: GRANT failed: password file ‘’ is full

再次查看orapworcl发现多出几行,即当设置的时候多出了个用户

[oracle@oracle dbs]$ strings orapworcl
]\[Z
ORACLE Remote Password file
INTERNAL
AB27B53EDC5FEF41
8A8F025737A9097A
TEST1
22F2E341BF4B8764
TEST2
2E3197EF1322CB1D
TEST3
9728B9C4700744F0
TEST4
D981A32D021D0F35
TEST5
76489E4C44B14CDF
TEST6
0AE1AAE2C08CD499
TEST7
BCD14F62F7BE3195
TEST8
B3F1A240058CED9B

注意不要轻易删掉密码文件,以免将其他账户的信息也删除

六、密码文件内容修改的几种方式

1、使用orapwd建立,修改密码文件,不建议使用
2、使用alter user sys identified by <>
3、使用grant sysdba to <> 或grant sysoper to <> 或 revoke sysdba |sysoper from <>

七、查看密码文件内容

[oracle@oracle dbs]$ strings orapworcl
]\[Z
ORACLE Remote Password file
INTERNAL
AB27B53EDC5FEF41
8A8F025737A9097A

当sys密码不记得可以使用OS系统身份认证登陆到sqlplus,再使用alter user修改密码

SQL> alter user sys identified by "Oracle_1";

User altered.

再次查看密码文件与上一次对比,已经发生变化

[oracle@oracle dbs]$ strings orapworcl
]\[Z
ORACLE Remote Password file
INTERNAL
ABE3DF9F8DA2D18F
72D6FA01DEAD0BB6

通过授予权限来修改密码,密码文件中多出了scott的信息

SQL> grant sysdba to test1;
Grant succeeded.
SQL> ho strings orapworcl
    ]/[Z
    ORACLE Remote Password file
    INTERNAL
    AB27B53EDC5FEF41
    8A8F025737A9097A
    TEST1
    22F2E341BF4B8764

注意此处使用test1登陆后,显示的账户信息还是sys而不是test1,但此时的test1已经具备了sys权限

[oracle@oracle admin]$ sqlplus test1/test1@oracle as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Tue May 8 22:54:01 2018

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> show user;
USER is "SYS"

八、sysdba与sysoper的区别

SQL> select * from system_privilege_map where name like '%SYS%';

PRIVILEGE NAME                            PROPERTY
---------- ---------------------------------------- ----------
     -3 ALTER SYSTEM                           0
     -4 AUDIT SYSTEM                           0
       -83 SYSDBA                              0
       -84 SYSOPER                             0

两者的schema不同

SQL> show user             ##sysdba
USER is "SYS"

SQL> conn / as sysoper
Connected.

SQL> show user             ##sysoper
USER is "PUBLIC"

查看密码文件视图,可以得到哪些用户为sysdba,哪些用户为sysoper

SQL> select * from v$pwfile_users;

USERNAME               SYSDB SYSOP SYSAS
------------------------------ ----- ----- -----
SYS                    TRUE  TRUE  FALSE
TEST1                  TRUE  FALSE FALSE
TEST2                  TRUE  FALSE FALSE
TEST3                  TRUE  FALSE FALSE
TEST4                  TRUE  FALSE FALSE
TEST5                  TRUE  FALSE FALSE
TEST6                  TRUE  FALSE FALSE
TEST7                  TRUE  FALSE FALSE
TEST8                  TRUE  FALSE FALSE

9 rows selected.

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 289211569@qq.com