How Do You Find a Linux Log That Was Modified in the past 10 Minutes?

Problem scenario:  In Linux, you want to view log activity captured recently.  Looking through all the logs in /var/log can take a great deal of time.  How do you correlate a recent event by finding log files that were modified in the past 10 minutes?

Solution
Run this command:
find /var/log -mmin -10

This command will find files in /var/log and its subdirectories.  It will only find files that were modified within the past 10 minutes.  This should help you find the event in the logs to the extent was captured.

How Do You Find the User ID In Linux/Unix When You Only Have the Username?

Problem scenario: You know the Linux username that you want to use. But you do not know its corresponding UID or GID. How do you find the UID when you only know the username?

Solution
Use the id command. For example:

id jdoe

The results will give you the UID and GID.

How Do You Use C++ to Retrieve Data from a PostgreSQL Database on Linux CentOS?

Problem scenario
You have C++ and Postgres installed on a Linux CentOS server.  How do you write C++ programs to retrieve data from Posgres databases?

Solution
Install the official C++ API for Postgresql.  These directions should work on any RedHat distribution of Linux (e.g., CentOS or Fedora).

Run the following from the Linux command prompt:

sudo yum -y install postgresql-devel gcc-c++
sudo cd /usr/bin/
sudo curl http://pqxx.org/download/software/libpqxx/libpqxx-4.0.tar.gz > libpqxx-4.0.tar.gz
sudo tar xvfz libpqxx-4.0.tar.gz
sudo cd libpqxx-4.0
sudo ./configure
sudo make
sudo make install

To read more about the official API for C++ and PostgreSQL, see this GitHub link or the official pdxx.org site (http://pqxx.org/development/libpqxx/).

How Do You Use an .Ova File with Oracle VirtualBox?

Problem Scenario:  You downloaded an .ova file.  You want to create a virtual server (a clone) with this pre-configured OS.  How do you use Oracle VirtualBox to use this .ova file?

Solution
1.  Open Oracle VirtualBox
2.  Go to File -> Import Appliance (or press Ctrl-i)
3.  Select the OVA file
4.  Click "Next"
5.  Now you are ready to start the VM.

How Do You Install Nginx on a CentOS/RHEL/Fedora Server?

Problem scenario
You want to install Nginx on a CentOS/RHEL/Fedora Linux server.  What do you do?

Solution
1.  Create a file here: /etc/yum.repos.d/nginx.repo

2.  It should have these five non-blank lines:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

3.  Run these commands:

sudo yum -y update
sudo yum -y install nginx
sudo systemctl start nginx

How Do You Troubleshoot a C++ Program That Will Not Compile Due to Undefined Reference Errors?

Problem scenario: You try to compile a C++ program with gcc, but you get this output:

/tmp/ccj6eKDl.o: In function `main':
q.cpp:(.text+0x14): undefined reference to `std::cout'
q.cpp:(.text+0x19): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
q.cpp:(.text+0x28): undefined reference to `std::cout'
q.cpp:(.text+0x2d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccj6eKDl.o: In function `__static_initialization_and_destruction_0(int, int)':
q.cpp:(.text+0x55): undefined reference to `std::ios_base::Init::Init()'
q.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

What do you do?

Root cause:  gcc will not link the standard C++ libraries.

Solution

Use g++ instead of gcc.

How Do You Troubleshoot Installing Oracle VirtualBox Guest Additions on a CentOS Server?

Problem scenario
You are running Oracle VirtualBox on a Windows host.  You have Linux CentOS guest server.  Guest additions fails to install.  You tried to run this script VBoxLinuxAdditions.run.  The run fails with an error saying "
check /var/log/VBoxGuestAdditions.log for details."

You examined /var/log/VBoxGuestAdditions.log, and it said this:
"vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: Please check that you have gcc, make, the header files for your Linux kernel and possibly perl installed."

You have already installed gcc, make, Perl and updated the Linux kernel.  To investigate the kernel you use a "uname -r" command.  You also go to /usr/src/kernels/ and find a directory name that does not match the results of the "uname -r" command.  In /usr/src/kernels/ you execute the "ls" command and find this directory:  3.10.0-514.10.2.el7.centos.plus.i686

You then issue this command:  uname -r
You find this result:  3.10.0-327.el7.i686

Under normal circumstances, these strings match.  What do you do to install Oracle VirtualBox Guest Additions on a Linux CentOS server?

Solution
Reboot the CentOS guest.  After you run "yum update kernel*" the changes (assuming changes were made) will not take effect completely until you reboot.  Then install guest additions.  The errors should go away.

How Do You Use PowerShell to Search Every Folder for a File by the File’s Name?

Problem scenario:  You want to find a file on your Windows desktop by a specific name.  How do you search every folder to find it?

Solution
Use PowerShell.  Run these two commands to find "foobar.txt":

cd /
gci -recurse -filter "foobar.txt"

How Do You Permanently Change the Hostname of a Linux CentOS Server?

Problem scenario:  You edited the /etc/hosts file and the /etc/sysconfig/network file to have a new hostname. You used the "hostname newHostname" command (where newHostname is the desired new host name). You rebooted the server and the hostname is still localhost.localdomain. How do you configure the host name to be different?

Solution
None of the above steps were necessary for changing the hostname for the purposes of having the "hostname" command return a new hostname.  The above steps by themselves would not change the hostname that you see on the command prompt either.  To change the hostname for the "hostname" command to return the new hostname, and to have the character prompts reflect a new hostname, do this:

1.  Modify /etc/hostname to the desired new hostname.
2.  Save the file.
3.  Reboot for the hostname on the character prompts to reflect the new hostname.

How Do You Find the Network Interface Name of a CentOS 7.X Server That Will Connect to the Default Gateway of Your Network?

Problem scenario:  You want to find the name of the network interface that can connect to the default gateway on your Linux CentOS 7.x server.  Typical new deployments of CentOS 7.x using the minimal .ISO do not have the "route" utility nor the "ifconfig" utility installed.  How do you find the interface's name (e.g., eth0) without using the "route" command? 

Solutions

Solution #1 (preferred)  

The easiest solution is to use this command:  ip route | awk '{print $3}'

Solution #2

This solution avoids the "ip route".  This solution takes more time.  

a.  Run this command:  ​ip addr show
b.  Evaluate the results.  The results of the above command will have integer values followed by discrete interfaces.  For example you'll see "1: lo" and "2: ..." 

The first result, "1: lo", is a loopback interface.  The second result, "2: enp0s3", will be the interface you want (assuming there are only two interfaces).   The lo interface by default will never have the default gateway.  For a simple CentOS server the other interface will.  

For CentOS 7.3 servers installed from the current .ISO (as of March 17, 2017)  with the default settings, the name of the interface that can connect to the default gateway is "enp0s3" as shown above.  There may be multiple interfaces with multiple default gateways for servers in complex environments with multiple NICs.

Also note that the corresponding file for this interface name will have a name that is prefaced with "ifcfg-".  Therefore in /etc/sysconfig/network-scripts/ the file that governs the interface will be "ifcfg-enp0s3".