Controlling servers from a distance is an extremely important job for system admins, users, and programmers. It guarantees that the servers are operating correctly, and safely, and are current with the newest software updates. A highly effective tool for handling remote servers is the Ubuntu terminal. This guide will take you through the process of setting up, overseeing, and keeping remote servers in good shape, offering you key commands and top tips.
Table of Content
Syntax for Setting-Up SSH (Secure Shell)
STEP 1: Installing SSH on Ubuntu
To start managing a remote server, A user needs SSH installed in their OS or Ubuntu System. Then run the following commands:
sudo apt updatesudo apt install openssh-serverSTEP 2: Configuring SSH Server
After installation, Make sure the SSH server is running perfectly:
sudo systemctl enable sshsudo systemctl start sshSTEP 3: Generating SSH Keys
SSH keys enhance security. Generating a key pair using:
ssh-keygen -t rsa -b 4096 -C "abc@example.com"Basic SSH Commands to Connect Remote Server
STEP 1: Connecting to a Remote Server
For Connecting, Use:
ssh username@remote_hostSTEP 2: Executing Commands Remotely
Run the commands on the remote server directly:
ssh username@remote_host 'command_to_run'STEP 3: Transferring Files with SCP
Securely copy files between your local machine and the remote server:
scp localfile username@remote_host: /path/to/remote/directorySTEP 4: Using SFTP for File Management
For more interactive file transfer sessions, use SFTP:
sftp username@remote_hostAdvanced SSH Techniques
STEP 1: SSH Tunneling and Port Forwarding
Now, securely access services on your remote server by creating tunnels:
ssh -L local_port:remote_host:remote_port username@remote_hostSTEP 2: Managing Multiple Servers with SSH Config File
Simplify connections using an SSH config file (~/.ssh/config):
Host server1
HostName remote_host1
User username
IdentityFile ~/.ssh/id_rsa
STEP 3: Using SSH Agent for Key Management
Load the keys into the SSH agent for easier access:
ssh-add ~/.ssh/id_rsaSTEP 4: Setting Up SSH Multiplexing
Speed up multiple SSH connections by configuring multiplexing:
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600
Monitoring Remote Servers
STEP 1: Installing and Using htop for Resource Monitoring
Install htop for a real-time view of system performance:
sudo apt install htophtop
STEP 2: Using netstat for Network Monitoring
Check active connections and listening ports:
netstat -tulnSTEP 3: Checking Disk Usage with df and du
Monitor disk space usage:
df -hdu -sh /path/to/directorySTEP 4: Monitoring Logs with tail and grep
Track log files for issues:
tail -f /var/log/sysloggrep "error" /var/log/syslogManaging Services on Remote Servers
STEP 1: Starting and Stopping Services
Control services using systemctl:
sudo systemctl start service_namesudo systemctl stop service_nameSTEP 2: Checking Service Status
Now, check the status of a service:
sudo systemctl status service_nameSTEP 3: Managing Services with systemctl
Enable or disable services at startup:
sudo systemctl enable service_namesudo systemctl disable service_nameRemote Server Maintenance
STEP 1: Updating and Upgrading Packages
Keep your server updated:
sudo apt updatesudo apt upgradeSTEP 2: Scheduling Tasks with cron
Automate tasks using cron jobs:
crontab -eSTEP 3: Performing Backups with rsync
Backup data efficiently:
rsync -avz /source/directory /destination/directorySTEP 4: Disk Management and Partitioning
Use `
fdisk`and `lsblk`to manage disks and partitions.
Security Best Practices
STEP 1: Configuring Firewalls with ufw
Set up a firewall for added security:
sudo ufw allow sshsudo ufw enableSTEP 2: Using Fail2Ban to Prevent Brute Force Attacks
Install and configure Fail2Ban:
sudo apt install fail2banSTEP 3: Regularly Updating Software for Security Patches
Ensure all software is up-to-date to prevent vulnerabilities:
sudo apt updatesudo apt upgradeSTEP 4: Setting Up Two-Factor Authentication (2FA)
Adding an extra protection of security with 2FA for user's benefits.
Tools for Enhanced Remote Management
STEP 1: Using tmux and screen for Persistent Sessions
Keeping sessions alive even after disconnecting:
tmux
screen
STEP 2: Installing and Configuring Ansible for Automation
Automate server management tasks with Ansible.
STEP 3: Utilizing Monitoring Tools like Nagios and Zabbix
Set up comprehensive monitoring for your servers.
Conclusion
Controlling servers located far away through the Ubuntu command line is a strong and effective method for keeping your systems up and running. By becoming skilled in SSH, using monitoring software, managing services, and following top security practices, you can make sure your servers operate without problems and are safe. Consistent upkeep and oversight are crucial for avoiding problems and keeping your systems performing at their best.
