Linux's Basics - Part 01

Linux's Basics - Part 01

Linux 🐧

In this guide, we’ll cover:

  • Linux Basics: Understand its history and core principles 🔍
  • File System Hierarchy: Learn how files and directories are structured 📂
  • Command Line Skills: Master essential commands to navigate and manipulate files 🖥️
  • User & Group Management: Manage access and permissions effectively 👥

Ready to dive into the world of Linux? Let’s get started! 💻✨

_______________________

Introduction to Linux

Linux is an open-source, Unix-like operating system. It is widely used for servers, desktops, and embedded systems due to its flexibility, security, and the ability to customize. Unlike proprietary operating systems (e.g., Windows, macOS), Linux is free to use, modify, and distribute.

Key Characteristics of Linux

• Open Source: The source code is available for anyone to view, modify, and distribute.

• Multi-user and Multi-tasking: Multiple users can work on the system simultaneously, and it can run several programs at once.

• Security: Linux is known for its robust security, with a well-defined user permission system and regular updates.

• Stability: Linux is very stable and rarely crashes, making it ideal for servers and critical applications.

Linux Distributions (Distros)

There are various Linux distributions tailored to different needs. Some popular ones include:

• Ubuntu: User-friendly, ideal for beginners.

• CentOS/Red Hat: Widely used in enterprise environments.

• Debian: Known for its stability and versatility.

• Arch Linux: Lightweight and customizable, but more suited for advanced users.

Why Choose Linux?

• Cost-Effective: Linux is free to use.

• Customizability: You can modify Linux to meet specific requirements.

• Community Support: With a large and active community, there are many forums and resources for troubleshooting and learning.

History of Linux

The story of Linux begins with Linus Torvalds, a Finnish software engineer, in 1991. Torvalds was studying computer science at the University of Helsinki and wanted to create a free and open-source operating system that could run on personal computers. Here’s a quick timeline of how Linux evolved:

1. 1991 – The Birth of Linux

Linus Torvalds started developing Linux as a hobby while studying at the university. His goal was to create a free, Unix-like operating system that could run on Intel-based PCs.

• On August 25, 1991, Torvalds posted a message on the comp.os.minix newsgroup, announcing the first version of Linux:

I'm doing a free operating system (just a hobby, won't be big and professional like GNU) for 386(486) AT clones.

2. 1992 – The Linux Kernel

Linux became a “kernel,” the core part of an operating system that interacts with the hardware and software. It was released under the GNU General Public License (GPL), which allowed anyone to use, modify, and distribute the code freely.

3. 1993-1994 – Rise of Distributions

As Linux grew in popularity, developers began packaging it into distributions (distros), which included the Linux kernel and additional software. The first well-known distros were Slackware (1993) and Debian (1993).

4. 1996-2000 – Linux Grows in Popularity

Linux saw rapid growth in the 1990s. Major companies like IBM started supporting Linux, and it was adopted by more businesses for its reliability and low cost. By the late 1990s, Red Hat Linux and SUSE Linux became popular distributions.

5. 2000s to Present

The 2000s saw the rise of Linux on the desktop, especially with user-friendly distributions like Ubuntu. It became the foundation of many enterprise environments and cloud services.

Linux’s popularity has soared due to its use in smartphones (Android is based on Linux), servers, and supercomputers (most supercomputers run Linux).

Linux Today

• Mobile Devices: Android, which powers most smartphones and tablets, is based on Linux.

• Servers and Cloud: Linux powers most of the world’s web servers and cloud infrastructure.

• Enterprise and Development: Linux is widely used in software development and DevOps due to its flexibility, security, and open-source nature.

Linux in the Modern World

Today, Linux is an essential part of modern computing. From powering the majority of the internet’s infrastructure to being the operating system of choice for developers, Linux has come a long way from its humble beginnings in 1991.

Terminal Basics

The terminal in Linux is a command-line interface (CLI) that allows you to interact with the system by typing commands. Unlike graphical user interfaces (GUIs) that rely on windows and icons, the terminal lets you control the system using text-based commands. It’s powerful and fast once you’re familiar with the commands.

Opening the Terminal

• In most Linux distributions, you can open the terminal by pressing Ctrl + Alt + T or searching for “Terminal” in the application menu.

Basic Terminal Operations

• Command Prompt:

When you open the terminal, you’ll see a prompt (e.g., user@hostname:~$), where you can type commands. It shows the current user and the directory you’re in.

• Executing Commands:

Simply type a command and press Enter. For example, typing ls will list the contents of the current directory.

• Getting Help:

You can always use man (manual) or --help for help with commands. For example:

man ls  # Displays the manual for the 'ls' command  

ls --help  # Displays basic help information for 'ls'  

File System Hierarchy in Linux

Linux follows a structured directory system, starting from the root directory (/). The file system hierarchy is organized like a tree, with / at the root and other directories branching off it. Here’s a breakdown of common directories:

Common Linux Directories

• / (Root Directory):

This is the top-level directory that contains all other files and directories on the system.

• /bin:

Contains essential binary executables (programs) that are required for system booting and for running basic commands.

Example: ls, cp, mv

• /boot:

Contains files needed for the boot process, including the Linux kernel (vmlinuz), initial RAM disk (initrd), and bootloader configuration files.

• /dev:

Contains device files that represent hardware devices. For example, hard drives, USB drives, and terminals are represented here as device files.

Example: /dev/sda, /dev/tty

• /etc:

Contains system-wide configuration files for the operating system and installed programs.

Example: /etc/passwd (user account details), /etc/fstab (filesystems).

• /home:

Contains home directories for users. Each user has their own subdirectory here.

Example: /home/user for user data, files, and configurations.

• /lib:

Contains shared library files and kernel modules required for the system to run properly.

Example: system libraries like libc.so

• /media:

Mount points for removable media devices like CDs, DVDs, or USB drives. When you insert a USB stick, it may automatically mount here.

• /mnt:

Temporary mount points for file systems (used by administrators to manually mount devices).

• /opt:

Contains optional application software packages that are not part of the core system.

• /proc:

A virtual directory that contains system information and process information (in real-time).

Example: /proc/cpuinfo (CPU information), /proc/meminfo (memory information)

• /root:

The home directory for the root user (administrator).

Example: /root is similar to /home/user but for the root user.

• /sbin:

Contains system binaries for administration and system repair. These commands typically require superuser privileges to execute.

Example: fsck, shutdown

• /srv:

Contains data for services provided by the system, like web or FTP servers.

• /tmp:

A temporary directory where programs can store files temporarily. This directory is usually cleared at boot.

• /usr:

Contains read-only user data, including system programs, libraries, and documentation.

Example: /usr/bin for user binaries.

• /var:

Contains variable data files, such as logs, databases, and spool files.

Example: /var/log for system logs.

File System Navigation

Navigating the Linux file system is an essential skill. You can move around the directories using commands like cd and check the content with commands like ls.

1. Change Directory (cd):

Use the cd command to change the current directory.

• Go to a specific directory:

cd /home/user/Downloads

• Go to the home directory:

cd ~

• Go to the parent directory:

cd ..

2. List Directory Contents (ls):

Use ls to display the contents of a directory.

• Basic usage:

ls

• List with details (permissions, owner, size):

ls -l

3. Print Working Directory (pwd):

Use pwd to display the current directory’s full path.

pwd

Basic Commands

Here are a few basic commands every Linux user should know:

1. ls (List files):

Lists files in the current directory.

ls  

ls -l  # Lists with details  

ls -a  # Lists all files, including hidden files

2. cd (Change directory):

Changes the current directory.

cd /path/to/directory

cd ~  # Go to the home directory

cd ..  # Go to the parent directory

3. pwd (Print working directory):

Shows the current directory’s full path.

pwd

4. mkdir (Make directory):

Creates a new directory.

mkdir new_directory

5. rmdir (Remove directory):

Removes an empty directory.

rmdir directory_name

File Manipulation Commands

1. cp (Copy files):

Copies files from one location to another.

• Example: Copy a file to a new directory.

cp file.txt /path/to/destination/

2. mv (Move/rename files):

Moves or renames files and directories.

• Example: Rename a file:

mv oldname.txt newname.txt

• Example: Move a file to another directory:

mv file.txt /path/to/destination/

3. rm (Remove files):

Deletes files or directories.

• Delete a file:

rm file.txt

• Delete a directory and its contents:

rm -r directory_name

4. touch (Create empty file):

Creates a new empty file.

touch newfile.txt

These commands and concepts form the foundation for working in the Linux terminal. Once you’re comfortable with these, you’ll be able to efficiently manage files and navigate the system.

Understanding File Permissions in Linux

Linux uses a permission model that controls who can read, write, or execute a file. Every file and directory has three types of permissions: read (r), write (w), and execute (x). These permissions are assigned to three different user categories: owner, group, and others.

Permissions Overview

• Read (r): Allows the user to view the contents of a file.

• Write (w): Allows the user to modify the contents of a file.

• Execute (x): Allows the user to run a file as a program or script.

User Categories

• Owner (user): The user who owns the file.

• Group: Other users in the file’s group.

• Others: All other users on the system.

Permissions are typically shown using the ls -l command. Here’s an example of how they might look:

-rwxr-xr-- 1 user group 1234 Oct 10 12:00 file.txt

• The first character (-) represents the file type (- for a regular file, d for a directory, etc.).

• The next three characters (rwx) are the owner’s permissions: read, write, and execute.

• The next three characters (r-x) are the group’s permissions: read and execute.

• The final three characters (r--) are the others’ permissions: read only.

File Permission Example

If a file has the following permissions:

-rwxr-xr-- 1 user group 1234 Oct 10 12:00 file.txt

• Owner (user): Can read, write, and execute the file.

• Group: Can read and execute the file but not write to it.

• Others: Can only read the file.

Changing File Permissions

You can change file permissions using the chmod (change mode) command. Permissions can be modified either symbolically (using r, w, x) or numerically.

Symbolic Method

• Add permission:

chmod +x file.txt  # Adds execute permission to the file for all categories (user, group, others).

chmod u+x file.txt  # Adds execute permission only for the owner (user).

• Remove permission:

chmod -x file.txt  # Removes execute permission from all categories.

chmod g-w file.txt  # Removes write permission from the group.

Numeric Method (Octal Notation)

Permissions can also be set using numbers. Each permission type corresponds to a number:

• Read (r) = 4

• Write (w) = 2

• Execute (x) = 1

You sum the numbers for the desired permissions:

• 7 = rwx (read, write, execute)

• 6 = rw- (read, write)

• 5 = r-x (read, execute)

• 4 = r-- (read)

• 3 = wx- (write, execute)

For example:

chmod 755 file.txt  # rwxr-xr-x (Owner can read, write, execute; Group and others can read, execute)

chmod 644 file.txt  # rw-r--r-- (Owner can read, write; Group and others can read)

Viewing File Content

You can view the content of a file using several commands:

1. cat (Concatenate and display file content)

cat is used to display the entire content of a file.

cat file.txt  # Displays the full content of the file

2. less (View content with pagination)

less allows you to view large files one screen at a time. You can scroll through the file using arrow keys or space for the next page.

less file.txt

• Navigate with arrow keys (up/down), space for next page, and q to quit.

3. more (View content with pagination)

Similar to less, but with fewer features. You can scroll down one page at a time, using the space key.

more file.txt

• Press space to go to the next page, b to go back, and q to quit.

Searching Through Files

There are several powerful commands to search through files in Linux:

grep (Global Regular Expression Print)

grep is used to search for specific text patterns within files. It’s case-sensitive by default.

• Search for a word or pattern in a file:

grep "pattern" file.txt

• Search recursively through directories:

grep -r "pattern" /path/to/directory

• Use -i for case-insensitive search:

grep -i "pattern" file.txt

• Display line numbers of matches:

grep -n "pattern" file.txt

find (Search for files)

find is used to search for files and directories by name, type, permissions, and other attributes.

• Find a file by name:

find /path/to/search -name "file.txt"

• Find files with a specific extension:

find /path/to/search -name "*.txt"

• Find files larger than a certain size:

find /path/to/search -size +100M  # Files larger than 100MB

• Search for files and execute commands on them:

find /path/to/search -name "*.log" -exec rm {} \;  # Finds and deletes all .log files

By understanding file permissions, changing permissions, and knowing how to view and search through files, you’ll be equipped with essential tools to interact with Linux systems effectively. These skills are fundamental to maintaining security, managing files, and performing efficient searches.

User Accounts and Groups in Linux

In Linux, user accounts and groups play a crucial role in organizing and managing permissions and access control. Every user on a system has a unique identifier (UID), and groups are used to assign permissions to multiple users. Users and groups help to control access to files, directories, and other system resources.

User Accounts

A user account represents an individual user on a Linux system. Each user has a unique username and user ID (UID), which are used to identify the user. The user also has associated files, such as the home directory and configuration files.

Groups

A group is a collection of users. Groups help simplify managing permissions by assigning a set of permissions to the group, rather than managing each user individually. Each group has a group name and a group ID (GID). Users can belong to multiple groups, and each user has a primary group.

Creating and Managing Users

In Linux, you can create and manage user accounts using commands such as useradd, usermod, and userdel.

Creating a New User

To create a new user, use the useradd command. For example:

sudo useradd john  # Creates a user named "john"

This command creates the user but does not create a home directory or set a password. To include these options, use:

sudo useradd -m -s /bin/bash john  # -m creates a home directory, -s specifies the shell

• The user’s home directory will be /home/john.

• The user will use /bin/bash as their shell.

Setting a Password for a User

After creating the user, you can set their password with the passwd command:

sudo passwd john  # Sets or changes the password for "john"

You will be prompted to enter the new password for the user.

Viewing Existing Users

To see a list of all users, you can check the /etc/passwd file:

cat /etc/passwd  # Displays the list of all users

Changing User Information

The usermod command is used to modify an existing user’s details, such as username, home directory, and shell.

Change the Username

To change a user’s username:

sudo usermod -l newname oldname  # Changes the username from "oldname" to "newname"

Change the Home Directory

To change a user’s home directory:

sudo usermod -d /home/newdir -m username  # Moves the home directory to /home/newdir

Change the User’s Shell

To change the shell for a user:

sudo usermod -s /bin/zsh username  # Changes the default shell to Zsh

Changing Passwords

The passwd command allows you to change a user’s password, including for yourself or other users (with sudo privileges).

Changing Your Own Password

To change your password:

passwd  # You will be prompted to enter a new password

Changing Another User’s Password

To change the password for another user:

sudo passwd username  # Changes the password for "username"

Assigning Users to Groups

In Linux, users can belong to one or more groups. Groups are used to manage permissions for multiple users collectively. The usermod command is used to assign users to groups.

Adding a User to a Group

To add a user to an existing group:

sudo usermod -aG groupname username  # Adds user "username" to the "groupname"

• The -a flag appends the user to the group without removing them from other groups.

• The -G flag specifies the group(s) the user should belong to.

Listing the Groups a User Belongs To

To see which groups a user is part of:

groups username  # Displays all groups the user "username" is a member of

Creating a New Group

To create a new group, use the groupadd command:

sudo groupadd developers  # Creates a group named "developers"

Deleting a User or Group

To remove a user:

sudo userdel username  # Deletes the user "username" from the system

To remove a group:

sudo groupdel groupname  # Deletes the group "groupname"

Example Commands for Managing Users and Groups:

1. Create a user named “alice” with a home directory and set her shell to /bin/bash:

sudo useradd -m -s /bin/bash alice

sudo passwd alice  # Set her password

2. Add “alice” to the “developers” group:

sudo usermod -aG developers alice

3. Change “alice”’s password:

sudo passwd alice  # Prompt to change the password

4. List all users on the system:

cat /etc/passwd  # Displays all users

5. Delete a user named “bob”:

sudo userdel bob  # Deletes the user "bob"

6. Create a new group called “admins”:

sudo groupadd admins

7. List groups for user “alice”:

groups alice  # Displays groups "alice" is part of

Managing users and groups is an essential aspect of system administration in Linux. Proper user account management ensures that access control is in place and that users only have access to the resources they need. By understanding commands like useradd, usermod, passwd, and groupadd, you can effectively manage user accounts, set permissions, and assign users to the appropriate groups for your system’s security and organization.

__________________________________________________________________________________

Conclusion

Mastering Linux starts with the basics. With a solid understanding of its history, file system, commands, and user management, you'll be well-equipped to explore its full potential. Start your journey today and unlock the power of Linux! 🚀

Nuwan Godagama

Assistant Engineer at Sri Lanka Telecom (services) Ltd

1d

Great job keep writing 🫡

Like
Reply
Thisara Shaminda

Business Analyst | Developer | Information Technology

3w

Very helpful

Like
Reply
MOHAMED RIKAZ

Information Technology Support at Hamad International Airport, Doha, Qatar

3w

thank you, very useful

Like
Reply
Dayan Wijesekera

Intern DevOps Engineer | Fullstack Developer| Event Co-ordinator |

3w

Great work and Thank you sharing this 👏

Like
Reply
Yasiru Sandamal

IT Administrator | MSc Information Technology in Cyber Security (Reading) | BSc(Hons) Networking and Network Security | 10+ CISCO Badges | 20+ Microsoft Badges

3w

Great job 👏

Like
Reply

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics