Linux Basics for Network Automation
![]()
Linux is the foundation of most network automation tools. Understanding basic Linux commands and concepts is essential for anyone starting with network automation.
Why Linux for Network Automation?
- Most automation tools run on Linux
- Command-line efficiency for automation workflows
- Better integration with development tools
- Cost-effective and open-source
Essential Commands for Network Automation
File and Directory Management
# Navigate directories
cd /path/to/directory # Change directory
pwd # Print working directory
ls -la # List files with details
# Create and manage files
mkdir automation # Create directory
touch playbook.yml # Create empty file
cp source dest # Copy files
mv old_name new_name # Move/rename files
rm filename # Remove file
rm -rf directory # Remove directory recursively
File Editing
# Text editors (choose one)
nano playbook.yml # Simple editor for beginners
vim playbook.yml # Advanced editor
code playbook.yml # Visual Studio Code (if installed)
File Permissions
# View permissions
ls -la
# Change permissions
chmod +x script.sh # Make executable
chmod 600 private_key # Secure private key
chown user:group file # Change ownership
Package Management
# Ubuntu/Debian
sudo apt update
sudo apt install package_name
# CentOS/RHEL
sudo yum install package_name
# or
sudo dnf install package_name
# Python packages
pip install ansible
pip install --user ansible # Install for current user only
Process Management
# View running processes
ps aux | grep ansible
top # Interactive process viewer
htop # Enhanced top (if installed)
# Kill processes
kill process_id
killall process_name
Network Commands
# Network connectivity
ping 192.168.1.1
ssh user@hostname
scp file user@hostname:/path/
wget https://example.com/file
curl https://api.example.com/data
# Network information
ip addr show
ip route show
netstat -tuln
ss -tuln
Environment and Variables
# View environment variables
env
echo $PATH
echo $HOME
# Set variables
export ANSIBLE_HOST_KEY_CHECKING=False
export EDITOR=nano
# Persistent variables (add to ~/.bashrc)
echo 'export ANSIBLE_HOST_KEY_CHECKING=False' >> ~/.bashrc
source ~/.bashrc
Working with Text Files
Viewing Files
# View entire file
cat filename
# View file page by page
less filename
more filename
# View beginning/end of file
head -20 filename
tail -20 filename
tail -f logfile # Follow log file in real-time
Searching in Files
# Search for text in files
grep "search_term" filename
grep -r "search_term" directory/ # Recursive search
grep -i "search_term" filename # Case insensitive
# Find files
find . -name "*.yml" # Find YAML files
find . -type f -name "*.py" # Find Python files
Shell Scripting Basics
Simple Script Example
#!/bin/bash
# This is a comment
# Variables
HOSTNAME="192.168.1.1"
USERNAME="admin"
# Commands
echo "Connecting to $HOSTNAME..."
ssh $USERNAME@$HOSTNAME "show version"
Make Script Executable
Virtual Environments
Python Virtual Environment
# Create virtual environment
python3 -m venv automation_env
# Activate virtual environment
source automation_env/bin/activate # Linux/macOS
# automation_env\Scripts\activate # Windows
# Deactivate
deactivate
# Install packages in virtual environment
pip install ansible
pip install -r requirements.txt
File System Navigation
Common Directories
/home/username/ # User home directory
/etc/ # System configuration
/var/log/ # Log files
/tmp/ # Temporary files
/usr/local/bin/ # User-installed programs
Path Management
# Add directory to PATH
export PATH=$PATH:/path/to/scripts
# Check if command exists
which ansible
whereis ansible
Best Practices
- Use Tab Completion: Press Tab to auto-complete commands and filenames
- Use History: Press Up/Down arrows to navigate command history
- Use Aliases: Create shortcuts for common commands
- Backup Important Files: Always backup before making changes
- Use Absolute Paths: When in doubt, use full paths
Common Aliases for Network Automation
Add these to your ~/.bashrc:
# Network automation aliases
alias ansible-playbook='ansible-playbook -i inventory'
alias ansible-inventory='ansible-inventory --list -i inventory'
alias ansible-doc='ansible-doc -t module'
# Quick navigation
alias ll='ls -la'
alias ..='cd ..'
alias ...='cd ../..'
Learn More
Next Steps
Once you're comfortable with Linux basics, explore: - Ansible Introduction & Getting Started - Visual Studio Code for Network Automation - Git Basics for Version Control