KAHIBARO
Discord Login Register

3.2. Linux Basics for Backend Developers

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:

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:

DistributionTypical UsePackage Manager
UbuntuVery popular on cloud serversapt
DebianStable base, often for serversapt
CentOS / Rocky / AlmaEnterprise style serversyum / dnf

As a backend developer you mostly care that:

If you are on:

Navigating the File System

The Linux file system is hierarchical and starts at / (root).

Common important directories:

PathWhat it usually contains
/Root of everything
/homeHome directories for users
/rootHome directory for the root user
/etcConfiguration files
/varVariable data, logs, caches
/usrInstalled programs and libraries
/tmpTemporary files

As a normal user your home directory is usually /home/yourname, often referenced as ~.

Basic navigation commands

Use these in the terminal.

ActionCommand example
Show current directorypwd
List filesls
List with detailsls -l
List including hidden filesls -la
Change directorycd path
Go to home directorycd or cd ~
Go to parent directorycd ..
Go to root directorycd /

Examples:

bash
$ 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:

Working with Files and Directories

You often need to create folders for projects, move files, and inspect their contents.

Creating and removing directories

TaskCommand example
Create directorymkdir my_project
Create nested directoriesmkdir -p app/logs
Remove empty directoryrmdir old_dir
Remove directory and contentrm -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

TaskCommand example
Create empty filetouch app.py
Copy filecp config.example.env .env
Copy directorycp -r src backup_src
Move or renamemv old_name.py new_name.py
Delete filerm old.log
Delete multiplerm *.log

Examples:

bash
# 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 *.pyc

Viewing file contents

TaskCommand example
Print entire filecat file.txt
Show with pagingless file.txt
Show start of filehead file.txt
Show last 10 linestail file.txt
Follow a growing log filetail -f app.log

Examples:

bash
# 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:

Editing Files in the Terminal

On servers you often cannot open a GUI editor. You edit files using a terminal editor.

Common editors:

EditorNotes
nanoSimple, beginner friendly
vimVery powerful, but has a learning curve
viOlder version of vim, available everywhere

For beginners on a remote server, nano is usually easiest.

Basic nano usage

bash
nano config.env

At the bottom you will see shortcuts. ^ means Control:

Example workflow:

bash
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:

bash
ls -l

You might see:

bash
-rw-r--r-- 1 alex devs  1200 Aug 25 10:00 app.py
drwxr-xr-x 2 alex devs  4096 Aug 25 09:58 config

Breakdown of the first item:

Permission string pattern:

Each group of three uses:

So:

For directories:

Typical patterns in backend work:

PatternMeaningExample use
rw-------Only owner can read/writePrivate keys, secrets
rw-r--r--Owner read/write, others readSource files, config examples
rwxr-xr-xExecutable for everyone, only owner can writeScripts, application directories

You rarely need to change permissions as a beginner. If you must:

For example, to make a script executable:

bash
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:

TaskCommand example
Show running processes (detailed)ps aux
Filter by name`ps auxgrep uvicorn`
Interactive process viewertop or htop

Example:

bash
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:

bash
top

Useful in top:

htop is a nicer alternative, but may not be installed by default. You can install it with:

bash
sudo apt update
sudo apt install htop
htop

Stopping processes

To stop a process when you know its PID:

bash
kill PID

Example:

bash
ps aux | grep uvicorn
# Suppose you see a line with PID 12345
kill 12345

If the process does not stop, you can force kill:

bash
kill -9 12345

Use this only when needed, since it does not allow the process to clean up.

Foreground vs background

When you run a command like:

bash
uvicorn main:app --reload

your terminal is occupied by this process.

To run in the background:

bash
uvicorn main:app --reload &

The & sends it to the background.

To view background jobs started in the current shell:

bash
jobs

To bring a job back to the foreground:

bash
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:

In backend work you often use apt on Ubuntu servers, so we focus on that.

Using apt on Ubuntu

Basic pattern:

  1. Update package list:
bash
sudo apt update
  1. Install a package:
bash
sudo apt install PACKAGE_NAME
  1. Upgrade installed packages:
bash
sudo apt upgrade

Examples:

bash
# 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:

ActionShortcut / Command
Show previous commandsPress (up arrow)
Next command in historyPress
Search in historyCtrl + R, then type
Show recent historyhistory

Example:

This is very helpful for repeated commands like long docker or uvicorn invocations.

Autocompletion and tab

Press Tab to:

Example:

Common shortcuts

ActionShortcut
Cancel current commandCtrl + C
Clear current lineCtrl + U
Move to beginning of lineCtrl + A
Move to end of lineCtrl + E
Clear screenCtrl + 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:

Pipes

A pipe | sends the output of one command into another command as input.

Examples:

bash
# 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

TaskCommand example
Write output to a filecommand > file.txt
Append output to a filecommand >> file.txt
Redirect errors to a filecommand 2> errors.log

Examples:

bash
# 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:

bash
find PATH -name "PATTERN"

Examples:

bash
# 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:

bash
grep "STRING" file.txt

More useful flags:

Examples:

bash
# 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" .env

Users, Root, and sudo

Linux is a multi user system. You generally run as a regular user and only use elevated privileges when needed.

Common scenarios in backend work:

To see which user you are:

bash
whoami

To confirm you are not accidentally root:

bash
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:

bash
ssh username@server_address

Examples:

bash
# Using a domain
ssh ubuntu@api.example.com
# Using an IP address
ssh root@203.0.113.42

If a private key is used:

bash
ssh -i /path/to/key.pem ubuntu@api.example.com

Once connected, you are in a shell on the remote machine, and all the commands in this chapter apply there.

Typical workflow:

  1. SSH into server.
  2. Navigate to project directory.
  3. Pull latest code with git.
  4. 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):

bash
# 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 terminal

If 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, and pwd to navigate confidently.
  • Use mkdir, touch, cp, mv, and rm to manage files and directories.
  • Use cat, less, head, and tail to inspect files and logs.
  • Edit files on servers with nano (or vim if you prefer).
  • Understand basic permissions from ls -l output.
  • Use ps, top, kill to inspect and manage processes.
  • Install system tools with sudo apt update and sudo apt install PACKAGE.
  • Chain commands with pipes | and redirect output with > and >>.
  • Use grep and find to search code and config.
  • Avoid running as root and be careful with sudo and destructive commands like rm -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

Comments

Please login to add a comment.

Don't have an account? Register now!