UNIX - Introduction
Tags: unix, linux, programming, beginners
Published: 2023-03-09 12:53:28

## OS and its history

Almost every computer science student comes across the term operating system now and then. The operating system is a computer program that serves as an interface between the user and the hardware. It does a variety of tasks for the user such as process management, file management, memory management, providing security for the user, CPU scheduling, and many more. The first OS came in the 1950s and was known as GMOS by IBM. However, the major leap in OS was made in 1969 when AT&T developed the UNIX OS. It was the first shot at a multiuser and multitasking OS developed with MIT's help. 

In 1977, BSD UNIX was developed by programmers at the University of California at Berkeley. This version of UNIX included enhanced networking capabilities. This enabled the OS to operate in a Local Area Network. 

In 1983, MIT Lab started the GNU (GNU's NOT UNIX) project. The main aim of the project was to develop an open source UNIX like OS. The kernel for this OS was known as the Herd kernel. While the Herd kernel was still struggling, Linus Torvalds, a student at the University of Helsinki created a kernel for the x86 architecture and then in 1991 our very first Linux kernel came into existence. This in association with GNU became the Linux OS. 


## What is Kernel?

The kernel is at the heart of the operating system. It is part of the program that always resides in the memory of the computer and has total control over the system's hardware to facilitate the interaction between the software and the hardware. The kernel performs all the necessary tasks such as handling hardware resources, running and managing the processes, handling interrupts, etc in its protected kernel space (a critical part in the memory that is inaccessible to other software or the less critical part of OS).


<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8j6j4ddnmztqj1atk78.png" width="50" height="50">

## The File System

Data in UNIX is stored as a file. All the files are organized into directories and the directories are organized into the file system. UNIX has a hierarchical file system with the root ( / ) as the base directory. In UNIX every user is taken to the /home directory as soon as they log in. The UNIX file system has the following properties:

- The root directory **( / )** contains all files and directories.

- The boot directory **( /boot )** has all the bootstrap programs that are used to start the OS.

- The bin directory **( /bin )** contains all the binaries that are required by the OS to boot and run. All user commands are stored here.

- All the system-wide configuration files are located in the **/etc** directory.

- The **/lib** directory contains all the libraries required to run the programs in the /bin folder.

- **/home** has all the user data

- The **/dev** folder contains all the device files that correspond to the physical device.

- **/mnt** is a regular directory that is used to mount other filesystems.

## File types in UNIX

UNIX has three types of files namely ordinary, directory, and special. Under special file types we have different subtypes such as symbolic link files, character devices, pipes, etc. To check the file type we use the command `ls -l`.
The the output of the above command is as follows.
```
drwxr-xr-x. 2 root root    6 Mar 11  2022 Desktop
drwxr-xr-x. 2 root root    6 Mar 11  2022 Documents
drwxr-xr-x. 2 root root    6 Mar 11  2022 Downloads
-rw-r--r--  1 root root    0 Mar  5 00:47 file1.txt
```

Each line in the output contains information about each file. The very first character in every line denotes the type of file. Refer to the table below to know the character that represents different file types.

|Type|Character|Description|
|----|---------|-----------|
|Ordinary| - (dash) |programme files, text, data etc|
|directory| d |folders present in file system|
|Symbolic links| l |these points to some other file in the system|
|Sockets | s |used for inter-process communication|
|Pipe| p |an unnamed object that is used to pipe the data from one programme to another during inter-process communication|
|Character device| c |used for unbuffered data transfer between two device|
|Block device| b |this file refers to a device|
|Door | D |used for inter-process communication for client and server, currently available in [Solaris](https://en.wikipedia.org/wiki/Oracle_Solaris)

## The Shell

The shell serves as an interface between the user and the kernel. 

It is the primary command interpreter that accepts commands from the user and passes them to the kernel for execution.

## Command Line Interface (CLI)
The user interacts with the shell using a GUI called CLI. 
Using this CLI, you can input text and send it to the shell for interpretation and execution.
The input texts are called **commands**. 
A command syntax helps the shell understand what the user wants to do.
Command syntax refers to the order and structure of different command components, such as commands, options, and arguments.

A few examples of commands are `ls`, `cd`, `mkdir`, `pwd`, etc. 

Now, these base commands have a fixed behavior and to change these behaviors we use options with the commands.

For example, let's check the behavior of the `ls` command with and without the `-l` option.
```
[root@server ~]# ls
Desktop  Documents  Downloads  file1.txt Music  Pictures  Public  Templates  Videos
[root@server ~]# ls -l
total 6
drwxr-xr-x. 2 root root    6 Mar 11  2022 Desktop
drwxr-xr-x. 2 root root    6 Mar 11  2022 Documents
drwxr-xr-x. 2 root root    6 Mar 11  2022 Downloads
-rw-r--r--  1 root root    0 Mar  5 00:47 file1.txt
drwxr-xr-x. 2 root root    6 Mar 11  2022 Music
drwxr-xr-x. 2 root root    6 Mar 11  2022 Pictures
drwxr-xr-x. 2 root root    6 Mar 11  2022 Public
drwxr-xr-x. 2 root root    6 Mar 11  2022 Templates
drwxr-xr-x. 2 root root    6 Mar 11  2022 Videos
```
Commands can be available with or without options. Options generally start with

a `-` or `--`. In most cases when we use a character as an option we use `-` and with words we use `--`.

How do we know when to use which option?

For that, we use the `<command> --help` command to get a list of all the options.

Let's take an example. We want to know about all the options available in the 'uname' command.

So for that, we do `uname --help` and the output is as follows.

```
Usage: uname [OPTION]...
Print certain system information.  With no OPTION, same as -s.

  -a, --all                print all information, in the following order,
                             except omit -p and -i if unknown:
  -s, --kernel-name        print the kernel name
  -n, --nodename           print the network node hostname
  -r, --kernel-release     print the kernel release
  -v, --kernel-version     print the kernel version
  -m, --machine            print the machine hardware name
  -p, --processor          print the processor type (non-portable)
  -i, --hardware-platform  print the hardware platform (non-portable)
  -o, --operating-system   print the operating system
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/uname>
or available locally via: info '(coreutils) uname invocation'
```

To get more detailed explanation we can use `man` command follow by the command whose description
is needed. For example: `man ls`

```
LS(1)                                                                         User Commands                                                                        LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

....... to be continued
```

Man page is a topic itself and we will talk about it later in this blog.  
The third command component i.e. arguments is the input for the command. 
They enable us to additionally define the output of the command.  For example, the `ls` command will show the list of files and directories in a directory, but `ls <sub_directory>` will show the files and directories inside the _sub_directory_.

```
[root@server ~]# ls
Desktop  Documents  Downloads  file1.txt  Music  Pictures  Public  Templates  Videos
[root@server ~]# ls Documents
file_in_document.txt
```
We can also give multiple commands in a single line by using `;` as delimiter between two commands.
For example, `uname;ls`
```
[root@server ~]# uname;ls
Linux
Desktop  Documents  Downloads  file1.txt  Music  Pictures  Public  Templates  Videos
```
## How to know about a command?

You can find the answer to this question by looking at the man page of that command.
Simply type `man <command_name>` and get all the details for the command.
The man page includes a command description, all applicable options and flags, examples, and other information about the command.
Man pages can be big and to navigate inside them we need to know certain commands to navigate and search with the man page.

### Searching with-in the man page
There are two ways to search in man page:
- Search by section number
- Search by keyword

To search by section number we use the command `man section_number <command>`.
 Eg: `man 2 uname`.

|Section | Description|
|--------|------------|
| 1 |User Command|
|2|System Calls|
|3|Library Function|
|4|Special files and Devices|
|5|File Format|
|6|Games|
|7|Miscellaneous|
|8|System Admistration|
|9|Kernel Routine|

To search by keyword we use the command `man -k <keyword>`.
For example, `man -k uname`.

## User accounts

A user account in UNIX is a login account through which a user can log in to the system.
Based upon role there are two types of users, one of which is a normal user and another is an admin, the super user or the root user.
A group is a collection of multiple users who can share files and resources.
Each group has a group identification number (GID), group name, and a list of users associated with it.

A user account has the following componentst associated with it:

- Username: The unique name for a particular user within the system.

- Password: The password is used in conjunction with the username to log in.

- UID (user identification number): An identification number assigned to a user for the purpose of identifying them within the system.

- GID (group identification number): This helps users identify groups.

- Home directory: The direct where the user will be taken after successful login.

In a traditional UNIX, the system pass is stored in the _/etc/passwd_ file, but in newer Linux distributions the hash of the password is stored in the _/etc/shadow_ file.
This was done to eliminate the vulnerability.

To change the password for the current user we use the `passwd` command.
For example, if a user with username user1 wants to change his/her password.
```
[user1@server ~]$ passwd
Changing password for user user1.
Current password: 
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[user1@server ~]$ 
```
But the root user does not need to provide the current password of any user while changing their password. To change the password for any user other than the logged-in user, the command would be `passwd <user_name>`.

So, thank you for this blog. Hope this helps you get a better understanding of the UNIX environment. I would appreciate any suggestions or queries you may have in the comment section below. Till then stay tuned. Bye!