Skip to content

linux

4 posts with the tag “linux”

Automating Homelab Tasks with Cron

In a homelab, there are many tasks that benefit from automation: backups, log rotation, system updates, or even just checking the status of services. cron is the classic Unix utility for scheduling commands to run periodically. This post will guide you through setting up and managing cron jobs.

A cron job entry (a “crontab” entry) consists of five time fields followed by the command to be executed. The fields are:

* * * * * command-to-be-executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

For common schedules, cron offers special strings:

StringDescriptionEquivalent
@rebootRun once after rebootN/A
@yearlyRun once a year0 0 1 1 *
@annuallySame as @yearly0 0 1 1 *
@monthlyRun once a month0 0 1 * *
@weeklyRun once a week0 0 * * 0
@dailyRun once a day0 0 * * *
@midnightSame as @daily0 0 * * *
@hourlyRun once an hour0 * * * *

Each user has their own crontab. To edit it, use:

Terminal window
crontab -e

This will open your crontab in a text editor (usually nano or vim). Add your job entries at the end of the file.

To view your current cron jobs:

Terminal window
crontab -l

To remove all your cron jobs:

Terminal window
crontab -r

Let’s say you have a backup script at /usr/local/bin/backup-homelab.sh. To run it every day at 2:00 AM:

0 2 * * * /usr/local/bin/backup-homelab.sh >> /var/log/homelab-backup.log 2>&1
  • 0: Minute 0
  • 2: Hour 2 (2 AM)
  • *: Every day of the month
  • *: Every month
  • *: Every day of the week
  • >> /var/log/homelab-backup.log 2>&1: Redirects both standard output and standard error to a log file.

To check for system updates every hour:

0 * * * * sudo apt update > /dev/null 2>&1
  • 0: Minute 0
  • *: Every hour
  • > /dev/null 2>&1: Discards all output to prevent excessive emails from cron.

Run a script once after every system reboot:

@reboot /usr/local/bin/start-homelab-services.sh

Cron jobs run with a minimal set of environment variables. If your script relies on specific PATH settings or other variables, you need to define them within the crontab or explicitly in your script.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 3 * * * /usr/local/bin/my-script.sh

Alternatively, specify the full path to all commands in your script.

IssuePossible CauseSolution
Job not runningIncorrect time syntax, script pathCheck crontab -l, verify script path
Job fails silentlyEnvironment variables, permissionsRedirect output to a log file, check script permissions (chmod +x)
No outputOutput redirected to /dev/nullRemove redirection temporarily to debug
Email spamJob produces outputRedirect output to /dev/null or a log file

Cron activity is often logged in /var/log/syslog or /var/log/cron.

Terminal window
sudo grep CRON /var/log/syslog
  1. Use full paths: Always specify the full path to commands and scripts (e.g., /usr/bin/python3 instead of python3).
  2. Log output: Redirect output to a log file (>> /path/to/log.log 2>&1) to capture errors and verify execution.
  3. Test scripts: Run your scripts manually from the command line before adding them to cron.
  4. Minimal privileges: Run cron jobs as the user with the least necessary privileges.
  5. Comments: Add comments to your crontab (# This is a comment) to explain complex entries.

cron is a robust and reliable tool for automating tasks on your homelab. By understanding its syntax and following best practices, you can free yourself from repetitive manual work and ensure your systems are maintained efficiently.

Automate responsibly!

Essential Linux Commands for Developers

Working in software development often means spending a lot of time in the terminal, especially on Linux systems. Mastering a few essential commands can significantly boost your productivity. Here’s a quick reference guide to some of the most useful ones.

CommandDescriptionExample Usage
lsList directory contentsls -la (long format, all files)
cdChange directorycd ../projects/my-app
pwdPrint working directorypwd
mkdirCreate directorymkdir new-feature
rmRemove files or directoriesrm -rf old-logs/ (force, recursive)
cpCopy files or directoriescp -r src/ build/
mvMove or rename files/directoriesmv old-name.txt new-name.md
findSearch for files in a directory hierarchyfind . -name "*.js"
CommandDescriptionExample Usage
catConcatenate and display file contentcat README.md
lessView file content page by pageless /var/log/syslog
headDisplay first N lines of a filehead -n 10 config.yaml
tailDisplay last N lines of a filetail -f access.log (follow changes)
grepSearch for patterns in filesgrep -r "error" . (recursive search)
nano / vimText editorsnano script.sh
CommandDescriptionExample Usage
psReport a snapshot of current processes`ps aux
top / htopDisplay Linux processes dynamicallyhtop (more user-friendly)
killSend a signal to processeskill 12345 (terminate process ID 12345)
killallKill processes by namekillall node
systemctlControl the systemd system and service managersudo systemctl status nginx
CommandDescriptionExample Usage
dfReport file system disk space usagedf -h (human-readable)
duEstimate file space usagedu -sh my-project/ (summary, human-readable)
freeDisplay amount of free and used memoryfree -h
unamePrint system informationuname -a (all information)
lscpuDisplay CPU architecture informationlscpu
CommandDescriptionExample Usage
ip addrDisplay IP addresses and network interfacesip addr show eth0
pingSend ICMP ECHO_REQUEST to network hostsping google.com
curlTransfer data from or to a servercurl -I https://codevibr.dev (show headers)
wgetNon-interactive network downloaderwget https://example.com/file.zip
sshOpenSSH remote login clientssh user@remote-host

Understanding file permissions is crucial for security and proper application function.

Terminal window
# View permissions
ls -l my-file.sh
# Change permissions (read, write, execute for owner, group, others)
chmod 755 my-file.sh
# Change ownership
sudo chown user:group my-file.sh
CommandDescriptionExample Usage
apt updateRefresh package listssudo apt update
apt upgradeUpgrade installed packagessudo apt upgrade -y
apt installInstall new packagessudo apt install git
apt removeRemove packagessudo apt remove htop

While not strictly a Linux command, Git is indispensable for developers.

Terminal window
# Initialize a new repository
git init
# Clone a repository
git clone https://github.com/user/repo.git
# Check status
git status
# Add changes to staging area
git add .
# Commit changes
git commit -m "feat: add new feature"
# Push changes to remote
git push origin main

This is just a starting point. The Linux terminal is a powerful environment, and there’s always more to learn. The best way to get comfortable is to use it daily. Experiment, read man pages (man <command>), and don’t be afraid to break things in a safe environment.

Happy coding!

Homelab Networking Basics

Networking can seem daunting, but for a homelab, understanding a few core concepts will get you a long way. This post covers the essentials: IP addresses, subnets, and basic firewall rules.

An IP address is like a street address for your device on a network. There are two main types you’ll encounter:

  • IPv4: The most common, looks like 192.168.1.100. It’s a 32-bit number.
  • IPv6: The newer standard, looks like 2001:0db8:85a3:0000:0000:8a2e:0370:7334. It’s a 128-bit number, designed to address the exhaustion of IPv4 addresses.

Most homelabs still primarily use IPv4, often with private IP address ranges (e.g., 192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8) that are not routable on the public internet.

On a Linux machine, you can check your IP address with ip addr:

Terminal window
ip addr show eth0

Example output:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86077sec preferred_lft 86077sec
inet6 fe80::211:22ff:fe33:4455/64 scope link
valid_lft forever preferred_lft forever

Here, 192.168.1.100 is the IPv4 address, and /24 indicates the subnet mask.

A subnet divides a larger network into smaller, more manageable segments. The subnet mask determines which part of an IP address identifies the network and which part identifies the host.

IP AddressSubnet MaskNetwork AddressHost Address
192.168.1.100255.255.255.0192.168.1.00.0.0.100
10.0.0.50255.255.0.010.0.0.00.0.0.50

In CIDR notation, /24 means the first 24 bits are for the network, and the remaining 8 bits are for hosts. This allows for 254 usable IP addresses (2^8 - 2).

  • Organization: Group devices logically (e.g., servers, IoT, guests).
  • Security: Isolate sensitive devices from less secure ones.
  • Performance: Reduce broadcast traffic within segments.

A firewall controls incoming and outgoing network traffic based on predefined security rules. For Linux, ufw (Uncomplicated Firewall) is a user-friendly interface for iptables.

Terminal window
# Deny all incoming, allow all outgoing by default
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow SSH (port 22)
sudo ufw allow ssh
# Allow HTTP (port 80) and HTTPS (port 443)
sudo ufw allow http
sudo ufw allow https
# Enable UFW
sudo ufw enable
# Check status
sudo ufw status verbose

Let’s say you have a web server running on your homelab at 192.168.1.10 and you want to allow external access to it on ports 80 and 443.

Terminal window
sudo ufw allow from any to 192.168.1.10 port 80 proto tcp
sudo ufw allow from any to 192.168.1.10 port 443 proto tcp
sudo ufw reload

This explicitly allows traffic to those ports on that specific IP address.

With these basics, you can start planning your homelab network. Consider drawing a simple network diagram to visualize your devices and their IP addresses. As you grow, you might explore:

  • VLANs for advanced network segmentation
  • VPNs for secure remote access
  • DNS servers for local name resolution

Happy homelabbing!