环境配置:Oracle10gR2,Windows XP
Oracle的用户信息一般来说是保存在数据字典里的,所以常规用户在Oracle数据库没有启动的时候是无法登陆的。但有两类用户例外,这就是具有sysdba或者sysoper权限的用户。Oracle sysdba或者sysoper用户的登陆有两种方式:一是通过OS认证,二是通过密码文件验证。
究竟使用哪一种验证方式以及能否成功登陆取决于三个方面的因素:
1. sqlnet.ora中SQLNET.AUTHENTICATION_SERVICES的设置
2. 参数文件中REMOTE_LOGIN_PASSWORDFILE的设置
3. 密码文件 PWD%sid%.ora
Oracle进行权限验证的大致顺序如下:
1. 根据SQLNET.AUTHENTICATION_SERVICES的值决定是进行os验证还是密码文件验证。
2. 如果是os验证,根据当前用户的用户组判断是否具有sysdba权限。如果os验证失败,则进行密码文件验证。
2. 如果是密码文件验证,REMOTE_LOGIN_PASSWORDFILE的值以及密码文件是否存在决定了验证是否成功。
1. OS 验 证
要启用os验证,就必须在qlnet.ora中设置SQLNET.AUTHENTICATION_SERVICES=(NTS),然后在Windows中建立ora_dba用户组,把相关用户加入到这个组中(e.g., administrator),这样administrator就可以在不用提供用户名和密码(或者提供任意的用户名和密码)的情况下以sysdba身份本地登陆。因为操作系统已经代替Oracle进行了验证。
测试一:ora_dba用户本地登陆
C:\>sqlplus / as sysdba
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the OLAP and Data Mining options
SQL>
C:\>sqlplus wrong_user/wrong_password as sysdba
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the OLAP and Data Mining options
SQL>
C:\>sqlplus / as sysdba
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the OLAP and Data Mining options
SQL>
C:\>sqlplus wrong_user/wrong_password as sysdba
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the OLAP and Data Mining options
SQL>
测试二:非ora_dba用户本地登陆
C:\>sqlplus / as sysdba
ERROR:
ORA-01031: insufficient privileges
Enter user-name:
C:\>sqlplus wrong_user/wrong_password sysdba
ERROR:
ORA-01017: invalid username/password; logon denied
Enter user-name:
C:\>sqlplus sys/change_on_install as sysdba
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the OLAP and Data Mining options
SQL>
C:\>sqlplus / as sysdba
ERROR:
ORA-01031: insufficient privileges
Enter user-name:
C:\>sqlplus wrong_user/wrong_password sysdba
ERROR:
ORA-01017: invalid username/password; logon denied
Enter user-name:
C:\>sqlplus sys/change_on_install as sysdba
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the OLAP and Data Mining options
SQL>
2. 密码文件验证
密码文件包含了被授予sysdba和sysoper权限的用户的用户名和密码。这是一个加密文件,一般来说存放在%oracle_home%/database目录下,文件名为PWD%sid%.ora。
如果要使用密码文件验证,则把sqlnet.ora改为SQLNET.AUTHENTICATION_SERVICES=none,或者从sqlnet.ora中删除SQLNET.AUTHENTICATION_SERVICES。同上匿名登陆sqlplus会失败,给出用户名和密码就可以成功登陆。
C:\>sqlplus / as sysdba
ERROR:
ORA-01031: insufficient privileges
Enter user-name:
C:\>sqlplus sys/change_on_install as sysdba
Connected to an idle instance.
idle>
C:\>sqlplus / as sysdba
ERROR:
ORA-01031: insufficient privileges
Enter user-name:
C:\>sqlplus sys/change_on_install as sysdba
Connected to an idle instance.
idle>
测试一:删除密码文件。使用用户名和密码登陆,失败!
C:\>sqlplus sys/change_on_install as sysdba
ERROR:
ORA-01031: insufficient privileges
Enter user-name:
C:\>sqlplus sys/change_on_install as sysdba
ERROR:
ORA-01031: insufficient privileges
Enter user-name:
测试二:恢复密码文件,设置REMOTE_LOGIN_PASSWORDFILE=none。使用用户名和密码登陆,失败!
SQL> alter system set remote_login_passwordfile=none scope=spfile;
System altered.
C:\>sqlplus sys/change_on_install as sysdba
ERROR:
ORA-01017: invalid username/password; logon denied
Enter user-name:
SQL> alter system set remote_login_passwordfile=none scope=spfile;
System altered.
C:\>sqlplus sys/change_on_install as sysdba
ERROR:
ORA-01017: invalid username/password; logon denied
Enter user-name:
测试三:恢复密码文件,设置REMOTE_LOGIN_PASSWORDFILE为EXCLUSIVE或者SHARED。使用用户名和密码登陆,成功!
SQL> alter system set remote_login_passwordfile=exclusive scope=spfile;
System altered.
C:\>sqlplus sys/change_on_install as sysdba
Connected to an idle instance.
SQL>
SQL> alter system set remote_login_passwordfile=exclusive scope=spfile;
System altered.
C:\>sqlplus sys/change_on_install as sysdba
Connected to an idle instance.
SQL>
3. 密码文件
查看具有sysdba或者sysoper权限的用户:
SQL> select * from v$pwfile_users;
USERNAME SYSDB SYSOP
------------------------------ ------- -------
SYS TRUE TRUE
SQL> select * from v$pwfile_users;
USERNAME SYSDB SYSOP
------------------------------ ------- -------
SYS TRUE TRUE
每次使用grant sysdba/sysoper授予新用户特殊权限或是alter user命令修改拥有sysdba/sysoper权限的用户密码的时候,Oracle都会自动的同步密码文件,这样保证在数据库没有打开的情况拥有特殊权限的用户能正常的登陆数据库以进行管理操作。
SQL> grant sysdba to logicgate;
Grant succeeded.
SQL> select * from v$pwfile_users;
USERNAME SYSDB SYSOP
------------------------------- -------- --------
SYS TRUE TRUE
LOGICGATE TRUE FALSE
SQL> grant sysdba to logicgate;
Grant succeeded.
SQL> select * from v$pwfile_users;
USERNAME SYSDB SYSOP
------------------------------- -------- --------
SYS TRUE TRUE
LOGICGATE TRUE FALSE
使用orapwd命令可以重建密码文件。
C:\>orapwd
Usage: orapwd file=<fname> password=<password> entries=<users> force=<y/n>
where
file - name of password file (mandatory),
password - password for SYS (mandatory),
entries - maximum number of distinct DBA (optional),
force - whether to overwrite existing file (optional)
C:\>orapwd
Usage: orapwd file=<fname> password=<password> entries=<users> force=<y/n>
where
file - name of password file (mandatory),
password - password for SYS (mandatory),
entries - maximum number of distinct DBA (optional),
force - whether to overwrite existing file (optional)
其中文件名和密码是必需的。entries设置了密码文件可包含的dba用户的最大数目。force定义了是否覆盖当前文件。重建密码文件会清除系统内除了sys用户以外所有sysdba用户的密码。必须使用grant sysdba同步密码文件。
C:\>orapwd file=%oracle_home%\database\PWDepcit.ora password=temp entries=20 force=y;
C:\>sqlplus sys/temp as sysdba
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the OLAP and Data Mining options
SQL> select * from v$pwfile_users;
USERNAME SYSDB SYSOP
------------------------------- -------- --------
SYS TRUE TRUE
C:\>orapwd file=%oracle_home%\database\PWDepcit.ora password=temp entries=20 force=y;
C:\>sqlplus sys/temp as sysdba
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the OLAP and Data Mining options
SQL> select * from v$pwfile_users;
USERNAME SYSDB SYSOP
------------------------------- -------- --------
SYS TRUE TRUE