JOE Tutorial for Linux

JOE Tutorial for Linux

Start joe:

joe ./textFile.txt

Show help:

Ctrl + K + H (to hide help, enter this command again)

Save file

Ctrl + K + D

Save and exit:

Ctrl + K + X

Exit without saving:

Ctrl + K + Q

Go to previous/next screen:

Ctrl + U/Ctrl + V

Move cursor to the start/end of file file:

Ctrl + K + U/Ctrl + K + V

UnDo recent change:

Ctrl + Shift + _

ReDo undone change:

Ctrl + Shift + ^

Insert or overwrite:

Ctrl + T

Selecting text:

Ctrl + K + B (block begin)
Ctrl + K + K (block end)

When you have block selected, to move block using:

Ctrl + K + M

Delete block:

Ctrl + K + Y

Copy block:

Ctrl + K + C

Search in the file:

Ctrl + K + F

Then choose between ignore (I), replace (R) options. To navigate to next result use:

Ctrl + L

If you want to change joe’s default setting use:

Ctrl + T

You can turn on/off auto-indent, word wrap, line numbers, highlighting and set tab width, left margin, etc.

Linux: File System Hierarchy

Linux: File System Hierarchy

In this tutorial is described Filesystem Hierarchy Standard (FHS), which specifies required directories. The root directory is “/” and it should contain only the subdirectories.

/bin

  • Contains binaries which can be executed from the command line (ls, grep, mkdir…)
  • Programs that can be used by users (system, admin, normal users)
  • It can be in single-user mode

/boot

  • Contains everything required for the boot process
  • Kernel
  • Grant Unified Boot-loader
  • LILO (LInux LOader)

/sbin

  • Binaries
  • Program used by system and admin
  • Normal users can use programs in /bin if they are allowed
  • Usually, normal users do not have this directory in $PATH variable

/dev

  • Files of all devices
  • Created during installation operating system.
  • Create new devices: /dev/MAKEDEV
File Description
Sda First SCSI drive on the SCSI/SATA bus
md0 First group of meta discs (RAID)
ttyS0 First serial port
lp0 First parallel printer
null bin for bits
random Deterministic random bits
urandom Non-deterministic random bits

/etc

File Description
passwd Users information
fstab Partition and storage mounting information
rc or rc.d or rcX.d Run commands – commands that runs when OS starts

/home

  • The home directory for users
  • All data and system settings of users
  • Can be divided into groups (school, office, financial)

/root

  • Home directory for user root
  • Normal users don’t have permissions.

/lib

  • Libraries for programs
  • /lib/modules: kernel modules, network controls, file system control

/tmp

  • Temporary files
  • Used by running programs

/mnt

  • Mounting temporary file systems
  • File systems from /etc/fstab are mounted during start OS
  • Network file systems
  • /media: DVD, USB

/usr

  • Programs, libraries installed from OS distribution
  • Accessible for everyone
  • /usr/
Directory Description
local Software installed by admin on local device
X11R6 Files of Windows OS
bin Almost all commands for users
sbin Usually server’s programs
include Header files for C language
lib Stable libraries
  • /usr/share/
Directory Description
X11 Files of Windows OS
dict Glossary
man Manual pages
doc Documentation
info Information files
src source files

/var
Contains files that are changed when OS is running.

Subdirectory Description
log Logging files
run Run-time variable data
spool Program using queue (mails, printers)
mail Mailbox
local variable data from /usr/local
lib Holds dynamic data libraries/files
lock Lock files. Indicates that resource (database, file) is in use and should not be accessed by another process.

/opt
Third-party software

/proc

  • Created by OS kernel
  • Information about system
  • Stored only in RAM
  • Does not use any disc space
  • Every process has a subdirectory (by PID)
Subdirectory Description
/PID/status Stats about process
/PID/cmdline How was the process started and what input arguments
/PID/maps Region of contiguous virtual memory in a process or thread
/PID/environ Environment of process
  • Interesting files
File Description
cpuinfo Information about CPU
meminfo Usage of memory
version Kernel version
cmdline Kernel’s parameters from the boot loader
devices List of drivers for the kernel
interrupts Which interrupts are used and how many times
ioports list of currently registered port regions used for input or output communication.
dma ISA Direct Memory Access channel
kcore Image of physical system memory
cmdline Kernel’s parameters from the boot loader
cmdline Kernel’s parameters from the boot loader

/lost+found

  • Recovered or damaged data after a crash.
  • Each partition has its own /last+found directory.

Generate SSL certificates using openssl

Generate SSL certificates using openssl with a Certificate Signing Request

The file ca.key and ca.crt are the Certificate Authority

We will be generating the .key and .csr (Certificate Signing Request) files from the below command.

[root@node01 ssl]# openssl req -new -sha256 -newkey rsa:2048 -nodes -keyout linuxcent.com.key -days 365 -out linuxcent.com.csr -sha256 -subj "/C=IN/ST=TG/L=My Location/O=Company Ltd./OU=IT/CN=linuxcent.com/subjectAltName=DNS.1=linuxcent.com"

The resultant files are a PEM certificate request .csr and a Private .key file. Now that we have successfully generated the .csr, we approach a Certificate Authority, Upload our CSR, and purchase the signer certificates along with Intermediate Chain keys for a given Number of days, typically done for 365 Days.

The -days flag is optional, and can be skipped as we are only generating a Signing Request.

Here we can use the openssl command to verify the .csr file that is generated as shown below:

[root@node01 ssl]# openssl req -in linuxcent.com.csr -noout -text
Certificate Request:
Data:
Version: 0 (0x0)
Subject: C=IN, ST=TG, L=MY Location, O=Company Ltd., OU=IT, CN=linuxcent.com/subjectAltName=DNS.1=linuxcent.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:00:e4:b4:24:d7:22:ec:5d:c1:37:8c:d1:a0:62:17:
96:24:77:8d:75:4e:d5:74:15:4d:61:e0:8b:66:d6:
                Exponent: 65537 (0x10001)
        Attributes:
            a0:00
    Signature Algorithm: sha256WithRSAEncryption
         87:ef:83:b2:a6:f5:3a:f3:6f:1c:e4:02:ec:bf:5d:75:64:1d:

- OUTPUT TRUNCATED --

In the next section we shall see How the .csr can be signed by a CA to generate a .crt PEM certificate

Signing a .csr with a Certificate Authority [Demo Purpose] – Sample CA files

Here is the process of Generating a Selfsigned certificate(Not to be used on public facing sites)

Now we will using the root ca.key and ca.crt to digitally sign this .csr and generate a .crt PEM certificate

x509 is a Certificate Data Management and Certificate Signing Utility

This generally takes the private key as input, and signs the certificate requests and Converting the certificate to various formats

[root@node01 ssl]# openssl x509 -req -in linuxcent.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out linuxcent.com.crt -days 365 -sha256

-subj "/C=IN/ST=TG/L=My Location/O=Company Ltd./OU=IT/CN=linuxcent.com/subjectAltName=DNS.1=linuxcent.com"

We have generated the .crt file from the .csr

[root@node01 ssl]# ls linuxcent.com.crt linuxcent.com.key 
linuxcent.com.crt linuxcent.com.key

We have successfully generated the linuxcent.com.key file and linuxcent.com.crt, and digitally self signed with the root CA key and certificates.

Generating Self Signed SSL certificates using openssl x509

The x509 is the certificate signing utility we will be using here.\ to generate a PEM certificate

Below is the complete command to generate the ssl self signed certificate.

openssl req -x509 -days 365 -sha1 -newkey rsa:2048 -nodes -keyout linuxcent.com.key -out linuxcent.com.crt -sha256 -subj "/C=IN/ST=State/L=My Location/O=Company Ltd./OU=IT/CN=linuxcent.com/subjectAltName=DNS.1=linuxcent.com"

The Days parameter can be specified to any number of days depending on your requirement

The Self signed certificates are mostly commonly used within the internal network or among small group of familiar individuals like an office for specific purposes and not advised to be used out in the public domain as the browser does not identify the certificate authenticity or the ingenuity of the concerned website. The Self-signed certificates are not validated with any third party until and unless you import them to the browsers previously.

Generating a Wildcard certificate Request.

[root@node01 ssl]# openssl req -new -sha256 -newkey rsa:2048 -nodes -keyout linuxcent.com.key -out linuxcent.com.csr -sha256 -subj "/C=IN/ST=TG/L=My Location/O=Company Ltd./OU=IT/CN=linuxcent.com/subjectAltName=DNS.1=*.linuxcent.com, linuxcent.com"

Mentioning the Alternate Domain as *.linuxcent.com will create a wildcard .CSR

The SANs often seen in shortform to SubjectAltName allows us to secure multiple subdomains using a SSL certificate

The CA signing process of the wildcard is identical and obviously costs more.

Control Structure: Bash If then Else

The Bash being a scripting language does tend offer the conditional if else, We shall look at them in the following sections.

Firstly there needs to be a conditional check that has to be performed in order for the corresponding Block of code to be executed.

To break down the semantics of conditional control structures in BASH we need to understand The conditional keyword that performs the validation, the It is represented most commonly as “[“ and very rarely represented as “test” keyword.

It can be better understood by the following demonstration:

vamshi@linux-pc:~/Linux> [ 1 -gt 2 ]
vamshi@linux-pc:~/Linux> echo $?
1
vamshi@linux-pc:~/Linux>
vamshi@linux-pc:~/Linux> [ 1 -lt 2 ]
vamshi@linux-pc:~/Linux> echo $?
0

The [ is synonymous to the command test on the linux kernel.

vamshi.santhapuri@linux-pc:~/Linux> test 1 -gt 2

vamshi.santhapuri@linux-pc:~/Linux> echo $?
1
vamshi.santhapuri@linux-pc:~/Linux> test 1 -lt 2
vamshi.santhapuri@linux-pc:~/Linux> echo $?
0

We Shall now look at the different variations of Conditional controls structures.

  1. if then..fi

    if [ Condition ] ; then
    
    statement1...statementN
    
    fi
  2. if then..else..fi

    if [ Condition ] ; then
    
        If Block statements
    
    ...
    
    else
        else-Block statement
    
    fi
  3. if..then..elif then..elifN then..fi

    if [ Condition ] ; then
    
        If Block statement1
    
    ...
    
    elif [ elif Condition ]; then   # 1st elif Condition
    
        elif Block statement1
    
    
    elif [ elif Condition ]; then    # 2nd elif Condition
    
        elif Block statements
    
    elif [ elif Condition ]; then    # nth elif Condition
    
        elif Block statements
    
    fi

    An else can also be appended accordingly when all the if and elif conditions fail, which we will see in this section .

     

  4. if..then..elif then..elifN then..else..fi

    The “if elif elif else fi” control structure is like multiple test checking control diversion strategy in bash, gives the user the power to write as many test conditions as possible until a test condition is matched leading in the resultant block of code being executed. Writing this multiple elif can be tedious task and the switch case is mostly preferred

    if [ Condition ] ; then
    
        If Block statement
    
    elif [ elif Condition ]; then   # 1st elif Condition
    
        elif Block statement1
    
    elif [ elif Condition ]; then    # nth elif Condition
    
        elif Block statement
    
    ...
    
    else Block statementN # else block while gets control when none of if or elif are true.
    
        else Block statements
    
    fi

    Atleast one of the block statements are executed in this control flow similar to a switch case. The else block here takes the default case when none of the if nor the elif conditions matches up.

  5. Nested if then..fi Control structure Blocks

    Adding to the if..elif..else there is also the nested if block wherein the nested conditions are validated which can be Demonstrated as follows:

    if [ condition ]; then
    
        Main If Block Statements
    
        if [ condition ]; then # 1st inner if condition
    
            1st Inner If-Block statements
    
            if [ condition ]; then # 2nd inner if condition
    
                2nd Inner If-Block statements
              
                if [ condition ]; then 
                    Nth Inner If Block statements 
    
                fi
    
            fi
    
        fi
    
    fi

    This logic of nested ifs are used while dealing with scenarios where the outermost block of statements must be validated before, if the test succeeds then the control flow is passed to the innermost if test statement execution. Thus the name Nested if.

 

Here is the switch case bash script with practical explanation.
We will look at the Exit codes within the BASH in the next sections.

Rename files in linux

The linux mv command has very featureset, It can be used to rename the file(s) and Directory names, also also used to relocate the contents and help better in organizing the files and directories on a linux OS.

Syntax of mv command:

$ mv [OPTIONS] </path/to/Source> </path/to/Destination>

How to rename a single file

The rename operation is linux is done using the mv command

[vamshi@linuxcent mv]$ ls
demo.txt
$ mv demo-today.txt demo-old.txt
[vamshi@linuxcent mv]$ ls
demo-old.txt

Here the file demo-today.txt has been renamed to demo-old.txt

How to move or relocate multiple files and directories at once into a Destination Directory

Out DemoProject Directory contains the following content

[vamshi@node02 DemoProject]$ ls
api LICENSE mvnw mvnw.cmd README.md

We are only interested to move out only selected directories core/ site/ admin/ and the file pom.xml to the target destination /tmp/Demo-test/, We can achieve this using the option -t --target-directory= Option

[vamshi@node02 DemoProject]$ mv -vi core/ site/ admin/ pom.xml -t /tmp/Demo-test/
‘core/’ -> ‘/tmp/Demo-test/core’
‘site/’ -> ‘/tmp/Demo-test/site’
‘admin/’ -> ‘/tmp/Demo-test/admin’
‘pom.xml’ -> ‘/tmp/Demo-test/pom.xml’

As a result we have successfully moved the selected content:

[vamshi@linuxcent DemoProject]$ ls /tmp/Demo-test/
admin core pom.xml site

Renaming multiple files with extensions

Here’s what we will be demonstrating in this tutorial, We will use a combination of tools like cut combining them with a for loop to accomplish our task in an iterative loop.

For simplicity sake let’s consider we have 10 files ending with .txt extension, as seen below

[vamshi@linuxcent ~]$ ls
file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt

We will now rename them and append an extension of .txt to all the files as demonstrated below:

[vamshi@node02 source]$ for i in *.txt; do sh -c "mv $i `echo $i| cut -d'.' -f1 `.html" ; done
[vamshi@linuxceent ~]$ ls
file10.html file1.html file2.html file3.html file4.html file5.html file6.html file7.html file8.html file9.html

Using the rename command to rename the file extensions.

The linux rename command takes the arguments

We have here 10 files with .html extension

[vamshi@linuxcent ~]$ rename .html .doc *
[vamshi@linuxcent ~]$ ls
file10.doc file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc

While we also might have many other files in another extension format and we can change thrir extension format in the following method.
Suppose have 3 files with .txt extension as file11.txt file12.txt file13.txt and remaining files with .doc extension, they all can be renamed to .html as per the following format.

[vamshi@linuxcent source]$ ls
file10.doc file11.txt file12.txt file13.txt file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc
[vamshi@linuxcent ~]$ ls
file10.doc file11.txt file12.txt file13.txt file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc
[vamshi@linuxcent ~]$ rename .doc .txt .html *
[vamshi@node02 source]$ ls
file10.html file11.html file12.html file13.html file1.html file2.html file3.html file4.html file5.html file6.html file7.html file8.html file9.html

Setting hostname in Linux

In the systemd Environment there has been an architectural change and the systemd daemon controlling all the essential processes

root 1 0 0 Apr15 ? 00:00:12 /usr/lib/systemd/systemd --switched-root --system --deserialize 32

We can make use of the hostnamectl command to immediately reflect the system hostname change.


Command to get the current hostname on Linux:

[vamshi@server02 ~]$ sudo hostnamectl 
   Static hostname: server02.linuxcent.com
   Pretty hostname: SERVER02
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2338f55840d640689fc5ac4b356b160c
           Boot ID: 418256281d2f4e11822809dde7c1b09e
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1062.18.1.el7.x86_64
      Architecture: x86-64

As you can see the current hostname is set to SERVER02

The file /etc/hostname also used to have the same effect but on the cloud systems this is dynamically generated and doesn’t hold true on cloud and tends to get overwritten after reboot.
On the general DataCenter or PC environment we can use the static files to populate the hostnames and make them permanent:

$ cat /etc/sysconfig/network
HOSTNAME=node2.linuxcent.com

But only /etc/hostname file is given importance since the systemd version and updating this ensures persistence.

$ cat /etc/hostname 
node02.linuxcent.com

 

Process to set or change the hostname in Linux Commandline:

The systemd provides a sophisticated command hostnamectl to set the hostname, It will also update the static file /etc/hostname and ensure the changes are permanent across reboots.

$ sudo hostnamectl set-hostname node02.Linuxcent

To get the hostname along with FQDN

[vamshi@server02 ~]$ hostname --fqdn
node02.linuxcent.com
[vamshi@node02 ~]$ sudo hostnamectl status 
   Static hostname: node02.linuxcent.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2338f55840d640689fc5ac4b356b160c
           Boot ID: 33619e39ea4c4900bd848e13d2a6239e
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1062.18.1.el7.x86_64
      Architecture: x86-64

Changing hostname is ubuntu and Debian systems can be done through hostnamectl command

vamshi@debian:~$ hostnamectl 
Static hostname: debian
Icon name: computer-vm
Chassis: vm
Machine ID: b4adcdb84c724856b577524ebbfa0003
Boot ID: 67e1bf27946a4770b8e939f2420d06fd
Virtualization: oracle
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-5-amd64
Architecture: x86-64