{"id":2876,"date":"2025-03-18T18:57:21","date_gmt":"2025-03-18T18:57:21","guid":{"rendered":"https:\/\/w3buddy.com\/?p=2876"},"modified":"2026-01-15T13:10:33","modified_gmt":"2026-01-15T07:40:33","slug":"automate-oracle-dba-login-with-a-custom-alias-on-linux","status":"publish","type":"post","link":"https:\/\/w3buddy.com\/blog\/automate-oracle-dba-login-with-a-custom-alias-on-linux\/","title":{"rendered":"Automate Oracle DBA Login with a Custom Alias on Linux"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Managing Oracle databases on an Amazon Linux 2 EC2 instance often requires switching users and setting environment variables before running SQL*Plus. This guide will walk you through setting up a custom alias (<strong>s<\/strong>) that lets you instantly connect to Oracle as sysdba without manual switching. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>&#8211; You can also use this for all other Linux OS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 1: Log in as ec2-user<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, log in to your <strong>Amazon Linux 2<\/strong> EC2 instance using SSH:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">[ec2-user@ip-172-31-46-238 ~]$ uname -a\nLinux ip-172-31-46-238.ap-south-1.compute.internal 5.10.234-225.910.amzn2.x86_64 #1 SMP Fri Feb 14 16:52:40 UTC 2025 x86_64 x86_64 x86_64 GNU\/Linux\n[ec2-user@ip-172-31-46-238 ~]$\n[ec2-user@ip-172-31-46-238 ~]$ whoami\nec2-user\n[ec2-user@ip-172-31-46-238 ~]$<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 2: Grant Sudo Access to ec2-user<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To allow <strong>ec2-user<\/strong> to switch to oracle and run SQL*Plus without a password, run:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">[ec2-user@ip-172-31-46-238 ~]$ whoami\nec2-user\n[ec2-user@ip-172-31-46-238 ~]$ sudo visudo<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following line at the end:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">ec2-user ALL=(oracle) NOPASSWD: \/bin\/bash, \/u01\/app\/oracle\/product\/19c\/db_1\/bin\/sqlplus<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Before adding this line, verify your SQL*Plus version and correct path by running:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">[oracle@ip-172-31-46-238 ~]$ which sqlplus\n\/u01\/app\/oracle\/product\/19c\/db_1\/bin\/sqlplus<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If your SQL*Plus is located in a different directory, update the path accordingly in the sudoers file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Save and exit the editor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>For your reference:<\/strong> <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"107\" src=\"https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-1-1024x107.png\" alt=\"\" class=\"wp-image-2877\" srcset=\"https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-1-1024x107.png 1024w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-1-300x31.png 300w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-1-768x80.png 768w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-1-150x16.png 150w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-1.png 1254w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 3: Modify the .bashrc File for ec2-user<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To make the alias work every time you log in, edit the .bashrc file:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following alias at the end of the file:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">alias s=\"sudo -u oracle bash -c 'source \/home\/oracle\/.bash_profile; read -p \\\"Enter ORACLE_SID: \\\" ORACLE_SID; export ORACLE_SID; echo \\\"Connecting to ORACLE_SID=\\$ORACLE_SID...\\\"; sqlplus \/ as sysdba'\"<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"194\" src=\"https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-6-1024x194.png\" alt=\"\" class=\"wp-image-2886\" srcset=\"https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-6-1024x194.png 1024w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-6-300x57.png 300w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-6-768x146.png 768w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-6-1536x291.png 1536w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-6-2048x389.png 2048w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-6-150x28.png 150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Explanation:<\/strong> What this above command is doing exactly&#8230;<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>alias s=&#8221;&#8230;&#8221;<\/strong>\n<ul class=\"wp-block-list\">\n<li>Creates an alias named <strong>s<\/strong>, so when you type <strong>s<\/strong>, it executes the enclosed command.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>sudo -u oracle bash -c &#8216;&#8230;&#8217;<\/strong>\n<ul class=\"wp-block-list\">\n<li>Runs the command inside single quotes (<code>'...'<\/code>) as the <strong>oracle<\/strong> user.<\/li>\n\n\n\n<li><strong>-u oracle<\/strong> means switch to the<strong><code> <\/code>oracle<code> <\/code><\/strong>user.<\/li>\n\n\n\n<li><strong>bash -c &#8216;&#8230;&#8217;<\/strong> runs the given commands in a new bash shell.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>source \/home\/oracle\/.bash_profile;<\/strong>\n<ul class=\"wp-block-list\">\n<li>Loads the Oracle environment variables defined in <strong>.bash_profile<\/strong> for the <strong>oracle<\/strong> user.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>read -p \\&#8221;Enter ORACLE_SID: \\&#8221; ORACLE_SID;<\/strong>\n<ul class=\"wp-block-list\">\n<li>Prompts the user to enter the <strong>ORACLE_SID<\/strong> (Database System Identifier).<\/li>\n\n\n\n<li>Stores the user input in the variable <code>ORACLE_SID<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>export ORACLE_SID;<\/strong>\n<ul class=\"wp-block-list\">\n<li>Exports the entered <strong>ORACLE_SID<\/strong> so that SQL*Plus uses it.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>echo \\&#8221;Connecting to ORACLE_SID=\\$ORACLE_SID&#8230;\\&#8221;;<\/strong>\n<ul class=\"wp-block-list\">\n<li>Displays a message indicating which <strong>ORACLE_SID<\/strong> is being used.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>sqlplus \/ as sysdba<\/strong>\n<ul class=\"wp-block-list\">\n<li>Launches SQL*Plus in <strong>SYSDBA<\/strong> mode, allowing administrative database operations.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How It Works:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Every time you type <code>s<\/code> in the terminal:\n<ol class=\"wp-block-list\">\n<li>It switches to the <strong>oracle <\/strong>user.<\/li>\n\n\n\n<li>Loads Oracle environment variables.<\/li>\n\n\n\n<li>Asks for the <strong>ORACLE_SID<\/strong>.<\/li>\n\n\n\n<li>Exports it for the session.<\/li>\n\n\n\n<li>Displays which SID it&#8217;s connecting to.<\/li>\n\n\n\n<li>Opens <strong>SQL*Plus<\/strong> as <strong>sysdba<\/strong>.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This ensures that even if multiple databases are running, you manually select the correct one each time! \u2705<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 4: Apply the Changes<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After saving the file, apply the changes:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">source ~\/.bashrc<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 5: Test the New Alias<\/strong> from ec2-user<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now, simply run: <strong>s<\/strong> and see the magic&#8230;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>When database already up and running.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"386\" src=\"https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-4-1024x386.png\" alt=\"\" class=\"wp-image-2884\" srcset=\"https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-4-1024x386.png 1024w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-4-300x113.png 300w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-4-768x290.png 768w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-4-150x57.png 150w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-4.png 1076w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>When database is down then also it will ask for Oracle SID.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"334\" src=\"https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-5.png\" alt=\"\" class=\"wp-image-2885\" srcset=\"https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-5.png 1000w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-5-300x100.png 300w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-5-768x257.png 768w, https:\/\/w3buddy.com\/blog\/wp-content\/uploads\/2025\/03\/image-5-150x50.png 150w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now, with just a single command (<code>s<\/code>), you can quickly access SQL*Plus as <strong>sysdba <\/strong>without manually switching users or setting environment variables. This saves time and ensures consistency in database administration on Amazon Linux 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let me know if you need any further refinements!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing Oracle databases on an Amazon Linux 2 EC2 instance often requires switching users and setting environment variables before running SQL*Plus. This guide will walk you through setting up a custom alias (s) that lets you instantly connect to Oracle as sysdba without manual switching. Note&#8211; You can also use this for all other Linux [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2882,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowu461DA:productID":"","footnotes":""},"categories":[1225],"tags":[],"class_list":["post-2876","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/2876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/comments?post=2876"}],"version-history":[{"count":3,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/2876\/revisions"}],"predecessor-version":[{"id":2887,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/2876\/revisions\/2887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media\/2882"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=2876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=2876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/tags?post=2876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}