How Do You Add a Linux User to the sudoers Users?

Problem scenario
You want a given Linux user to be added to the sudoers group.  You want the user to be able to use the "sudo " command before running other commands.  How do you give the sudo privilege to a given user?

Solution
If you are using Debian or Ubuntu Linux, do this (but replace "cooluser" with the username of your choice):
sudo adduser cooluser sudo

If you are using CentOS/RHEL/Fedora, do this (but replace "cooluser" with the username of your choice):

Modify the /etc/sudoers file.
Find this stanza:
root    ALL=(ALL)    ALL

Add a stanza above it like this:

cooluser ALL=(ALL)    ALL

How Do You Troubleshoot the Vagrant Error “default: Waiting for SSH to become available.”?

Problem scenario
You run "vagrant up" and you see things stop at this stage:  "==> default: Waiting for SSH to become available..."

What should you do?

Possible Solutions
1.  Run the command again with " --debug" at the end.  This provides verbose output.  

2.  You may want to review your Vagrantfile settings.  This page helps with such a review (i.e., if you do not know what to look for).

3.  Verify that the Security Group your AWS instance is trying to connect to allows for inbound connections from the external IP address of your server.  Even if your server is an AWS EC2 instance, the Security Group must allow for inbound connections for "vagrant up" to work.  To modify the security group, find the EC-2 server's external IP address.  Run this command  curl ipinfo.io  It will show you the external IP address if it has access to the internet.

How Do You Run a Groovy Program on a Red Hat Derivative Linux Server?

Problem scenario
You want to use a Java Virtual Machine language on a CentOS/RHEL/Fedora server.  You want to test out the language too.  What should you do?

Prerequisites
Install Groovy; this command should work (assuming your server is configured to work with a yum repository with groovy):  sudo yum -y install groovy
# If your yum repositories do not have Groovy, see this posting.

Procedures
1.  Create a file called hello.groovy with the following five lines:

class Example {
   static void main(String[] args) {
      println('Hello World');
   }
}

2.  Run it like this:  groovy hello.groovy

How Do You Test a Remote Oracle Database Connection (with Credentials You Were Given) from a Linux Command Prompt?

Problem scenario
You have no desktop GUI and a limited ability to install new applications other than the Java compiler.  You are not be allowed to install a SQL command line client such as sqlconnect or a utility such as tnsping.  What should you do to test the credentials, server name and port number to ensure they work to allow access to an Oracle database?

Solution
The overview is to use a Java program with the code provided below.

Prerequisite
You need to be able to compile Java programs.  The "javac" command must be recognized.  To do this install a Java compiler.  If you do have this and do not know how, see this posting.

Procedures
1.  Find the jre/lib/ext directory path on your server.  (e.g., sudo find / -name ext -type d | grep lib). 

2.  Place an ojdbc14.jar file (e.g., downloaded from Oracle.com or some trustworthy website) in that directory above. Here is one place to get it.

3.  From any directory create a program such as this called OracleCon.java:

    import java.sql.*;  //first line of file
    class OracleCon{  
    public static void main(String args[]){  
    try{  
    //step1 load the driver class  
    Class.forName("oracle.jdbc.driver.OracleDriver");  

    //step2 create  the connection object  
    Connection con=DriverManager.getConnection(  
    "jdbc:oracle:thin:@contintserver:1521:xe","jdoe","strongPassword");  

    //step3 create the statement object  
    Statement stmt=con.createStatement();  

    //step4 execute query  
    ResultSet rs=stmt.executeQuery("select * from all_users");  
    while(rs.next())  
    System.out.println(rs.getString(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  

    //step5 close the connection object  
    con.close();  

    }catch(Exception e){ System.out.println(e);}  

    }  
    }  // last line of file

(This code was largely taken from https://www.javatpoint.com/example-to-connect-to-the-oracle-database.)

In the above code, you need to make some modifications:

  • Replace "jdoe" with the Oracle SQL user, and replace "strongPassword" with jdoe's password. 
  • Replace "contintserver" with the servername of the remote Oracle server.
  • Replace "xe" with the SID.  You may need to ask the DBA for this SID as it can be crucial. 
  • Under the comment "//step4 execute query" there is a SELECT statement in quotes. You can change this SQL command, but it often works as a test.

4.  To compile it, use javac OracleCon.java

5.  To run the compiled byte code, run java OracleCon without quotes.  The output of running this Java program should give you an indication of whether or not the credentials work to connect to the database.

As a closing tip, we have seen a source (an article, http://glennpaulley.ca/conestoga/2015/06/configuring-sonarqube-with-oracle-12c/, published in June of 2015) indicated that the two forward slashes "//" are a necessary constructor for a JDBC connection string.  We know in 2018 that this is not necessary in all instances.

How Do You Troubleshoot “java.lang.AbstractMethodError: Method oracle/jdbc/driver/OraclePrepreadStatementWrapper.setBinaryStream (IL java/io/InputStream;)V is abstract”?

Problem scenario
You get an error such as this on a Linux server:

"Caused by: java.lang.AbstractMethodError: Method oracle/jdbc/driver/OraclePrepreadStatementWrapper.setBinaryStream (IL java/io/InputStream;)V is abstract at oracle.jdbc.driver.OraclePreparedStatementWrapper.setBinaryStream (OraclePreparedStatementWrapper.java)"

What should you do?

Possible Solution #1
If you are doing Java coding, have you imported the packages needed for the line of Java code that is throwing this error?

Possible Solution #2
If you are using JBoss, an Atlassian tool, SonarQube, or some web application written in Java, try to reinstall Java on that web application server.  If you need help with this, see this posting after Java has been removed.

Possible Solution #3
Was the database upgraded recently?  If there is a database involved, you may need a newer .jar file for a driver to connect to the database.

How Do You Install kubectl on Any Type of Linux?

Problem scenario
You want a quick, generic way to install kubectl on any type of Linux.  What should you do?

Solution
Run these commands:

cd /tmp

curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl

sudo mv kubectl /usr/local/bin/

Test it by running this command: kubectl version

(If you would prefer to use apt commands, because you are using a Debian/Ubuntu distribution of Linux, to install kubectl, see this posting.)

How Do You Troubleshoot This Salt Minion Message “SaltReqTimeoutError”?

Problem scenario
You run "sudo salt-minion -l debug" on a Salt Minion.  You see this message:  "[INFO    ] SaltReqTimeoutError: after 60 seconds. (Try 4 of 7)"

What should you do?

Solution
Go to the Salt Master server.  Run this command:  sudo systemctl start salt-master

How Do You Install SonarQube on a Red Hat Distribution of Linux?

Problem scenario
You have a Red Hat distribution of Linux (e.g., CentOS/RHEL/Fedora).  How do you install SonarQube?

Solution

Prerequisites
Install the Java Development Kit so you can later take advantage of plugins.  If you need assistance with this, see this link.

Procedures
For future reference, at some point the "9.6"s you see below will have to be incremented to a newer version of PostgreSQL. 

1.  If you are running RHEL, run this command:  sudo yum -y update

If you are using CentOS, run these two commands:
sudo yum -y install epel-release
sudo yum -y update

2.  If you are running RHEL, run this command:
sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm

If you are running CentOS, run this command:
sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

3.  Run these commands:
sudo yum -y install postgresql96-server postgresql96-contrib
sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb

4.a.  Enable MD5-based authentication by editing the /var/lib/pgsql/9.6/data/pg_hba.conf file.

4.b.  Run this command:  sudo vi /var/lib/pgsql/9.6/data/pg_hba.conf

4.c.  Find the following stanzas; change the word "peer" to "trust" and change the word "ident" to "md5".  Here is how it may appear before you make these changes:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident

Here is what it will look like after the changes:
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

5.  Run these two commands:

sudo systemctl start postgresql-9.6
sudo systemctl enable postgresql-9.6

6.  Change the postgres user's password:  sudo passwd postgres

7.  Assume the postgres user with this command:  su - postgres

8.  Create a user named "sonar" with this command:  createuser sonar

9.   Enter the PostgreSQL command prompt by running this command:  psql

10.  Prepare a command such as this, but replace "goodPassword" with the password you want for the sonar PostgreSQL database user:

ALTER USER sonar WITH ENCRYPTED password 'goodPassword';

11.  Run the prepared command above (after you replaced goodPassword with the password of your choice).

12.  Run this command from the SQL command prompt:  CREATE DATABASE sonar OWNER sonar;

13.  Leave the psql prompt by running this command:  \q

14.  Exit the postgres user to be a user that is a sudoer.  Run this:  exit

15.  Run a script with these five lines (using sudo privileges when you run it):

version=7.0
curl -L https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-$version.zip > /tmp/sonarqube-$version.zip
yum -y install unzip
unzip /tmp/sonarqube-$version.zip -d /opt
mv /opt/sonarqube-$version /opt/sonarqube

# Call the file above "sonar.sh" and put it in /tmp.  Run it with "sudo bash /tmp/sonar.sh"

16.  Modify the SonarQube configuration file.  To enter it, run this:  sudo vi /opt/sonarqube/conf/sonar.properties

Find the following two stanzas:

#sonar.jdbc.username=
#sonar.jdbc.password=

"Uncomment and provide the PostgreSQL username and password of the database that we have created earlier. It should look like:" (Taken from Vultr.com.)

sonar.jdbc.username=sonar
sonar.jdbc.password=goodPassword

17.  While in the same file, find this stanza (it may be on the 39th line):

#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

Uncomment the line, and save the file.  (Also taken from Vultr.com.)

18.a  Create this file: /etc/systemd/system/sonar.service

18.b.  The contents should be the following (with the first line being "[Unit]" and last line being "WantedBy = multi-user.target"):

[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking

ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop

User=sonar
Group=sonargroup
Restart=always

[Install]
WantedBy = multi-user.target

19.a. Run these commands:

sudo useradd sonar
sudo groupadd sonargroup
sudo passwd sonar
  # respond with a new password
sudo usermod -a -G sonargroup sonar

 Run a command like this but replace sonar with the user that will start the sonar service and replace sonargroup with the user's group:

sudo chown -R sonar:sonargroup /opt/sonarqube

# If you do not know how to find the user's group, log in as the user and run the command "groups" to list the user's membership(s) of any group(s).

19.b.  Run these three commands:
sudo systemctl start sonar
sudo systemctl enable sonar
sudo systemctl status sonar

20.  Configure the firewall to allow connections.  If you are in a special environment, you can disable your OS firewall with this command:

sudo systemctl stop firewalld   # Normally you would configure a special exception in the firewall.
sudo setenforce 0

21.  Run this command to install Apache web server: sudo yum -y install httpd

22.a  Run this command, but replace x.x.x.x with the external IP address of your server (or the URL that you will present with this server):  

sudo vi /etc/httpd/conf.d/sonar.x.x.x.x.conf

22.b.  Place these lines in the file (where the top and bottom lines have "VirtualHost" in them):

<VirtualHost *:80>
    ServerName sonar.x.x.x.x
    ServerAdmin admin@continualintegration.com
    ProxyPreserveHost On
    ProxyPass / http://localhost:9000/
    ProxyPassReverse / http://localhost:9000/
    TransferLog /var/log/httpd/sonar.x.x.x.x_access.log
    ErrorLog /var/log/httpd/sonar.x.x.x.x_error.log
</VirtualHost>

22.c.  Replace x.x.x.x with the IP address or domain name of your server.

22.d.  Replace "admin@continualintegration.com" with an email address of yours.

23.  Run these two commands:

sudo systemctl start httpd
sudo systemctl enable httpd

24.  Open a web browser.  Go to the external IP address of the server.  You should see the web UI for SonarQube.

How Is the Password Set for a Challenge for a Password from a “git clone git@…” command?

Problem scenario
A "git clone git@..." command prompts the user for a password.  What is the password or how is it set?

Solution
The authentication for such a git command is usually done by the individual servers not through Git itself.  You should go to the server with the source repository, and run this to change the password to something you know:

sudo passwd git

Alternatively you could change the owner and group of the .git file or the permissions of the git file. Then you could set up SSH with a user on the server that has the .git repo that has permissions to access the file.  To set up SSH see this posting.

Why Do Linux Commands Not Work when They Work as Part of a Python Program with the subprocess.check_output Invocation?

Problem scenario
You are trying to debug a Python program that uses shell (or Bash) commands.  The shell or Bash commands work when they are called with Python's subprocess.check_output.  They do not work when called directly from the Linux command prompt.  What is wrong?

Possible solutions
Normally I.T. professionals can get the Linux (Bash or shell) commands to work without as many moving parts.  In some instances the Python script that executes the Bash commands seem to help the reliability of the commands.  Sometimes you inherit code that you must maintain.  In these instances you need to reverse engineer the code.  These solutions below were made for the scenario where the Python program works, but when the individual Linux command is run separately without Python, the Linux command fails by itself.

1.  Does the Python code have backslashes "\"?  These are used as "escape" characters so the parsing of the Bash commands will be done properly.  Some quotation marks are needed in Bash commands.  The Python interpreter may not know if it should parse the punctuation itself or save it for the Bash command.  It may be necessary to have these escape characters in the Python program.  However these may cause the Bash commands to fail when run outside the context of a Python program (e.g., if they are lifted verbatim and run interactively for testing purposes).

2.  Are there environment variables that are involved?  It could be that manually running the Bash commands reveals that your user context is different from the Python invocation of such commands.  As a debugging tool, compare the output of an "env" command run interactively to the same command run from the Python program.  Some utilities in Linux may need their full subdirectory path to be called to work via Python subprocess.check_output invocations.

3.  Does the Python code do some sort of authentication process?  When trying to manually troubleshoot Linux commands used in a Python program, see if there are previous lines of code in the Python program that log into a system.  Do not get ahead of yourself or make assumptions.  Some Linux commands may only work if the sequence is exact.