How Do You Use the screen Program in Linux?

Problem scenario
You would rather not create duplicate terminal windows in your Linux desktop or with PuTTy in Windows.  This may be an enforced security policy rather than a preference.  Some Linux systems only allow one log on session.  Perhaps you want the systems administrator to use the "who" or "w" command to see which users are logged in without seeing more than one session for your user.

How do you use the screen command to have different terminals simultaneously with the requirements above?

How do you allow a continually-streaming program wall or echo messages to the console while you have a separate session without a second log on session?

Solution
Use the screen utility.  It is extremely helpful with long-duration processes to manage different screens from one session.

1.  Optional step.  Run this command:  screen -list

2.  Enter this command:  screen

3.  Run commands as you desire.  To exit, use these two commands:  ctrl-a, ctrl-d

(Holding control tap "a" then holding control again tap "d".)

4.  Run commands from the original prompt.  You can use "screen -list" to view the different sessions.  You can create more than one screen session.  They will not register as new logon sessions on the Linux server.  If you want to return to a previous session, use this command (but replace 1234.gooserver with the left-most column value of the output resulting from a "screen list" command):

screen -r 1234.goodserver

How Do You Trace the Route of a Network Path from One Server to an IP address elsewhere on the Network?

Problem scenario
You want to see the path that network traffic takes from one server to another network endpoint.  How do you find the intermediate path from the server your are on to a reachable IP address?

Solution
Use a tracing utility.  If your server is running Windows, use tracert.  Here is an example of how to use it:  tracert 8.8.8.8

If your server is running Linux, use traceroute.  Here is an example of how to use it: traceroute 8.8.8.8

For comprehending the details of TCP/IP connectivity and identifying latency (e.g., pinpointing slowness), tracing utilities such as these can be very effective.

The underlying mechanism of both of these tracing utilities is the destination is attempted to be contacted with a series of network packets.  The first packet in the series has a TTL (time-to-live) setting of 1.  The second in the series has a 2.  The third has a three, and the subsequent packets are successively incremented.  When the destination is finally reached, the tracing utility stops sending packets.  

If you do not know how TTL packets work, read the following.  A TTL setting of one will allow the packet to traverse one hop toward the destination IP address.  A TTL setting of two will allow the packet to make two hops toward the destination IP address.  A packet's TTL value is decremented with each time it traverses a device (such as a router, switch, firewall or computer).  That is why a packet with a TTL setting of 1 can only make one hop.  The physical length of the cables supporting the packet or the duration of the packet's life in terms of time have no affect on a packet's TTL value.  

The starting TTL setting (integer value) is the maximum number of hops from the server to another discrete device (e.g., a router, a switch, a firewall, a routing computer) that the network packet can pass through.  Therefore a TTL setting of one will allow the packet to make one hop to the nearest router or switch, then stop.  In a tracing utility the second packet in the series will make two hops toward the destination IP address based on routing tables.  If you do a traceroute or tracert for the nearest network device, the first packet will arrive at its destination and the program will end.

For more information, about tracert, see this Microsoft link.  For more information about traceroute, see this link.

How Do You Add Files to a New Project in GitLab?

Problem scenario
You want to add files to a new project in GitLab.  You want to do this from the command prompt (not through the web UI).   How do you do this?

Solution
This assumes you have a new project created; if you do not know how, see this posting.  If you want to import a project, see this posting.

Procedures
1.  Make sure you have the regular git installed on your server.  (Installing GitLab does not necessarily install git.) Run this command to test it:  git --version

(Install git if the command was not recognized.)

2.  From the backend of the Linux server, run these commands but read the notes to the right of them before you run them as the commands are just drafts:

git config --global user.name "Administrator"
git config --global user.email "admin@continualintegration.com"  
 # change to email of your choice

git clone git@x.x.x.x:root/contint.git  # change x.x.x.x to the IP address of the GitLab server and change contint to the name of the project you just created
cd contint # change contint to the name of the project you just created
touch README.md
touch foobar.txt
git add .
git commit -m "added two new files"
git push -u origin master

3.  You have now uploaded files to a project in GitLab.

How Do You Install MongoDB on a Debian/Ubuntu Linux Server?

Problem scenario
You want to install MongoDB on an Ubuntu/Debian Linux server.  How do you do this?

Solution
1.  If you know your distribution of Linux, go to step #2.  Otherwise try this command:  cat /etc/*-release

2.  Do one of the following that corresponds to your distribution of Linux:

On Debian:  sudo apt-get -y install mongodb mongodb-server

On Ubuntu 16.x or Ubuntu 17.x run these two commands (that may span three lines):

curl http://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb-org/3.6/multiverse/binary-amd64/mongodb-org-server_3.6.4_amd64.deb > /tmp/mongodb-org-server_3.6.4_amd64.deb
dpkg -i /tmp/mongodb-org-server_3.6.4_amd64.deb

3.  To test:  mongod -version


If you want to install MongoDB on a RedHat derivative of Linux, please see this posting.

How Do You Install Groovy on Centos 7.X?

Problem scenario
You want to install Groovy on CentOS 7.x but no yum repositories are configured for directly installing it.  What should you do?

Solution
Run these commands:

sudo yum -y install ant ant-junit antlr-tool apache-commons-cli apache-commons-logging apache-ivy bsf jansi jline junit objectweb-asm tomcat-jsp-2.2-api tomcat-servlet-3.0-api xstream

curl http://rpmfind.net/linux/centos/7.4.1708/os/x86_64/Packages/groovy-1.8.9-7.el7.noarch.rpm > /tmp/groovy-1.8.9-7.el7.noarch.rpm

sudo rpm -i /tmp/groovy-1.8.9-7.el7.noarch.rpm

How Do You Create a Dockerfile and Build a Docker Image?

Problem scenario
You know how to use Docker containers.  You know how to use Docker images if they are already built.  Now you want to use a Dockerfile to understand how to build images.  How do you use Dockerfile and how do you create your own image?

Solution

Prerequisites
This assumes you have installed Docker on your Linux server.  If you have not, click the URL corresponding with your distribution of Linux:

CentOS/RHEL/Fedora
Debian/Ubuntu
SUSE

Procedures
 1.  Use vi or a text editor.  Create a file called Dockerfile with the following content:

FROM ubuntu:latest
MAINTAINER NAME EMAIL

RUN apt-get -y update && apt-get -y upgrade && apt-get install -y build-essential

2.  Run this command to create a Docker image:  docker build -t "foobar:contint" .

Remember to substitute foobar and contint (in the above command) according to the following bullets:

-  foobar can be any arbitrary string as long as it is all lowercase.  This is the name of a repository of images.

-  contint is the "tag" name to idenify the image.  It have uppercase and lowercase letters.

3.  The Docker image has now been built.  (For your information the period in the docker build -t "foobar:contint" . command specifies the build context location.)

You are done. If you want to create a Docker container from this image just created, see this posting. When a docker build command runs, where is the image stored? For a pragmatic answer, use this command: docker images

For a more detailed answer, see this external page.

If you want more information, see this link.  For detailed reference information, see this link.  See this link for best practices.  To create a private registry of Docker images, see this posting.  If you want to purchase a book on Docker, click here.

How Do You Leverage Ansible to Deploy Salt Minion to an Ansible Managed Node?

Problem scenario
You have two configuration management servers: one is an Ansible control server and another is a Salt Master server.  You want Ansible to deploy Salt Minion to the managed nodes.  You want the managed nodes of Ansible to receive configurations from the Salt Master server.  This way one team can use SaltStack and another team can use Ansible.  How do you do this?

Solution

Prerequisites
This assumes you have deployed Salt Master and Ansible.  (If you are running Linux SUSE and need to install Ansible, see this posting.)  This assumes that a third server is configured to be a managed node and receives configurations from the Ansible control server with playbook runs.

This solution assumes you have the same username with sudoer privileges on each managed node.  For the example below we use "cooluser".  This username needs to be able to use sudo without being prompted for a password.  If you need assistance with this, see this posting.

Procedures
1.  Get a file that will be the template and source of the /etc/salt/minion file for the Salt Minion client servers.  Place this copy on the Ansible control server.  (If you want more information about this file, see "How do you install and configure Salt..." step #7 of this posting.)

2.  Place this file where you have other source files on the Ansible control server (and you can name it sminstaller.txt or whatever you want):

distro=$(cat /etc/*-release | grep NAME)

debflag=$(echo $distro | grep -i "ubuntu")
if [ -z "$debflag" ]
then   # If it is not Ubuntu, test if it is Debian.
  debflag=$(echo $distro | grep -i "debian")
  echo "determining Linux distribution..."
else
   echo "You have Ubuntu Linux!"
fi

rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then   #If it is not RedHat, see if it is CentOS or Fedora.
  rhflag=$(echo $distro | grep -i "centos")
  if [ -z "$rhflag" ]
    then    #If it is neither RedHat nor CentOS, see if it is Fedora.
    echo "It does not appear to be CentOS or RHEL..."
    rhflag=$(echo $distro | grep -i "fedora")
    fi
fi

if [ -z "$rhflag" ]
  then
  echo "...still determining Linux distribution..."
else
  echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
  yum -y install salt-minion   # install nc for initial testing only.
fi

if [ -z "$debflag" ]
then
  echo "...still determining Linux distribution..."
else
   echo "You are using either Ubuntu Linux or Debian Linux."
   apt-get -y install salt-minion
fi

suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
  if [ -z "$debflag" ]
  then
    if [ -z "$rhflag" ]
      then
      echo "*******************************************"
      echo "Could not determine the Linux distribution!"
      echo "Installation aborted. Nothing was done."
      echo "******************************************"
      exit
    fi
  fi
else
   zypper -n install salt-minion
fi

3.  On the Ansible control server write an Ansible playbook (e.g., a .yaml file) to transfer the files in step #1 and step #2. This playbook will execute the file in step #2 (above) on the managed node after it is transferred.  Create a .yaml file (e.g., contint.yaml) with the following lines (excluding the last "#**" lines):

- name: something
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
  - file:
      path: /etc/salt
      state: directory
      owner: cooluser
      mode: 0744

- name: Transfer the script file down.
  hosts: all
  tasks:
  - copy:
      src: /home/cooluser/sminstaller.txt # File in step #2; rename if whatever
      dest: /tmp/smin.sh
      owner: cooluser
      mode: 0644

- name: Install Salt Minion on any distribution of Linux.
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
     - name: Run the installer
       command: bash /tmp/smin.sh
     - name: Run the salt minion service for the first time.
       command: salt-minion
       async: 10
       poll: 0

- name: Transfer the minion file down.
  hosts: all
  tasks:
  - copy:
      src: /home/cooluser/minion.txt # File in step #1.
      dest: /etc/salt/minion
      owner: cooluser
      mode: 0644

#** Change the "cooluser" to a username that has sudoer privileges.
#** Change the src file names and directory paths above to match your configuration.

4.  Run the playbook with this command:  ansible-playbook contint.yaml

5.a.  Go to the Salt Master server to configure the new Salt Minions to work with the Salt Master server itself.  Run this command:  sudo salt-key -L

5.b.  Run this command but substitute "<nameOfSaltMinion>" with the hostname of the Salt Minion client based on the results of the above command:  sudo salt-key -a <nameOfSaltMinion>

How Do You Get Ansible Playbooks to Create Directories on Managed Nodes?

Problem scenario
You want Ansible to create directories on managed nodes.  How do you write a playbook to do this?

Solution
Prerequisites

This assumes that you have installed and configured Ansible.  If you do not know how to deploy Ansible, see this posting if you are using a Red Hat derivative.  If you are using Linux SUSE, see this posting.  If you need help running a playbook, see this posting.

Procedures
In the playbook (the .yaml file) for the section that creates the subdirectory, add these two stanzas where "cooluser" is a username with sudoer privileges on the managed node:

  remote_user: cooluser
  become: yes

Here is a complete playbook that creates a directory called "contint" in the /etc/ directory:

- name: something
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
  - file:
      path: /etc/contint
      state: directory
      owner: cooluser
      mode: 0744

How Do You Find the Size of an RPM Package before You Install It?

Problem scenario
You want to find out how much disk space you will consume before you install a given yum or RPM package.  You want to list available packages and their size based on a near match of a name.  What is the Linux (e.g., yum) command to find size of the package before you actually install it?

Solution

Prerequisites
This assumes that you have installed yum-utils.  If you need to install yum-utils, use this command: sudo yum -y install yum-utils

Procedures
Use this command but replace "openssl" with the name of the package you want to learn the more about):

sudo repoquery *openssl* --qf "%{name}:\n%{size:h}"

# We do not recommend changing the "h" to a "k" or "m" as the man page may suggest.

You may want to try this command too (but replace "openssl" with the name of the package you want):

yum info openssl

How Do You Install npm and Node.Js on Any Distribution of Linux?

Problem scenario
You have Linux servers of different distributions (including Debian/Ubuntu, Red Hat derivatives such as CentOS, RHEL, or Fedora, and Linux SUSE).  You want to install npm and Node.js on them.  What do you do?

Solution
Prerequisites
i.  You need a server with at least 1 GB of RAM.  To add virtual memory, see this posting.  To resize your GCP server, see this posting.  To upgrade your EC-2 instance (a VM running in AWS), see this posting.
ii.  Install the C compiler.  If you do not know how, see this posting.
iii. You have the make utility installed. If you need assistance, see this posting.

Procedures
1.  Create a file named "npminstaller.sh" (with no quotes) with the following content:

cd /opt/
curl http://nodejs.org/dist/v14.1.0/node-v14.1.0.tar.gz > node-v14.1.0.tar.gz
tar xzvf node-v14.1.0.tar.gz && cd node-v14*
./configure
make
make install

2.  Run the script with "sudo bash npminstaller.sh" with no quotes.  This script may take over one hour to run.