How to execute sqlplus by root using sudo in linux? Scenario:

*oracle xe11g r2 is installed( and runs properly when logged in as oracle).
*logged in as user, root
*added root to groups oinstall and dba as follows:

#useradd -G oinstall root
#useradd -G dba root


*environment variables ORACLE_HOME and ORACLE_SID are set as follows:

#export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
#export ORACLE_SID=XE
#export PATH=$ORACLE_HOME/bin:$PATH


and executed:

#sudo -u oracle sqlplus / as sysdba


Output:

sudo: sqlplus: command not found


Please guide me in running sqlplus by root using sudo.
Not an Oracle error.

Sure you can put the above variables in .bash_profile of oracle, but when you use sudo like above, that will not be executed.

[root@o71 ~]# tail -3 /home/oracle/.bash_profile
export ORACLE_HOME=/u01/app/oracle/product/dbhome_1
export ORACLE_SID=ETALON
export PATH=$ORACLE_HOME/bin:$PATH
[root@o71 ~]# sudo -u oracle sqlplus / as sysdba
sudo: sqlplus: command not found


One of the many ways to work around this:

[root@o71 ~]# sudo -u oracle bash -c '. ~/.bash_profile; sqlplus / as sysdba'

SQL*Plus: Release 18.0.0.0.0 - Production on Sun Oct 28 10:52:43 2018
Version 18.4.0.0.0

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


Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0

SQL&gt; 


I am not a fan of using .bash_profile for setting the Oracle environment, I suggest using oraenv or a custom script that is not executed automatically.