DEV Community

alok-38
alok-38

Posted on

🐧 Linux Commands Every DevOps Beginner Learns While Deploying to EC2

πŸ” 1. SSH & Remote Access

ssh -i DevOps.pem ec2-user@ec2-x-x-x-x.compute-1.amazonaws.com

Purpose: Connect securely to an EC2 instance.

  • ssh β†’ Secure Shell
  • i DevOps.pem β†’ Use this private key for authentication
  • ec2-user@host β†’ Login user + EC2 hostname

πŸ‘‰ This is how you manually access EC2.


ssh user@host "command"

Purpose: Run a command on EC2 without logging in interactively.

Used in GitHub Actions to:

  • Verify connection

  • Create directories

  • Reload Nginx

Example:

ssh ec2-user@EC2_HOST "whoami && hostname"
Enter fullscreen mode Exit fullscreen mode

ssh-keyscan -H EC2_HOST >> ~/.ssh/known_hosts

Purpose: Trust EC2’s SSH fingerprint automatically.

  • Prevents the β€œAre you sure you want to continue connecting?” prompt

  • Required for non-interactive CI/CD

We later made it safer:

ssh-keyscan -T 10 -H EC2_HOST >> ~/.ssh/known_hosts || true
Enter fullscreen mode Exit fullscreen mode

πŸ“ 2. File System Navigation & Inspection

pwd -- Print current directory path.

ls -- List files and directories

Variants:

  • ls -l β†’ permissions, owner, size

  • ls -a β†’ include hidden files

cd directory - Change directory

Example:

cd DevOpsWeb
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ 3. File & Directory Permissions (VERY IMPORTANT)

chmod - Change permissions

Examples:

chmod 755 directory
chmod 644 file
Enter fullscreen mode Exit fullscreen mode

Meaning:

  • 7 = read + write + execute (owner)
  • 5 = read + execute (group/others)
  • 4 = read only

Used to:

  • Let Nginx read files

  • Prevent 403 Forbidden errors

chown - Change ownership

sudo chown -R ec2-user:nginx /home/ec2-user/DevOpsWeb
Enter fullscreen mode Exit fullscreen mode
  • Owner β†’ ec2-user

  • Group β†’ nginx

  • -R β†’ recursive

πŸ‘‰ This allowed Nginx to read your deployed files.

sudo - Run command as root (admin)

Used for:

  • Installing packages

  • Editing system configs

  • Restarting services

🌐 4. Nginx (Web Server)

sudo yum install nginx -y - Install Nginx on Amazon Linux.

sudo systemctl start nginx - Start the Nginx service.

sudo systemctl enable nginx - Start Nginx automatically on reboot.

sudo systemctl status nginx - Check if Nginx is running and healthy.

sudo nginx -t - Test Nginx configuration for syntax errors.

πŸ‘‰ Always do this before reloading.

sudo systemctl reload nginx - Reload config without downtime.

🌍 5. Networking & Debugging

curl http://localhost

Test if the web server is responding **locally **on EC2.

If this works but browser doesn’t:

  • Security Group issue

  • Firewall issue

curl http://PUBLIC_IP - Test public access from EC2 itself.

lsof -i :80 - Check what is using port 80.

Used to confirm:

  • Nginx is listening

  • Apache was conflicting earlier

πŸ“¦ 6. Package Management

sudo yum install package -y - Install software non-interactively.

Examples:

sudo yum install httpd -y
sudo yum install docker -y
Enter fullscreen mode Exit fullscreen mode

sudo yum remove httpd -y - Remove Apache to avoid port conflicts with Nginx.

🐳 7. Docker Basics (So Far)

docker run hello-world - Verify Docker installation.

docker build -t devops-website - Build Docker image from Dockerfile.

docker ps -a - List all containers (running + stopped).

docker exec -it container command - Run a command inside a running container.

Example:

docker exec -it devopsweb ls /usr/share/nginx/html
Enter fullscreen mode Exit fullscreen mode

sudo usermod -aG docker ec2-user - Allow ec2-user to run Docker without sudo.

Requires logout/login to take effect.

8. Rsync (CI/CD Deployment)

rsync -avz --delete

Efficient file sync over SSH.

  • -a β†’ archive mode (permissions, timestamps)

  • -v β†’ verbose

  • -z β†’ compression

  • --delete β†’ remove files not present in source

Used to deploy:

WSL β†’ GitHub β†’ GitHub Actions β†’ EC2
Enter fullscreen mode Exit fullscreen mode

--exclude
Prevent syncing unwanted files:

--exclude='.git*'
--exclude='.github/'
Enter fullscreen mode Exit fullscreen mode

--chmod
Force permissions on destination:

--chmod=Du=rwx,Dgo=rx,Fu=rw,Fgo=r
Enter fullscreen mode Exit fullscreen mode

This solved exit code 23 and permission issues.

πŸ§ͺ 9. GitHub Actions / CI Commands

set -e - Exit immediately if a command fails.

set -eux

  • -e β†’ exit on error

  • -u β†’ fail on undefined variables

  • -x β†’ print commands (debugging)

Used to make pipelines fail loudly and clearly.

mkdir -p ~/.ssh - Create directory safely (no error if exists).

echo "$SECRET" > ~/.ssh/id_rsa - Write SSH key from GitHub Secrets.

Top comments (0)