Linux is a powerful and versatile operating system widely used across various platforms, from servers to mobile devices. This article explores the basics of Linux, its structure, different distributions, and essential commands.
What is Linux?
Linux is an open-source, Unix-like operating system known for its stability and flexibility. It is widely used in servers, desktops, and embedded systems. The Linux operating system consists of the Linux kernel and a collection of software packages that provide a complete operating environment.
Linux: OS vs. Kernel
The Linux kernel, created by Linus Torvalds in 1991, is the core component of the Linux operating system. It manages hardware resources and allows software to interact with the hardware. The kernel itself is not a complete operating system; it requires additional utilities and a shell to form a full OS. For example, Android is a Linux-based operating system that includes a user interface.
Linux Distributions
Linux distributions, or distros, are variations of the Linux operating system that include the kernel and additional software packages. Some popular distributions include:
- Ubuntu
- CentOS
- Red Hat Enterprise Linux (RHEL)
- Fedora
- SUSE
- Arch Linux
These distributions are largely similar, with minor differences in commands and package management. RHEL, for example, is open source but requires a subscription for enterprise support.
Linux Servers
A Linux server, often referred to as a “box,” “node,” or “IP address,” provides various services through different protocols and ports. Common protocols and their associated ports include:
- SSH: Port 22, used for secure remote connections
- HTTP: Port 80, used for web traffic
- HTTPS: Port 443, used for secure web traffic
- MySQL: Port 3306, used for database connections
Servers have a range of ports from 0 to 65,535, each serving different purposes.
Authentication Methods
Authentication on Linux systems can be categorized into three types:
- What you know: Username and password
- What you have: Username and tokens (e.g., RSA keys)
- What you are: Biometric data like fingerprints or facial recognition
Connecting to a Linux Server
Connecting to a Linux server is akin to accessing a locked box with a visible lock (public key) and a hidden key (private key). This method uses the “what you have” authentication, involving public and private key pairs. On Windows, tools like Git Bash, PuTTY, or MobaXterm can generate these keys using the ssh-keygen command.
To connect to a server, use the SSH command:
ssh -i [private-key] username@IP For example:
ssh -i lokeshaws.pem ec2-user@54.164.254.130 Understanding Command Formats
Linux commands typically follow the format: command-name [options] [arguments]. Options and arguments can be optional. Use --help or man command-name to get detailed information about a command.
Basic Linux Commands
- uname: Displays the kernel name.
- ls: Lists directory contents. Variants include
ls -l,ls -lr,ls -lt,ls -ltr, andls -la. - cd: Changes the directory. Use
cd /to go to the root directory andcd ..to go back one level. - pwd: Prints the current working directory.
User Indicators in Terminal
In the terminal, the $ symbol indicates a normal user, while the # symbol indicates a root user.
Linux Case Sensitivity
Linux is case-sensitive, meaning that file and directory names distinguish between uppercase and lowercase letters.
Listing Users on a Linux Server
To get a list of users on a Linux server, you can use the awk command:
awk -F ":" '{print $1}' /etc/passwd CRUD Operations
- Create: Use
touchto create files andmkdirto create directories. - Read: Use
catto display file contents. - Update: Use
cat >to edit files,>to replace, and>>to append. - Delete: Use
rmfor files,rmdirfor empty directories, andrm -rfor directories with contents.
Copying and Moving Files
- cp: Copies files or directories. Use
-rfor recursive copying. - mv: Moves or renames files and directories.
Searching and Piping
- grep: Searches for patterns within files. Use
-ito ignore case. - Pipe (|): Passes the output of one command as input to another.
Viewing File Contents
- head: Displays the first 10 lines of a file. Use
-nto specify a different number of lines. - tail: Displays the last 10 lines of a file. Use
-nto specify a different number of lines.
Downloading Content
- wget: Downloads files.
- curl: Fetches text content directly to the terminal.
Text Manipulation
- cut: Splits lines based on a delimiter.
- awk: Processes and analyzes text files.
Permissions
Linux permissions are divided into three categories: read, write, and execute, represented by r, w, and x. The permissions are displayed in a format like -rw-r--r--, where:
- The first character indicates a file (
-) or directory (d). - The next three characters represent the owner’s permissions.
- The following three characters represent the group’s permissions.
- The last three characters represent others’ permissions.
Permissions can be changed using the chmod command. For example:
chmod o+w [file]: Gives write permission to others.chmod o-rwx [file]: Removes all permissions for others.chmod ugo+rwx [file]: Gives full permissions to everyone.
Permissions can also be set using numeric values, where:
- Read (
r) is 4 - Write (
w) is 2 - Execute (
x) is 1
For example, chmod 777 [file] gives all permissions to everyone.
vim Editor
vim is a powerful text editor with modes for editing and command execution. It has three primary modes:
- Esc Mode: Used for navigation and command execution.
- Command/Colon Mode: Accessed with
:, used for commands like:qto quit,:wqto save and quit, and:qto force quit. - Insert Mode: Accessed with
i, used for inserting text.
In Esc mode, you can use commands like u to undo, yy to copy, dd to delete a line, and p to paste. Commands like :/ and :? are used for searching, and :s/word-to-find/word-to-replace/g is used for replacing text.
Linux is a robust operating system that offers extensive control over computing environments. Understanding its structure and commands can greatly enhance your ability to manage and utilize Linux systems effectively.