How Can You Recursively Search Subdirectories in Linux For a File That Has a Specific Text Pattern?

Problem scenario:  How do you use the grep command to recursively search most subdirectories (excluding some directories) for a pattern?  You want to find a file with a string like this:

/var/lib/pgsql/data

You are not interested in the files in directory paths that have one or more parent (or any antecedent) directories named "proc" nor "sys".  You want to list files that have the above pattern.  You want the results that match the string above in a case-insensitive way.  What do you do?

Solution
Grepping for patterns with forward slashes don't require quotes around the patterns when using the grep command.  Searching for lower case patterns with the "-i" flag will return files that match the string's pattern in lower case or in upper case or in any combination thereof.  Knowing that you want to exclude any file that resides in a directory path having a directory with the name "proc" or "sys" can make the search quicker.  The "--exclude-dir=" flag allows you to exclude results with "proc" or "sys" directories.  These exclude-dir flags will exclude any directory path with "proc" or "sys" -- not just the root "proc" and "sys" directories.

Follow these steps:
1.  cd /
2.  grep -ir /var/lib/pgsql/data . --exclude-dir=proc --exclude-dir=sys

To clarify the step #2 command, it will return files with a string matching the pattern /var/lib/pgsql/data except those files that are in a path with a directory named "proc" or "sys".

How Do You Share Files Between an Oracle VirtualBox Linux Guest and a Windows Host without Using vboxsf?

Problem scenario:  You have Oracle VirtualBox running on a Windows 7 computer as the host.  You have a Linux CentOS guest supported by Oracle VirtualBox.  You want to have a file share that does not rely on vboxsf.  What do you do?

Solution
1.  On the Windows host desktop, create a folder.  For this example, we'll use "foobar" for the name.
2.  Right click this new folder,  go to "Properties" -> "Sharing" -> "Advanced Sharing..."
3.  Check the box for "Share this folder" then click "OK"
4.  Open a command prompt and run "ipconfig" to know the IP address.
5.  Go to the Linux guest, install the Samba client (as root, run this command: yum -y install samba-client).  
6.  Run this command:

smbclient //192.168.1.1/foobar -U jdoe

# This assumes that "foobar" is the name of the folder created in step #1.
# This assumes 192.168.1.1 is the IP address found in step #4.
# This assumes jdoe is a username of the Windows host.  
# Replace foobar, 192.168.1.1 and jdoe accordingly.

7.  Enter the password for the jdoe user.
8.  Now files you create in the folder in step #1 will be viewable in your Linux guest. 
9.  You are done.  This "step" simply provides basic usage tips of the smb prompt.  At the smb: prompt you can create directories that will appear on the Windows host.  You can use get foo.txt to retrieve from the Windows host a file named foo.txt (assuming it is in the folder that was created in step #1).  You can use put contint.txt to transfer a file named "contint.txt" from the Linux guest (assuming it exists in the directory you issued the command in step #6) to the Windows host (specifically to the folder that was created in step #1).

How Do You Give Internet Access to a Linux Guest of Oracle VirtualBox Running on a Windows Host?

Problem scenario:
You created a new Linux CentOS 7.x guest server using Oracle VirtualBox on a Windows host.  Your Windows machine has Internet access.  From the Linux Guest you try to ping a 8.8.8.8 but you get this message "connect: Network is unreachable."

How do you give the Linux guest Internet access?

Possible Solution #1
1.  Using the GUI of the Oracle VirtualBox guest server, go to Devices -> Network Settings.  If you see the drop down for "Attached to:" as "Internal Network" change it to "NAT."  If you just installed Oracle VirtualBox, this step (#1) is probably unnecessary.
2.  Back up this file: /etc/sysconfig/network-scripts/ifcfg-enp0s3
3.  Modify this file "/etc/sysconfig/network-scripts/ifcfg-enp0s3" so the "ONBOOT=no" stanza to says "yes."  For example this is the desired stanza:

ONBOOT=yes

4.  Once the file above has ONBOOT=yes, run this command:  service network restart
5.  Now your Linux guest should have Internet access.

Possible Solution #2
1. In Oracle VirtualBox's GUI for the VM, make sure the network setting for "Attached to:" is "NAT".
2. Run a command like this but replace "43" with the octet of your choice:
sudo ifconfig enp0s3 10.0.2.43 255.255.255.0

3. Run a command like this: sudo ip route add default via 10.0.2.2
4. Optional step: You may now use PuTTy to connect the guest machines if you use port forwarding. You can arbitrarily map IP addresses and ports to different IP addresses and ports on the guest VM. See this posting "How Do You Use Putty To Connect To A CentOS Server (A Guest VM running on Oracle VirtualBox on a Windows Host)?" for more information.

How Do You Install and Configure Postgresql on a Linux CentOS 7 Server?

Problem scenario:  You have a Linux Centos 7.3 server.  You want to get Postgresql to work on it.  What do you do?

Solution
To get Postgresql working on a CentOS 7.3 server, do the following four commands as the root user:

yum -y install postgresql-server postgresql-contrib

postgresql-setup initdb

systemctl start postgresql

systemctl enable postgresql

#  To issue SQL commands, run these two commands:

sudo -i -u postgres

psql

#  To exit the Postgres command prompt, type this followed by enter:

\q

How Do You Find The Default Gateway of Your Linux Server?

Problem scenarios:  Either you are using CentOS 7.x (and ifconfig is even not a command) or you are using ifconfig and different flags (like -v for verbose), but you cannot find the default gateway.  How do you know if you have a default gateway even configured on a Linux server?

Solution  The "ifconfig" command cannot show the default gateway.  To find the default gateway on your Linux server, there are three different commands:

​ip route
netstat -rn 
route -n

The "ip route" command will show the result by displaying "default via x.x.x.x..."  The x.x.x.x is the IP address of the default gateway.

How Do You Troubleshoot Chef’s Knife Command When You Get An Error About “Failed to read the private key”?

Problem scenario:  You are using knife commands and getting errors.  For example, you try to run this: knife client list
As a result, you get this error:

"WARN: Failed to read the private key /root/.chef/continual.pem: #<Errno::ENOENT: No such file or directory @ rb_sysopen - /root/.chef/continual.pem>
ERROR: Your private key could not be loaded from /root/.chef/continual.pem
Check your configuration file and ensure that your private key is readable"

What should you do?

Solution
Obtain a copy of the .pem file from the Chef server. Then place a copy on the Chef client.  Specifically place a copy in /root/.chef/ with the name continual.pem (assuming you were root when you ran the "knife client list" command).

If you were not root, make sure the user running the knife command has access to the copy of the .pem file.  The client.rb file should be configured to find the copy of the .pem file.  Custom locations are possible with the client.rb file.  As long as the .pem file is where the client.rb file is configured, you should not get errors unless there is a permissions problem.

How Do You Troubleshoot The Linux Error “/sbin/mount.vboxsf: mounting failed with the error: Protocol error”?

Problem scenario:  You are trying to share files between your Windows desktop host of Oracle VirtualBox and a Linux guest virtual server.  You run this command:

mount -t vboxsf contint /mnt/windows-share

but you get this error:

"/sbin/mount.vboxsf: mounting failed with the error: Protocol error"

Solution
For the example command above that reproduces the error, the "contint" needs to match the "Name" value in what was configured in the Oracle VirtualBox Devices -> Shared Folders -> Shared Folder Settings window.

Here is where you look to properly construct the mount command:

there is an option in the "Name" column for the argument that is "contint" as an example in this command:

mount -t vboxsf contint /mnt/windows-share

Once the spelling for the argument in the mount command is correct (as seen in the above example), the command should work with no "Protocol error."  To create an option in the folders list, follow step #1b in these directions (closely or loosely depending on your preference).

How To Install CentOS Linux 7

Question:  How do you install CentOS Linux 7?

Solution
1.  Create a bootable .iso. 
2.  Insert the bootable media and boot to it.  If the media is a USB stick, boot the server to it.  For some BIOS you may need to press F12 during the splash screen after turning on the server/workstation.  When you in the boot menu, choose the device that has the bootable media.
3.  Choose the option at the top "Install CentOS Linux 7"
4.  Choose English and click "Continue" in the lower right hand corner.
5.  Click icons that have the triangle with an exclamation point near it.  It will probably just be the "Installation Destination."  In the "Local standard disks" section click the hard drive you want to install to.  Then click "Done" in the upper left hand corner.
6.  Click "Begin Installation"
7.  When it is finished, click the "Reboot" button the lower right hand corner.

How Do You Determine the Repo ID When For The “reposync” Command?

Problem scenario:  Synchronizing yum repositories has a number of benefits.  In a secure environment, CentOS/RedHat/Fedora servers have no access to the Internet.  There are numerous valuable yum repos on the Internet.  Your network administrator may grant you temporary access to such an Internet repository.  You may go to the directory on a server on your network that you want to mirror a copy or house a non-updating copy of the repo locally. 

You want to use the reposync command.  You know that "reposync -r continualintegration" (where "continualintegration" is the repo ID) will synchronize a repository with a source of an Internet repo with the destination being the server you run this command from.  You run "reposync -r continualintegration" and nothing happens.  There is no error, but the directory you were in when you ran the command did not get any files.  You want to ensure that the repo ID itself is valid.  How do you determine the if repo ID itself is valid for the reposync command? 

Solution:  Go to /etc/yum.repos.d/.  In this directory look at content of the *.repo files.  In these files there may a string encapsulated in square brackets like this:  [continualintegration]

Repo IDs are found in *.repo files in /etc/yum.repos.d/ on a CentOS/RedHat/Fedora server.  The repo ID itself will have no square brackets but appear within square brackets [likethis].  In this "continualintegration" example the repository (or repo) ID is "continualintegration" with no quotes.  So to use reposync with "continualintegration," you would enter the command like this:  "reposync -r continualintegration" with no quotes.  Case sensitivity of the repo ID matters when you use the reposync command.  Look at the .repo files carefully for the word or words in brackets "[likeThis]."  For the repo ID you were trying, If there is no string matching in the *.repo files in the /etc/yum.repos.d/ directory, then you do not have a valid repo ID.