Skip to main content

What is Linux?

Basically, Linux is a free operating system kernel. It’s the core engine of the OS, created by Linus Torvalds back in 1991. The whole thing is based on the older Unix system. When people say “Linux,” they could be talking about just the kernel or the entire operating system that’s built around it (like Ubuntu or Fedora).

The Kernel: The Brain of the System

The kernel is the most important part. It’s what talks directly to the hardware. It handles all the low-level tasks, like:
  • Managing Processes: Decides which program gets to use the CPU and for how long.
  • Memory Management: Makes sure programs don’t mess with each other’s memory. It even uses virtual memory to let apps use more memory than is physically there.
  • Device Drivers: The code that lets the system talk to your hardware, like your keyboard, monitor, or storage.
  • Time-Sharing: This is how the system can handle multiple people or programs at once, making it look like everything’s running at the same time. This is why servers can handle many users.

Key Points

  • Open Source: Anyone can see and change the code. This is why so many developers contribute to it.
  • Very Stable: This is a big one. Linux systems can run for a very long time without needing a reboot. It’s why servers and important systems use it.
  • Multi-User: It was built for multiple users to access the same system at the same time, each with their own secure space.
  • Hierarchical File System: Everything starts from the / (root) directory, just like a family tree. It keeps things organized.
  • Used Everywhere: From my desktop to servers and even my Android phone.

Linux vs. Unix

Think of Unix as the granddad. It came first. Linux is like its super-popular, free-to-use grandson. Linux took a lot of ideas from Unix but is completely open source.

Terminal & Shell

This is where you do all the real work.
  • The Terminal is the window or application you type into.
  • The Shell is the program inside the terminal that actually understands and executes the commands you type.
It’s a really powerful way to control the system, especially for tasks that need to be automated.

Distributions & Package Managers

A Distribution (or “distro”) is a full Linux OS package. It comes with the kernel plus other tools and apps. Ubuntu and Fedora are examples. A Package Manager is a tool that handles installing, updating, and removing software. It’s a lifesaver. You don’t have to manually download and install everything.
  • APT is what distros like Ubuntu use.
  • YUM is used by distros like Fedora.

Basic Linux Commands

A command is just a specific instruction you give the computer to perform a task. To run these commands, you use a terminal emulator, which is basically the program that lets you type commands into a Linux system.

Logging In & Out

  • Log in: You enter your username and password. The password won’t show up on the screen for security.
  • Log out: You can either type the command exit or logout, or just press Ctrl+D.

Some Simple Commands

These are the most basic, day-to-day commands to get started.
  • who : Shows all users currently logged into the system.
  • whoami : Tells you which user you are right now.
  • date : Displays the current date and time.
  • pwd : Stands for “present working directory” and tells you which directory you are currently in.
  • mail : Lets you read or send emails right from the terminal. You use mail [username] to send one. Press Ctrl+D to send the email after you’ve written your message.
  • write : A command to chat with another user who’s currently logged in.

Fixing Mistakes & Getting Help

  • Mistakes: If you type a command wrong, the terminal will usually say “command not found.”
  • Deleting text:
    • Backspace to delete the last character.
    • Ctrl+U to delete the entire line you’re currently typing.
  • Tab completion: If you start typing a command or file name and press the Tab key, the shell will try to automatically complete it for you. This is super useful for avoiding typos.
  • Command history: The history command shows all the commands you’ve entered before.
  • Getting help: The man command (short for “manual”) is your best friend. To learn what a command does and see all its options, just type man [command_name]. For example, man who would show you the manual page for the who command.

Terminal Behavior and The stty Command

Sometimes the terminal might act weird. This is usually because a setting got messed up. The stty command lets you check and change these settings.
  • stty -echo : This is useful. It turns off the echo, so what you type isn’t displayed on the screen. This is what happens automatically when you type your password. To turn it back on, you just run stty echo.
  • stty sane : This is the magic command for when things go really wrong. It resets your terminal settings back to a reasonable, working state.
  • stty INTR Control C : This sets the interrupt character to Ctrl+C. It’s the key combination you use to stop a running command.

The User Session Lifecycle

Logging In

  1. User Initiation: You start a login attempt either through a graphical login screen or a text-based terminal. For remote access, you use SSH (Secure Shell).
  2. Authentication: The system checks your entered username and password against its account database. If they match, you’re authenticated. For better security, some systems use multi-factor authentication (MFA) along with your password.
  3. Session Initialization: After authentication, the system sets up your user environment, loads necessary configurations, and runs startup scripts.
  4. Shell Assignment: The system assigns a shell (like the Bourne Again Shell or Bash) for you to use. In a graphical environment, you’ll see the desktop.

Logging Out

  • When you log out, the operating system cleans up all the processes and resources that were being used during your session. This closes your session cleanly.

Files & Directories commands

The wc Command

The wc command (word count) gives you a quick summary of a file’s contents. It displays the number of lines, words, and characters in a file.
  • Syntax: wc [filename]
  • Example: wc employee.txt would show a count like 6 18 93 employee.txt, which means the file has 6 lines, 18 words, and 93 characters.

The grep Command

The grep command is a powerful tool for searching for specific patterns within text files. It finds and displays lines that match a given pattern or regular expression.
  • Syntax: grep [pattern] [filename]
  • Example: grep "raja123" employee.txt would find and display every line in employee.txt that contains the string “raja123”.
  • Useful Options:
    • -i: Ignores case when searching, making it case-insensitive.
    • -r: Recursively searches through directories and their subdirectories.
    • -v: Inverts the search, showing lines that do not match the pattern.
    • -n: Displays the line number where the pattern was found.
    • -l: Lists only the names of the files that contain the match.
    • -w: Matches whole words only.
The ln (link) command creates links to files or directories from different locations. There are two types:
  1. Hard Links:
    • Creates a new reference to the same physical data on the disk.
    • Both the original file and the hard link share the same inode.
    • Changes made to one affect the other because they point to the exact same data.
    • Can only be created within the same file system.
    • Syntax: ln [source_file] [link_name]
  2. Symbolic (Soft) Links:
    • Creates a new file that acts as a pointer to the original file or directory.
    • They don’t share the same data blocks as the original file.
    • Can span across different file systems.
    • If you delete the original file, the symbolic link will be broken.
    • Syntax: ln -s [source_file] [link_name]

The pwd Command

The pwd (print working directory) command displays your current location in the file system. This is useful for knowing exactly where you are.
  • Syntax: pwd

The ls Command

The ls command lists the files and directories in your current location.
  • Syntax: ls [options] [directory]
  • Useful Options:
    • -l: Lists files in a “long format” with detailed information like permissions, owner, size, and modification date.
    • -a: Shows all files, including hidden ones (which start with a dot .).
    • -h: Displays file sizes in a human-readable format (e.g., 6K instead of 6000).
    • -t: Sorts the files by creation or modification time, with the newest files at the top.
    • You can combine these options, for example, ls -lha for a long list of all files with human-readable sizes.

The vi Editor

The vi command opens a powerful text editor, but it can be challenging for new users because of its “modal” nature.
  • To open a file: vi [filename]
  • Insert mode: Press i to start typing. The bottom-left of the screen will show “INSERT”.
  • Saving and Quitting:
    • Press ESC to exit insert mode.
    • :w to save the file.
    • :wq to save and quit.

The pr Command

The pr command formats the contents of a text file into pages, making it easier to view or print.
  • Syntax: pr [options] [filename]
  • Useful Options:
    • -l [number]: Sets the number of lines per page (default is 66).
    • -n: Adds line numbers to the output.
    • -h [header]: Sets a custom header for the page.

The mv (Move) and cp (Copy) Commands

  • mv (Move): Moves a file from one directory to another. After moving, the file no longer exists in the original location.
    • Syntax: mv [source_file] [destination_directory]
  • cp (Copy): Copies a file to a new location. The original file remains untouched.
    • Syntax: cp [source_file] [destination_directory]

The rm (Remove) Command

The rm command permanently deletes a file. This action cannot be undone, so use it with caution.
  • Syntax: rm [filename]

The touch Command

The touch command has two main uses:
  1. It creates a new, empty file if the file doesn’t already exist.
  2. It updates the timestamp of an existing file without changing its content.
  • Syntax: touch [filename]
  • Useful Option: -d allows you to set a specific date and time for the file’s timestamp.

The tr Command

The tr (translate) command is used to translate or delete characters from a text stream. You can use it to replace specific characters or delete them.
  • Syntax: tr [set1] [set2]
  • Example: echo "HELLO" | tr 'A-Z' 'a-z' would convert “HELLO” to “hello”.

The pr Command

As mentioned earlier, the pr command formats the contents of a file for printing or viewing.
  • Useful Options:
    • -n: Specifies the number of lines per page.
    • -m: Merges multiple files onto a single page.

The man Command

The man command (short for manual) is the official documentation for commands and programs. It provides comprehensive information on a command’s usage and options.
  • Syntax: man [command_name]
  • Useful Options:
    • -k: Searches the manual pages for a keyword.
    • -w: Prints the file path to the manual page.
    • --help or -h: Provides help about the man command itself.

User and System Commands

  • password : Changes a user’s password. An administrator can also change another user’s password using sudo password [username].
  • date : Displays the current system date and time. You can also customize the output format with options. For example, date "+%A, %d %B %Y" would display “Monday, 07 August 2023”.
  • who : Shows a list of all users currently logged into the system.
  • whoami : Displays the username of the current user.
  • write : Allows you to send a message to another user who is logged into the same system.

The sort Command

The sort command sorts the lines of a text file in a specified order. By default, it sorts lines alphabetically in ascending order. The change is not permanent; it only affects the output displayed on the screen.
  • Syntax: sort [filename]
  • Useful Options:
    • -r: Sorts in reverse order.
    • -n: Sorts numerically.
    • -u: Removes duplicate lines, showing each line only once.
    • -k: Sorts based on a specific column or field.

The tail Command

The tail command displays the last few lines of a file. This is useful for checking log files or tracking changes in files that are updated frequently.
  • Syntax: tail [filename]
  • Useful Options:
    • -n [number]: Specifies how many lines to display from the end of the file.
    • -f: “Follows” the file in real time, displaying new lines as they are added.

File Comparison: cmp and diff

  • cmp (Compare): This command compares two files byte by byte and reports the first location (byte and line number) where they differ. If there is no output, the files are identical.
    • Syntax: cmp [file1] [file2]
  • diff (Difference): This command compares two files and shows you the differences between them line by line. It is useful for understanding how two versions of a text file have changed.

Networking Commands

The hostname Command

The hostname command displays or sets the system’s hostname, which is a unique name for a computer on a network. You can think of it like a contact name for a phone number—it makes a computer’s IP address easier to identify.
  • Syntax: hostname [options] [new_hostname]
  • Example: hostname -i will display the IP address of the machine.

The ping Command

The ping command checks if a network host is reachable and measures how long it takes for a data packet to travel to the host and back. It’s a key tool for diagnosing network issues.
  • Syntax: ping [hostname_or_IP]
  • Useful Options:
    • -c: Specifies the number of packets to send before stopping.
    • -i: Sets the time interval between packets.
  • The command sends ICMP echo request packets to a specified URL or IP address.

The traceroute Command

traceroute traces the path that packets take from one IP address to another. It shows you all the “hops” (routers) the packet crosses to reach its destination.
  • Syntax: traceroute [hostname_or_IP]
  • Useful Options:
    • -n: Displays IP addresses instead of resolving hostnames.
    • -m: Sets the maximum number of hops to check.

The Nmap Command

Nmap (Network Mapper) is an open-source tool for network scanning and security auditing. It helps discover hosts, services, and potential vulnerabilities on a network.
  • Warning: Only use Nmap on networks you have permission to scan, as using it on unauthorized networks can be illegal.

Remote Connections: ssh and scp

The ssh Command

The ssh (secure shell) command creates a secure, encrypted connection to a remote server. It allows you to log in, run commands, and manage the remote system as if you were sitting in front of it.
  • Syntax: ssh [user]@[hostname_or_IP]
  • Useful Options:
    • -p: Specifies a custom port number for the connection.
    • -i: Specifies a private key file for authentication instead of a password.

The scp Command

The scp (secure copy) command securely copies files and directories between a local and a remote host. It works over the SSH protocol, ensuring the transfer is encrypted.
  • Syntax: scp [options] [source] [destination]
  • Copying a local file to a remote host: scp [local_file] [user]@[remote_host]:/[remote_path]
  • Copying a remote file to a local host: scp [user]@[remote_host]:/[remote_file] [local_path]
  • Useful Options:
    • -r: Recursively copies entire directories.
    • -p: Specifies a custom port number.

The /etc/ssh Directory

This directory contains all the configuration files for the SSH client and server. These files are critical for managing the security and behavior of SSH connections.
  • Key files inside:
    • ssh_config: Configuration settings for the SSH client.
    • sshd_config: Configuration settings for the SSH server.
    • moduli: Contains prime numbers used for key exchange encryption.
    • ssh_host_key and ssh_host_key.pub: The private and public keys used by the server for authentication.

The Linux Shell

Shell vs. Terminal

  • A terminal emulator is the application you use to access the shell (e.g., GNOME Terminal, KDE Konsole).
  • The shell is the program that processes and executes your commands. Popular shells include Bash and Zsh.

Prompts & User Types

The command prompt tells you if you’re a standard user or a super user.
  • $ : A standard user.
  • # : The root user, which is the super user account with administrative privileges.

Paths

  • Absolute Path: The full path to a file or directory starting from the root directory (/).
  • Relative Path: The path to a file or directory relative to your current directory.
  • cd ..: Moves you to the parent directory.

Tab Completion & Command History

  • Tab completion: As you type a command or filename, pressing the Tab key will automatically complete it for you or show you possible options.
  • Command history: Use the up and down arrow keys to cycle through commands you’ve used before.