Unix / Linux

Learning Resources

Overview

File Systems

Unix Architecture

Console Commands

Linux Info

# Get the Linux version.
lsb_release -da

# Get the hostname that Linux is running on.
hostname

# Get the IP address of the host Linux is running on.
hostname -I

# Get the FQDN (Fully Qualified Domain Name) of the host Linux is running on.
hostname -f

Create a New File

touch /path/to/my/new/file.txt 

Copy files between hosts

The scp command allows files to be copied to, from, or between different hosts. It uses ssh for data transfer and provides the same authentication and same level of security as SSH.

# Copy a local directory to a remote host.
# scp -r <local_dir_path> <user>@<remote_host>:<remote_dir_path>
scp -r /local/path/to/original/dir/ myname@myremotedomain.com:/remote/path/to/copied/dir/
# Copy a file from a remote host to the Local host.
# scp <user>@<remote_host>:<remote_file_path> <local_dir>
scp -r myname@myremotedomain.com:/remote/path/to/original/file.txt /local/path/to/put/copied/file

Delete files and directories

The unlink command will remove only a single file, while rm can remove multiple files at once.

To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:

rm -rf dirname

Remove all non-hidden files and sub-directories (along with all of their contents) from a directory:

rm -r /path/to/directory/*

Rename and move files and directories

Linux uses the mv (move) command to rename or move files and directories.

Rename a file.

mv old_file_name new_file_name

Rename a directory.

mv old_folder_name new_folder_name

mv can also can move several files into a destination folder, or rename a single file.

mv file_name destination_directory

Rename will retain the attributes and the creation date of the original file/folder.

grep

grep [string to search for] [filename]

Flag Example Description
-i grep -i "REact" index.js Search case-insensitively.
-c $ grep -c "react" index.js Find the number of lines with matches.

Security

Traps

Traps for Linux is an application created by Palo Alto Networks which stops threats on endpoints and coordinates enforcement with network and cloud security to prevent successful cyberattacks. Traps replaces legacy antivirus with a multi-method prevention approach that blocks malware and exploits, both known and unknown, before they compromise endpoints, such as laptops, desktops and servers. The Traps agent protects Linux servers by preventing attackers from leveraging software exploits or vulnerabilities to compromise an endpoint. The Traps agent enforces organization security policies as defined in the ESM Console.


Display Internet connections active on your Linux box.
netstat -natp


/usr/local/bin is for programs that a normal user may run.


Aptitude is the package manager for Debian linux.
To get into the Aptitude UI simply type aptitude.


MAN Pages
– https://unix.stackexchange.com/questions/3586/what-do-the-numbers-in-a-man-page-mean


Unix and Linux (history, similarity, differences)

  • https://www.softwaretestinghelp.com/unix-vs-linux/
  • https://opensource.com/article/18/5/differences-between-linux-and-unix
  • https://www.cyberciti.biz/faq/what-is-the-difference-between-linux-and-unix/

Cron Jobs

  • https://www.ostechnix.com/a-beginners-guide-to-cron-jobs/
  • https://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs–net-8800
  • https://stackoverflow.com/questions/22358382/execute-php-script-in-cron-job
  • https://www.a2hosting.com/kb/developer-corner/php/run-php-scripts-from-cron-jobs
  • https://www.adminschoice.com/crontab-quick-reference
  • https://wiki.debian.org/cron
  • https://linuxconfig.org/linux-crontab-reference-guide
  • https://debian-administration.org/article/56/Command_scheduling_with_cron
  • https://opensource.com/article/17/11/how-use-cron-linux
  • https://www.geeksforgeeks.org/crontab-in-linux-with-examples/

cron functionality

cron is a Unix/solaris/Linux utility that allows tasks to be automatically run in the background at regular intervals.

A crontab (CRON TABle) is a file or set of files which contain a schedule of cron jobs to be run.
Crontabs are executed by cron app, a long-running daemon which launches during system boot.

The crontab is used to edit

The name cron comes from chron, the Greek prefix for time. Crontab file location(s) varies by operating systems.

The cron package that gets installed on Debian systems if the local admin does not explicitly install a different one.

  • anacron The canonical way to run at least the jobs from /etc/cron.{daily,weekly,monthly) after startup.
  • cronie Cronie was forked by Red Hat from ISC Cron 4.1 in 2007, is the default cron implementation in Fedora and Red Hat Enterprise Linux.
  • systemd timers
  • bcron
  • mcron
  • The crond daemon is the background service that enables cron functionality.
  • crond checks for files in the /var/spool/cron and /etc/cron.d directories and the /etc/anacrontab file.
  • The cron utility runs based on commands specified in a cron table (crontab). Each user, including root, can have a cron file.
  • These files don’t exist by default, but can be created in the /var/spool/cron directory using the crontab -e command that’s also used to edit a cron file.
  • I strongly recommend that you not use a standard editor (such as Vi, Vim, Emacs, Nano, or any of the many other editors that are available). Using the crontab command not only allows you to edit the command, it also restarts the crond daemon when you save and exit the editor. The crontab command uses Vi as its underlying editor, because Vi is always present (on even the most basic of installations).

Cron Assets

A Debian system has the following cron tab files:

/etc/crontab

  • Holds entries to run the jobs from /etc/cron.{daily,weekly,monthly}
  • The directories /etc/cron.{daily,weekly,monthly} contain scripts (not crontabs!) that get executed sequentially in lexical sort order via run-parts from /etc/crontab in the respective intervals.
  • The hourly etc. directories make it simpler to add jobs (no need to figure out the exact syntax), and more importantly, allow the system to vary the real execution time depending on circumstances; this is particularly useful on systems which aren’t always on, with anacron.
  • The downside of the hourly|daily|weekly|monthly directories is that (AFAICT), they always run as root, and don’t directly support running with lesser privilege.

/var/spool/cron/crontabs

  • Permission denied
  • Holds one crontab file per user. Best not to use. Personal crontabs are a great place to hide jobs you will never find again.

/etc/cron.d

  • /etc/cron.d can be populated by many files, and are treated the same as entries in /etc/crontab. This allows both packages and the local administrator to drop-in system-wide cron jobs without having to modify /etc/crontab.
  • Files in /etc/cron.d have to meet a certain number of requirements (beyond being valid cron jobs): they must be owned by root, and must conform to run-parts naming conventions (no dots, only letters, digits, underscores, and hyphens).
  • If you’re considering using /etc/cron.d, it’s usually worth considering one of /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, or /etc/cron.monthly instead.
  • The items in cron.d are typically added via package installation, whereas the various crontab files are full of things created by a corresponding user.

Networking

FQDN (Fully qualified domain name)

A fully qualified domain name (FQDN) is the complete domain name for a specific computer, or host, on the internet. The FQDN consists of two parts: the hostname and the domain name. For example, an FQDN for a hypothetical mail server might be mymail.somecollege.edu. The hostname is mymail, and the host is located within the domain somecollege.edu.


The host name is the real name the server has. A canonical name is a name that the host is known by, but that the host is not actually called.

A machine’s host name could be “barkley.example.com”, but because it runs the web and FTP services for the domain “example.com”, it could have canonical names of “www.example.com” and “ftp.example.com”.

CNAME (Canonical Name record) is an ‘Alias Record’ or ‘A Record’. A type of resource record in the Domain Name System (DNS) that specifies that one domain name is an alias of another canonical domain name.

Any system hosting a Web site must have an IP address in order to be connected to the World Wide Web.

In DNS, a hostname is a domain name that identifies a host computer (e.g. foo.example.com). The term hostname is also used to mean the name used for a computer without the domain suffix (foo).

Note that the above usage, as used by DNS administrators, is slightly different from more causal usage where example.com would be considered a domain but many people don’t realise a fully qualified hostname is also technically a domain name (see RFCs).

In DNS there are many types of records:

“A” records associate a domain name with an address
“CNAME” records associate an alias (extra) domain name with a canonical domain name
multiple CNAME records can be used to associate several aliases with a single canonical domain name
“PTR” records are normally used to associate an IP-address with a canonical domain name.
etc
“Canonical” means “unique distinguished exemplar”. A computer may have many aliases but should only have one canonical name.

From RFC1035

CNAME A which specifies the canonical or primary name for the owner. The owner name is an alias.

From RFC1034

Most of these systems have a notion that one of the equivalent set of names is the canonical or primary name and all others are aliases.


The A and CNAME records are the two common ways to map a host name (“name”) to one or more IP addresses. There are important differences between these two records.

The A record points a name to a specific IP. If you want blog.dnsimple.com to point to the server 185.31.17.133 you’ll configure:

blog.dnsimple.com. A 185.31.17.133
The CNAME record points a name to another name instead of to an IP. The CNAME source represents an alias for the target name and inherits its entire resolution chain.

Let’s use our blog as an example:

blog.dnsimple.com. CNAME aetrion.github.io.
aetrion.github.io. CNAME github.map.fastly.net.
github.map.fastly.net. A 185.31.17.133
We use GitHub Pages and we set blog.dnsimple.com as a CNAME of aetrion.github.io, which is a CNAME of github.map.fastly.net, which is an A record pointing to 185.31.17.133. This means blog.dnsimple.com resolves to 185.31.17.133.

An A record points a name to an IP. A CNAME record can point a name to another CNAME or to an A record.

‘ls’ command to list files and directories

ls -la lists ALL files and directories in the current location:

total 40
drwxr-xr-x  7 squarem1admin  staff   224 Apr 27 10:41 .
drwxr-xr-x  7 squarem1admin  staff   224 Jan 22 15:48 ..
drwxr-xr-x  8 squarem1admin  staff   256 Jan 22 15:48 .svn
-rw-r--r--  1 squarem1admin  staff  1897 Apr 27 10:41 email.php
-rw-r--r--  1 squarem1admin  staff  2695 Jan 22 15:48 file_downloads.php
-rw-r--r--  1 squarem1admin  staff  6373 Apr 28 09:35 handle_errors.php
-rw-r--r--  1 squarem1admin  staff  1191 Jan 22 15:48 mime_types.php

ls -la lists output under the following columns:

  • File Permissions
  • Number of hard links
  • Owner name
  • Group Name
  • File Size
  • Month of last modification
  • Day of last modification
  • Time of last modification
  • File or Directory Name

Unix/Linux Permissions

Every file and directory in Linux has a set of flags associated with it which determines who can access it, and how they can access it. These flags are called file permissions, access permissions or modes, as in “mode of access.”

Users

Linux systems have three types of user:

  • owner/user u
    Applies only to the owner of the file or directory, and will not impact the actions of other users. By default, the user who creates the file becomes the owner of the file.
  • group g
    Applies only to the group that has been assigned to the file or directory, and will not effect the actions of other users. A group can contain multiple users. All the users belonging to a group have the same access permissions.
  • all other users o
    Applies to all other users with access to the file. Other has neither created the file nor is a group member. this is the permission group that you want to watch the most.

Users and groups can be locally managed in /etc/psswd or /etc/group.

List all the local users of a system

cut -d: -f1 /etc/passwd | column  

Permissions

All files and directories in Linux have a standard set of access permissions.

There are two ways to represent a file’s permissions: symbolically, using symbols like “r” for read, or with an octal numeric value.

  • read r
    The read permission on a file allows the user to read the contents of the file. The read permission on a directory allows the user to list the contents of the directory.
  • write w
    The Write permissions refer to a user’s capability to write or modify a file or directory.
  • execute x
    The Execute permission affects a user’s capability to execute a file or view the contents of a directory.

  • The file permissions listing has 10 characters.

  • The first character is the special permission flag. It can vary depending on the context.
  • The next 9 characters are divided into the three file permission groups. The first 3 slots represent the owner, the next three the group and the last three all users.
  • In each group the three characters indicate the applied permissions: read r, write w, and e execute. The - character indicates the user does not have that permission.

‘chmod’ command to modify file permissions

chmod stands for “change mode.” It changes the way a file can be accessed.