🐧 Linux Commands Every DevOps Beginner Learns While Deploying to EC2

Published: (January 4, 2026 at 12:23 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

🔐 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

Running a command without interactive login

ssh user@host "command"

Purpose: Execute a command on the remote EC2 instance directly (useful in CI/CD pipelines).

Example

ssh ec2-user@EC2_HOST "whoami && hostname"

Managing known hosts for non‑interactive CI/CD

ssh-keyscan -H EC2_HOST >> ~/.ssh/known_hosts
  • Prevents the “Are you sure you want to continue connecting?” prompt.
  • Required for automated scripts.

A safer variant (adds a timeout and never fails the pipeline):

ssh-keyscan -T 10 -H EC2_HOST >> ~/.ssh/known_hosts || true

📁 File System Navigation & Inspection

pwd                # Print current directory
ls                 # List files and directories
ls -l              # Long format (permissions, owner, size)
ls -a              # Include hidden files
cd                 # Change directory

Example

cd DevOpsWeb

🔑 File & Directory Permissions (VERY IMPORTANT)

Changing permissions

chmod 755 directory
chmod 644 file
  • 7 = read + write + execute (owner)
  • 5 = read + execute (group/others)
  • 4 = read only

Typical uses

  • Allow Nginx to read files
  • Prevent 403 Forbidden errors

Changing ownership

sudo chown -R ec2-user:nginx /home/ec2-user/DevOpsWeb
  • Owner → ec2-user
  • Group → nginx
  • -R → recursive

Running commands as root

sudo runs a command with administrative privileges and is needed for:

  • Installing packages
  • Editing system configurations
  • Restarting services

🌐 Nginx (Web Server)

sudo yum install nginx -y          # Install
sudo systemctl start nginx        # Start service
sudo systemctl enable nginx        # Enable on boot
sudo systemctl status nginx       # Check status
sudo nginx -t                     # Test configuration (run before reload)
sudo systemctl reload nginx       # Reload config without downtime

🌍 Networking & Debugging

curl http://localhost               # Test local web server response
curl http://PUBLIC_IP               # Test public access from the instance
lsof -i :80                         # See what process is using port 80

If curl http://localhost works but the browser cannot reach the site, check:

  • Security Group rules
  • Instance firewall settings

📦 Package Management (Amazon Linux)

sudo yum install  -y       # Install a package non‑interactively
sudo yum remove  -y        # Remove a package

Examples

sudo yum install httpd -y
sudo yum install docker -y
sudo yum remove httpd -y            # Remove Apache to avoid port conflicts

🐳 Docker Basics (So Far)

docker run hello-world                     # Verify Docker installation
docker build -t devops-website .           # Build image from Dockerfile
docker ps -a                               # List all containers
docker exec -it <container> <command>      # Run a command inside a running container

Example

docker exec -it devopsweb ls /usr/share/nginx/html

Allow the ec2-user to run Docker without sudo:

sudo usermod -aG docker ec2-user
# Log out and back in for the change to take effect

8️⃣ Rsync (CI/CD Deployment)

rsync -avz --delete source/ user@EC2_HOST:/path/to/dest/
  • -a – archive mode (preserves permissions, timestamps, etc.)
  • -v – verbose
  • -z – compression
  • --delete – remove files on the destination that no longer exist in the source

Excluding unwanted files

--exclude='.git*'
--exclude='.github/'

Forcing permissions on the destination

--chmod=Du=rwx,Dgo=rx,Fu=rw,Fgo=r

These options helped resolve exit code 23 and permission‑related issues during deployments.


🧪 GitHub Actions / CI Commands

set -e          # Exit immediately if a command fails
set -eux        # -e: exit on error, -u: treat unset variables as error, -x: print commands (debug)
mkdir -p ~/.ssh
echo "$SECRET" > ~/.ssh/id_rsa   # Write SSH private key from GitHub Secrets

These settings make CI pipelines fail loudly and clearly, ensuring problems are caught early.

Back to Blog

Related posts

Read more »

How to Deploy AWS ELASTIC BEANSTALK

Overview AWS Elastic Beanstalk is a managed cloud service that lets you deploy and run web applications without worrying about the underlying infrastructure. I...