3.2. Linux Basics for Backend Developers
Table of Contents
Why Linux Matters for Backend Developers
Linux is the most common operating system on servers. Most cloud machines, containers, and production backends run on some Linux distribution. Even if you develop on Windows or macOS, you will deploy to Linux most of the time.
In this chapter you will not become a Linux expert, but you will learn enough to:
- Connect to a Linux server
- Move around the file system
- Inspect and edit files
- Manage processes
- Install basic tools
- Work safely without breaking the system
Later chapters on deployment, Docker, and servers will assume you know these basics.
Linux Distributions and Terminals
Linux is a kernel. On top of the kernel, different companies and communities build complete systems called distributions.
Common server oriented distributions:
| Distribution | Typical Use | Package Manager |
|---|---|---|
| Ubuntu | Very popular on cloud servers | apt |
| Debian | Stable base, often for servers | apt |
| CentOS / Rocky / Alma | Enterprise style servers | yum / dnf |
As a backend developer you mostly care that:
- Commands are typed into a terminal (also called shell).
- Most commands are the same between distributions.
- Package managers differ a bit (
aptvsdnf), but their concepts are similar.
If you are on:
- Linux: you already have a terminal.
- macOS: use Terminal or iTerm2.
- Windows: use WSL (Windows Subsystem for Linux) and then Ubuntu in WSL, or Git Bash / a remote Linux server.
Navigating the File System
The Linux file system is hierarchical and starts at / (root).
Common important directories:
| Path | What it usually contains |
|---|---|
/ | Root of everything |
/home | Home directories for users |
/root | Home directory for the root user |
/etc | Configuration files |
/var | Variable data, logs, caches |
/usr | Installed programs and libraries |
/tmp | Temporary files |
As a normal user your home directory is usually /home/yourname, often referenced as ~.
Basic navigation commands
Use these in the terminal.
| Action | Command example |
|---|---|
| Show current directory | pwd |
| List files | ls |
| List with details | ls -l |
| List including hidden files | ls -la |
| Change directory | cd path |
| Go to home directory | cd or cd ~ |
| Go to parent directory | cd .. |
| Go to root directory | cd / |
Examples:
$ pwd
/home/alex
$ ls
projects notes.txt
$ cd projects
$ pwd
/home/alex/projects
$ cd ..
$ pwd
/home/alex
Hidden files start with a dot, like .env or .gitignore. Use ls -a or ls -la to see them.
Relative vs absolute paths:
- Absolute: starts with
/and describes the full path, for example/home/alex/projects/app. - Relative: starts from the current directory, for example
appor../logs.
Working with Files and Directories
You often need to create folders for projects, move files, and inspect their contents.
Creating and removing directories
| Task | Command example |
|---|---|
| Create directory | mkdir my_project |
| Create nested directories | mkdir -p app/logs |
| Remove empty directory | rmdir old_dir |
| Remove directory and content | rm -r old_project |
mkdir -p creates parent directories if they do not exist.
Be very careful with rm -r since it deletes everything in that directory recursively.
rm -rf / or rm -rf * in the wrong place can destroy your system or project.
Never run destructive commands unless you are sure about the current directory (pwd) and the pattern.
Creating, copying, moving, and deleting files
| Task | Command example |
|---|---|
| Create empty file | touch app.py |
| Copy file | cp config.example.env .env |
| Copy directory | cp -r src backup_src |
| Move or rename | mv old_name.py new_name.py |
| Delete file | rm old.log |
| Delete multiple | rm *.log |
Examples:
# Create project folder and an empty main.py
mkdir my_api
cd my_api
touch main.py
# Copy sample env file
cp .env.example .env
# Rename a file
mv main.py app.py
# Remove all .pyc files
rm *.pycViewing file contents
| Task | Command example |
|---|---|
| Print entire file | cat file.txt |
| Show with paging | less file.txt |
| Show start of file | head file.txt |
| Show last 10 lines | tail file.txt |
| Follow a growing log file | tail -f app.log |
Examples:
# View a short config file
cat .env
# View a long log with paging
less /var/log/syslog
# See logs as they are written
tail -f app.log
In less:
- Press
qto quit. - Use arrow keys or
PageUp/PageDownto scroll.
Editing Files in the Terminal
On servers you often cannot open a GUI editor. You edit files using a terminal editor.
Common editors:
| Editor | Notes |
|---|---|
nano | Simple, beginner friendly |
vim | Very powerful, but has a learning curve |
vi | Older version of vim, available everywhere |
For beginners on a remote server, nano is usually easiest.
Basic nano usage
nano config.env
At the bottom you will see shortcuts. ^ means Control:
Ctrl + Oto write (save) the file.- Press Enter to confirm the filename.
Ctrl + Xto exit.
Example workflow:
cd my_api
nano .env
# type:
# DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
# DEBUG=true
# then:
# Ctrl + O, Enter, Ctrl + X
Later, when you are more comfortable, you can invest some time to learn vim, but for backend work nano is enough to start.
Understanding Permissions and Ownership
Linux controls access to files with owners, groups, and permission bits.
Run:
ls -lYou might see:
-rw-r--r-- 1 alex devs 1200 Aug 25 10:00 app.py
drwxr-xr-x 2 alex devs 4096 Aug 25 09:58 configBreakdown of the first item:
-rw-r--r--permission stringalexownerdevsgroupapp.pyfilename
Permission string pattern:
- First character: type,
-for file,dfor directory. - Next three: owner permissions.
- Next three: group permissions.
- Last three: others permissions.
Each group of three uses:
rreadwwritexexecute (or "can enter" for directories)-means permission not granted
So:
-rw-r--r--means:- Owner: read, write
- Group: read
- Others: read
For directories:
- Execute means "can enter and access contents".
Typical patterns in backend work:
| Pattern | Meaning | Example use |
|---|---|---|
rw------- | Only owner can read/write | Private keys, secrets |
rw-r--r-- | Owner read/write, others read | Source files, config examples |
rwxr-xr-x | Executable for everyone, only owner can write | Scripts, application directories |
You rarely need to change permissions as a beginner. If you must:
chmodchanges permissions.chownchanges owner and group.
For example, to make a script executable:
chmod +x deploy.sh
Be careful with chmod -R and chown -R since they change everything recursively.
Processes and System Monitoring
A process is a running program. On a server, your backend (for example a FastAPI app) is a process, databases are processes, and so on.
Listing processes
Basic tools:
| Task | Command example | |
|---|---|---|
| Show running processes (detailed) | ps aux | |
| Filter by name | `ps aux | grep uvicorn` |
| Interactive process viewer | top or htop |
Example:
ps aux | grep "python"This shows processes with "python" in the line. The number after the username is usually the PID (process ID).
top shows a live view:
top
Useful in top:
- Check CPU and memory usage.
- Press
qto quit.
htop is a nicer alternative, but may not be installed by default. You can install it with:
sudo apt update
sudo apt install htop
htopStopping processes
To stop a process when you know its PID:
kill PIDExample:
ps aux | grep uvicorn
# Suppose you see a line with PID 12345
kill 12345If the process does not stop, you can force kill:
kill -9 12345Use this only when needed, since it does not allow the process to clean up.
Foreground vs background
When you run a command like:
uvicorn main:app --reloadyour terminal is occupied by this process.
To run in the background:
uvicorn main:app --reload &
The & sends it to the background.
To view background jobs started in the current shell:
jobsTo bring a job back to the foreground:
fg %1
For real production you will use systemd, Docker, or process managers. Backgrounding with & is only for quick tests.
Managing Software with Package Managers
On Linux you install system level tools using the distribution's package manager.
Common ones:
- Debian / Ubuntu:
apt - CentOS / Rocky:
yumordnf
In backend work you often use apt on Ubuntu servers, so we focus on that.
Using apt on Ubuntu
Basic pattern:
- Update package list:
sudo apt update- Install a package:
sudo apt install PACKAGE_NAME- Upgrade installed packages:
sudo apt upgradeExamples:
# Install Python 3 and pip
sudo apt update
sudo apt install python3 python3-pip
# Install git
sudo apt install git
# Install PostgreSQL client tools
sudo apt install postgresql-client
sudo runs the command as an administrator (root). You will be asked for your password.
Only use sudo when necessary.
Never run random sudo commands copied from the internet without understanding them. They can modify or break your system.
Remember that apt installs system wide tools. Python dependencies for a project are usually installed via pip inside a virtual environment, which you will learn in the Python and environment chapters.
Using the Shell Efficiently
The shell (often bash or zsh) is the program that interprets the commands you type.
History and reusing commands
Useful shortcuts:
| Action | Shortcut / Command |
|---|---|
| Show previous commands | Press ↑ (up arrow) |
| Next command in history | Press ↓ |
| Search in history | Ctrl + R, then type |
| Show recent history | history |
Example:
- Press
Ctrl + R, typeuvicorn, and you will see the last command containinguvicorn. Press Enter to run it again.
This is very helpful for repeated commands like long docker or uvicorn invocations.
Autocompletion and tab
Press Tab to:
- Auto complete file and directory names.
- Auto complete commands and options, if configured.
Example:
- Type
cd my_pand pressTab. If there is onlymy_project, it becomescd my_project.
Common shortcuts
| Action | Shortcut |
|---|---|
| Cancel current command | Ctrl + C |
| Clear current line | Ctrl + U |
| Move to beginning of line | Ctrl + A |
| Move to end of line | Ctrl + E |
| Clear screen | Ctrl + L or clear |
Ctrl + C is also how you stop a running foreground process, for example stopping a development server.
Streams, Pipes, and Redirection
Many Linux commands produce text output and read input. As a backend developer you often chain commands to filter logs or search configurations.
There are three standard streams:
stdinstandard inputstdoutstandard outputstderrstandard error
Pipes
A pipe | sends the output of one command into another command as input.
Examples:
# Show only lines containing ERROR in app.log
cat app.log | grep "ERROR"
# List processes, then filter for python
ps aux | grep python
# See last 100 lines of a log and filter for "INFO"
tail -n 100 app.log | grep "INFO"
grep searches for lines matching a pattern.
Redirecting output to files
| Task | Command example |
|---|---|
| Write output to a file | command > file.txt |
| Append output to a file | command >> file.txt |
| Redirect errors to a file | command 2> errors.log |
Examples:
# Save the output of a command to a file (overwrite)
ls -la > files.txt
# Append info to a log file
echo "Started at $(date)" >> app.log
# Save only errors from a command
python script.py 2> errors.log
> overwrites the file, >> appends.
Finding Files and Searching Inside Files
In real projects you often search for configuration files or where a certain variable is used in the code.
Finding files by name
Use find:
find PATH -name "PATTERN"Examples:
# Find all .py files under current directory
find . -name "*.py"
# Find a specific file by name
find . -name "settings.py"
. refers to the current directory.
Searching inside files
grep is very handy for finding strings in files.
Common usage:
grep "STRING" file.txtMore useful flags:
-rrecursive-nshow line numbers-icase insensitive
Examples:
# Search recursively for "DATABASE_URL" in current directory
grep -rn "DATABASE_URL" .
# Case insensitive search for "error" in logs directory
grep -rni "error" logs/
# Search for "SECRET_KEY" inside .env file
grep "SECRET_KEY" .envUsers, Root, and sudo
Linux is a multi user system. You generally run as a regular user and only use elevated privileges when needed.
- Do not run as
rootfor daily development. - Use
sudoonly for administration tasks like installing packages or editing system configs.
Common scenarios in backend work:
- Use
sudo apt installto install system tools. - Use
sudo nano /etc/nginx/nginx.confto edit web server config. - Use your regular user for coding and running development servers.
To see which user you are:
whoamiTo confirm you are not accidentally root:
id
If the uid is 0, you are root. Do not stay logged in as root unless you know exactly what you are doing.
SSH: Accessing Remote Linux Servers
In deployment scenarios you usually log into a remote Linux server via SSH (Secure Shell).
Basic command:
ssh username@server_addressExamples:
# Using a domain
ssh ubuntu@api.example.com
# Using an IP address
ssh root@203.0.113.42If a private key is used:
ssh -i /path/to/key.pem ubuntu@api.example.comOnce connected, you are in a shell on the remote machine, and all the commands in this chapter apply there.
Typical workflow:
- SSH into server.
- Navigate to project directory.
- Pull latest code with
git. - Restart services or Docker containers.
SSH specifics, keys, and security will be covered in deployment related chapters.
Practical Mini Session: From Zero to Simple Folder
Here is a small exercise you can try on any Linux shell (local or remote):
# 1. Check who you are and where you are
whoami
pwd
# 2. Go to your home directory
cd ~
# 3. Create a new project folder and enter it
mkdir my_first_backend
cd my_first_backend
# 4. Create some files and folders
mkdir src logs
touch src/main.py
touch .env
# 5. List files with details, including hidden ones
ls -la
# 6. Open and edit .env with nano
nano .env
# Add a line like:
# DEBUG=true
# Save and exit (Ctrl+O, Enter, Ctrl+X)
# 7. View the contents with cat
cat .env
# 8. Simulate a log file and watch it
echo "App started" >> logs/app.log
tail -f logs/app.log
# Open another terminal, append something:
# echo "New request" >> logs/app.log
# See it appear in the first terminalIf you can do this comfortably, you already know the core of what you need for daily backend work in a Linux environment.
Summary
Key things to remember:
- Use
cd,ls, andpwdto navigate confidently. - Use
mkdir,touch,cp,mv, andrmto manage files and directories. - Use
cat,less,head, andtailto inspect files and logs. - Edit files on servers with
nano(orvimif you prefer). - Understand basic permissions from
ls -loutput. - Use
ps,top,killto inspect and manage processes. - Install system tools with
sudo apt updateandsudo apt install PACKAGE. - Chain commands with pipes
|and redirect output with>and>>. - Use
grepandfindto search code and config. - Avoid running as root and be careful with
sudoand destructive commands likerm -rf.
These skills will be used again and again when you configure databases, run application servers, deploy Docker containers, and debug production issues.
Views: 10
KAHIBARO