> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ahmadraza.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Setup

# Amazon Linux Server Management Guide

## 1. Introduction

Amazon Linux is a Linux distribution provided by AWS that is optimized for use in the Amazon Web Services cloud environment. This guide will help you manage an Amazon Linux server effectively, from basic operations to troubleshooting.

## 2. Updating and Upgrading the System

### 2.1. Checking for Updates

Before updating, it's important to check for available updates.

```bash theme={null}
sudo yum check-update
```

### 2.2. Updating the System

To update all installed packages to their latest versions, use:

```bash theme={null}
sudo yum update -y
```

### 2.3. Upgrading the System

Amazon Linux 2 does not have a traditional "upgrade" command like Debian-based systems. Instead, keeping the system updated as shown above ensures you're running the latest version of packages.

## 3. Installing Packages

### 3.1. Installing a Package

To install a specific package, use the `yum install` command:

```bash theme={null}
sudo yum install <package_name> -y
```

Example:

```bash theme={null}
sudo yum install httpd -y
```

### 3.2. Searching for a Package

To search for a package, use:

```bash theme={null}
yum search <package_name>
```

### 3.3. Removing a Package

To remove a package, use the `yum remove` command:

```bash theme={null}
sudo yum remove <package_name> -y
```

## 4. Basic Commands

### 4.1. Navigating the File System

* **List files and directories**:
  ```bash theme={null}
  ls
  ```
* **Change directory**:
  ```bash theme={null}
  cd /path/to/directory
  ```
* **Print the current working directory**:
  ```bash theme={null}
  pwd
  ```

### 4.2. File Operations

* **Create a file**:
  ```bash theme={null}
  touch filename.txt
  ```
* **View file contents**:
  ```bash theme={null}
  cat filename.txt
  ```
* **Copy a file**:
  ```bash theme={null}
  cp source.txt destination.txt
  ```
* **Move or rename a file**:
  ```bash theme={null}
  mv oldname.txt newname.txt
  ```
* **Delete a file**:
  ```bash theme={null}
  rm filename.txt
  ```

### 4.3. Managing Users and Groups

* **Add a new user**:
  ```bash theme={null}
  sudo useradd username
  ```
* **Set a password for a user**:
  ```bash theme={null}
  sudo passwd username
  ```
* **Delete a user**:
  ```bash theme={null}
  sudo userdel username
  ```
* **Add a user to a group**:
  ```bash theme={null}
  sudo usermod -aG groupname username
  ```

## 5. System Monitoring and Troubleshooting

### 5.1. System Resource Monitoring

* **Check system uptime**:
  ```bash theme={null}
  uptime
  ```
* **View running processes**:
  ```bash theme={null}
  top
  ```
* **Check memory usage**:
  ```bash theme={null}
  free -m
  ```
* **Check disk usage**:
  ```bash theme={null}
  df -h
  ```

### 5.2. Networking Commands

* **Check network interfaces**:
  ```bash theme={null}
  ifconfig
  ```
* **Test network connectivity**:
  ```bash theme={null}
  ping <hostname_or_IP>
  ```
* **Display routing table**:
  ```bash theme={null}
  netstat -rn
  ```

### 5.3. Log Management

* **View system logs**:
  ```bash theme={null}
  sudo tail -f /var/log/messages
  ```
* **View SSH logs**:
  ```bash theme={null}
  sudo tail -f /var/log/secure
  ```
* **View Apache logs**:
  ```bash theme={null}
  sudo tail -f /var/log/httpd/access_log
  sudo tail -f /var/log/httpd/error_log
  ```

### 5.4. Service Management

* **Start a service**:
  ```bash theme={null}
  sudo systemctl start <service_name>
  ```
* **Stop a service**:
  ```bash theme={null}
  sudo systemctl stop <service_name>
  ```
* **Restart a service**:
  ```bash theme={null}
  sudo systemctl restart <service_name>
  ```
* **Enable a service to start on boot**:
  ```bash theme={null}
  sudo systemctl enable <service_name>
  ```

## 6. Security Best Practices

### 6.1. Managing Firewalls

Amazon Linux uses `iptables` or `firewalld` for managing firewall rules.

* **Check firewall status**:
  ```bash theme={null}
  sudo systemctl status firewalld
  ```
* **Start/Stop/Restart the firewall**:
  ```bash theme={null}
  sudo systemctl start firewalld
  sudo systemctl stop firewalld
  sudo systemctl restart firewalld
  ```

### 6.2. Securing SSH Access

* **Change the default SSH port**:
  Edit the SSH configuration file `/etc/ssh/sshd_config` and change the `Port` directive.

* **Disable root login via SSH**:
  In the SSH configuration file, set `PermitRootLogin no`.

* **Restart SSH service**:
  ```bash theme={null}
  sudo systemctl restart sshd
  ```

## 7. Automating Tasks with Cron Jobs

### 7.1. Creating a Cron Job

Cron jobs are used to schedule tasks on the server.

```bash theme={null}
crontab -e
```

Add the job in the following format:

```bash theme={null}
* * * * * /path/to/script.sh
```

### 7.2. Viewing Scheduled Cron Jobs

```bash theme={null}
crontab -l
```

### 7.3. Removing a Cron Job

```bash theme={null}
crontab -r
```

## 8. Package Management with EPEL and Remi Repositories

### 8.1. Enabling EPEL Repository

EPEL (Extra Packages for Enterprise Linux) provides additional packages for Amazon Linux.

```bash theme={null}
sudo amazon-linux-extras install epel -y
```

### 8.2. Enabling Remi Repository

The Remi repository is used to install PHP and other packages.

```bash theme={null}
sudo amazon-linux-extras install epel
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
```

## 9. Managing System Backups

### 9.1. Creating a Backup with `tar`

The `tar` command is used to create compressed archives.

```bash theme={null}
tar -czvf backup.tar.gz /path/to/directory
```

### 9.2. Restoring a Backup

```bash theme={null}
tar -xzvf backup.tar.gz
```

## 10. Additional Resources

* **Official Amazon Linux Documentation**: [Amazon Linux 2 Documentation](https://docs.aws.amazon.com/linux/)
* **AWS CLI Documentation**: [AWS CLI Command Reference](https://docs.aws.amazon.com/cli/latest/reference/)
* **Systemd Cheat Sheet**: [Systemd Commands](https://linuxconfig.org/systemd-system-administration-cheat-sheet)

***

> This documentation should serve as a comprehensive guide to managing an Amazon Linux server, covering essential tasks and best practices.
