Unlock Linux Mastery: Essential Commands, Shell Basics, and System Administration Guide
This comprehensive guide introduces Linux fundamentals for programmers, covering core concepts, essential commands, shell usage, file operations, process management, networking, package handling, compression, backup tools, and Vim editing, providing practical examples and step‑by‑step instructions to help you become proficient with Linux.
Foreword
Learning Linux is essential for any programmer. Front‑end developers have fewer opportunities to work with Linux compared to back‑end developers, so it is often overlooked, but mastering Linux is a must‑have skill for developers.
If this article helps you, please give it a 👍.
The author uses an Alibaba Cloud ECS (the cheapest type) running CentOS 7.7 64‑bit. You can also install CentOS on a virtual machine on your own computer; the installation tutorials are widely available on Google.
Linux Basics
Operating System
The operating system, abbreviated as OS, is the first layer of software on top of hardware, acting as a bridge between hardware and other software.
The OS controls program execution, manages system resources, provides basic computing functions such as memory management and prioritizing resource allocation, and offers basic service programs.
What is Linux
Difference Between Linux Kernel and Linux Distributions
Linuxkernel is maintained by Linus Torvalds and provides hardware abstraction, disk and file system control, and multitasking. Linux distribution is the kernel plus a collection of common software packages, forming a complete operating system.
Summary: The true Linux refers to the kernel, while the commonly mentioned Linux usually means a complete distribution.
Linux vs Windows
Stable and efficient.
Free (or low‑cost).
Fewer vulnerabilities and fast patches.
Multitasking and multi‑user.
More secure user and file permission policies.
Suitable for embedded systems with small kernels.
Relatively low resource consumption.
Linux System Types
Red Hat Enterprise Linux ( RHEL) – widely used in production, commercial.
Fedora – community edition released by Red Hat, serves as a testing ground for RHEL.
CentOS – free rebuild of RHEL, widely used.
Deepin – Chinese distribution focusing on integration and configuration of open‑source products.
Debian – stable, secure, widely adopted abroad.
Ubuntu – Debian‑based, excellent desktop compatibility, also suitable for servers.
Connecting to an Alibaba Cloud Server via Terminal
Execute ssh [email protected] and enter the password to log in to the remote server. From now on you can operate the remote server from your local computer.
The black panel is the Shell (command‑line environment). ssh root@xxx is a command that must be run inside a Shell.
Shell
The word Shell literally means “shell”; it is the interface between the user and the kernel, providing a command prompt for user input. Shell is a program that offers a dialog environment with a single prompt, also called a command‑line interface ( CLI). Shell interprets user commands and passes them to the OS for execution, returning results. Shell is a toolbox that provides various utilities for interacting with the OS.
Types of Shells
Many programs can serve as a shell as long as they provide a command‑line environment.
Bourne Shell ( sh)
Bourne Again Shell ( bash) – the most commonly used shell.
C Shell ( csh)
TENEX C Shell ( tcsh)
Korn Shell ( ksh)
Z Shell ( zsh)
Friendly Interactive Shell ( fish)
On macOS, the default shell is bash.
Run echo $SHELL to view the current shell. Use cat /etc/shells to list all installed shells.
Commands
Command Prompt
After entering the command line, the prompt usually ends with a dollar sign $. For example, running pwd displays the current directory:
root@iZm5e8dsxce9ufaic7hi3uZ ~# pwd
/rootExplanation of the output: root – username. iZm5e8dsxce9ufaic7hi3uZ – hostname. ~ – home directory. # – indicates root privileges (ordinary users see $). whoami shows the current user; hostname shows the host name.
Commands such as creating or deleting users will be covered later; for now we use the root user for demonstration.
Note: root is the superuser with full system permissions.
Command Syntax
command parametersExamples of common parameters: -i – ignore case. -n – show line numbers. -v – show lines that do NOT match. -r – recursive search.
Advanced Usage
grepcan use regular expressions:
grep -E path /etc/profile # exact match
grep -E ^path /etc/profile # lines starting with "path"
grep -E [Pp]ath /etc/profile # case‑insensitive matchSorting
Sort lines in a file: sort name.txt Common options: -o – write output to a new file. -r – reverse order. -R – random order. -n – numeric sort.
Word Count
wcreports lines, words, and bytes: wc name.txt Options: -l – line count only. -w – word count only. -c – byte count only. -m – character count only.
Unique Lines
uniqremoves consecutive duplicate lines:
uniq name.txt # remove duplicates and print
uniq name.txt uniq.txt # save result to uniq.txtOptions: -c – prefix lines by occurrence count. -d – show only duplicated lines.
Cut
Extract sections of each line:
cut -c 2-4 name.txt # characters 2 to 4 of each line
cut -d , -f 1 notes.csv # first field using comma as delimiterOptions: -d – set delimiter. -f – select field numbers.
Redirection, Pipes, and Streams
Linux commands can send output to the terminal, a file, or another command. The three standard streams are stdin, stdout, and stderr.
Redirection
Output Redirection >
Redirect command output to a new file (overwrites if the file exists):
cut -d , -f 1 notes.csv > name.csvAppend Redirection >>
Append output to the end of a file (creates the file if it does not exist):
cut -d , -f 1 notes.csv >> name.csvError Redirection 2> and 2>>
Redirect standard error to a file (overwrite or append):
cat not_exist_file.csv > res.txt 2> errors.log
cat not_exist_file.csv >> res.txt 2>> errors.logRedirect Both Streams 2>&1
cat not_exist_file.csv > res.txt 2>&1 # overwrite both
cat not_exist_file.csv >> res.txt 2>&1 # append bothInput Redirection <
cat < name.csvHere‑Document <<
sort -n << END
...input lines...
ENDPipes |
Pass the output of one command as input to another:
cut -d , -f 1 name.csv | sort > sorted_name.txtStreams
A stream is a flow of data (often binary) that can be redirected or piped between commands.
Viewing Processes
w
06:31:53 up 25 days, 9:53, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 118.31.243.53 05:56 1.00s 0.02s 0.00s w 06:31:53ps
PID TTY TIME CMD
1793 pts/0 00:00:00 bash
4756 pts/0 00:00:00 psCommon options: -ef, -efH, -u, -aux, --sort -pcpu, -axjf.
top
top - 07:20:07 up 25 days, 10:41, 1 user, load average: 0.30, 0.10, 0.07
Tasks: 67 total, 1 running, 66 sleeping
%Cpu(s): 0.7 us, 0.3 sy, 99.0 id
KiB Mem : 1882072 total, 552148 free, 101048 used, 1228876 buff/cache
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
956 root 10 -10 133964 15848 10240 S 0.7 0.8 263:13.01 AliYunDunkill
kill 956 # terminate process 956
kill -9 7291 # force kill process 7291Managing Processes
Process States
R– running. S – sleeping (interruptible). D – uninterruptible sleep. Z – zombie (terminated but not reaped). T – stopped (signal‑stopped).
Foreground vs Background
Append & to run a command in the background. Use bg to resume a stopped job, fg to bring it to the foreground, and jobs to list background jobs.
nohup
Run a command immune to hang‑up signals:
nohup cp name.csv name-copy.csv &bg and fg
bg %1 # resume job 1 in background
fg %1 # bring job 1 to foregroundDaemons
Daemons are background services whose parent process is PID 1 ( systemd). They run continuously and provide core system functions.
systemd
ps -aux | grep systemd
root 1 0.0 0.2 51648 3852 ? Ss Feb01 1:50 /usr/lib/systemd/systemd --switched-root --system --deserialize 22Common systemctl commands: start, stop, restart, status, reload, enable, disable, is-enabled, list-unit-files --type=service.
File Compression and Archiving
tar
Create an archive: tar -cvf archive.tar file1 file2 file3 List contents: tar -tf archive.tar Append files: tar -rvf archive.tar file.txt Extract:
tar -xvf archive.targzip / gunzip
gzip archive.tar
gunzip archive.tar.gzTar + Gzip
tar -zcvf archive.tar.gz folder/ # create and compress
tar -zxvf archive.tar.gz # extractzcat , zless , zmore
zcat archive.tar.gzzip / unzip
Install: yum install zip unzip Compress and extract:
zip -r sort.zip sort/
unzip archive.zip
unzip -l archive.zipCompiling and Installing Software
When a package is not available via yum, compile from source:
Download source code.
Extract the archive.
Run ./configure to check dependencies.
Run make to compile.
Run make install to install.
Example: compile htop from source.
Networking
ifconfig
Show network interfaces (install net-tools if missing):
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.24.78 netmask 255.255.240.0 broadcast 172.31.31.255
ether 00:16:3e:04:9c:cd txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0 (Loopback)host
Install with yum install bind-utils. Resolve names and IPs:
host github.com
host 13.229.188.59SSH
Connect to a remote server: ssh [email protected] # default port 22 Configure ~/.ssh/config for shortcuts:
Host lion
HostName 172.x.x.x
Port 22
User rootAfter configuration, connect with ssh lion.
Key‑Based (Password‑less) Login
Generate a key pair with ssh-keygen (creates ~/.ssh/id_rsa and id_rsa.pub).
Copy the public key to the server with ssh-copy-id [email protected].
Now ssh [email protected] logs in without a password.
wget
Download files from the command line:
wget http://www.minjieren.com/wordpress-3.1-zh_CN.zipUse -c to continue interrupted downloads.
Backup
scp
Secure copy files between hosts:
scp file.txt [email protected]:/root
scp [email protected]:/root/file.txt ./file.txtrsync
Synchronize directories (install with yum install rsync).
rsync -arv Images/ backups/
rsync -arv Images/ [email protected]:backups/Use --delete to remove files that no longer exist in the source.
System Control
Shutdown, reboot, or power off:
halt # requires root
reboot # requires root
poweroff # can be run by any userVim Editor
What Is Vim?
Vim is an enhanced version of vi, offering powerful features such as code completion, compilation, and error navigation. It is a favorite editor among Unix users alongside Emacs.
Vim Modes
Normal (interactive) mode – default mode for navigation and commands.
Insert mode – for entering text (enter with i, a, o, etc.).
Command mode – accessed by : to run commands like wq, q!, etc.
Visual mode – for selecting text (character, line, or block).
Normal Mode
Use h j k l or arrow keys to move the cursor.
Insert Mode
Press i to insert before the cursor, a after, o on a new line below, Esc to return to Normal mode.
Command Mode
Press : then type commands such as w (write), q (quit), wq (write and quit).
Visual Mode
Press v for character selection, V for line selection, Ctrl+v for block selection. Use d to delete, I to insert at the beginning of each selected line, u / U to change case.
Basic Operations
Open Vim: vim filename (creates the file if it does not exist).
Insert text: press i then type.
Move cursor: h left, j down, k up, l right, 0 line start, $ line end, w next word.
Delete character: x (prefix with a number to delete multiple).
Delete line: dd (prefix with a number to delete multiple lines).
Delete word: dw (prefix with a number for multiple).
Copy line: yy; copy word: yw.
Paste: p (prefix with a number to paste multiple times).
Replace a character: r then the new character.
Undo: u (prefix with a number for multiple undos); redo: Ctrl+r.
Jump to line: gg (first line), G (last line), or 7gg (line 7).
Search: /pattern then n (next) or N (previous). Use ? to search backward.
Search and replace: :%s/old/new/g for whole file, or :s/old/new/g for current line.
Merge files: :r filename inserts the file at the cursor.
Split windows: :sp file (horizontal) or :vsp file (vertical). Navigate splits with Ctrl+w followed by h/j/k/l or Ctrl+w Ctrl+w. Resize with Ctrl+w + / -, equalize with =, close with q, close others with o.
Run external commands: :!ls executes ls in the shell.
Vim Configuration
Create a ~/.vimrc file to make settings permanent. Example:
set number " show line numbers
syntax on " enable syntax highlighting
set showcmd " display incomplete commands
set ignorecase " case‑insensitive search
set mouse=a " enable mouse supportCustomize further by adding plugins or additional options.
Conclusion
After reading this article you should have a more complete understanding of Linux and be able to use its core commands, manage processes, handle files, and edit efficiently with Vim.
If you found this useful, please give it a 👍 👍 👍.
References
[1] https://bintray.com/htop/source/download_file?file_path=htop-3.0.0.tar.gz
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
