Understanding SCP, Systemctl, Logs, Volume mount and More in Simple Terms
What is SCP?
SCP stands for Secure Copy Protocol. It’s a way to transfer files between computers securely over a network. If you have files on one machine and need to move them to another, SCP is a useful tool.
Basic SCP Commands:
Copy a file from your local machine to a remote machine:
scp -i [Private-Key] [Local-File] [Username]@[Remote-Server-Address]:[Remote-Destination-Path]
[Private-Key]
is your SSH private key for authentication.[Local-File]
is the file you want to transfer.[Username]
is your username on the remote server.[Remote-Server-Address]
is the address of the remote server.[Remote-Destination-Path]
is where you want to place the file on the remote server.
Copy a file from a remote machine to your local machine:
scp -i [Private-Key] [Username]@[Remote-Server-Address]:[Remote-File-Path] [Local-Destination]
[Remote-File-Path]
is the file path on the remote server.[Local-Destination]
is where you want to save the file locally. The dot (.
) means the current directory.
Some Useful Commands
Creating and Editing Files:
Create an empty file:
touch [File-Name]
For example:
touch myfile.txt
Write text to a file:
echo "text to write" > [File-Name]
For example:
echo "hello raees" > super_secret.txt
This writes "hello raees" into the file
super_secret.txt
.
Managing Services with Systemctl
Systemctl is used to control and manage services on a server, such as starting, stopping, or checking the status of services.
Basic Systemctl Commands:
Start a service:
sudo systemctl start [service-name]
Example:
sudo systemctl start docker
Stop a service:
sudo systemctl stop [service-name]
Example:
sudo systemctl stop docker
View logs for a service:
sudo journalctl -fu [service-name].service
Example:
sudo journalctl -fu docker.service
Understanding Logs and Searching Through Them
Logs keep track of events, errors, and other important information. You can search and analyze logs using various commands:
Grep:
Search for lines with a specific term:
grep [search-term] [file-name]
Example:
grep ERROR app.log
Make the search case-insensitive:
grep -i [search-term] [file-name]
Search recursively through directories:
grep -r [search-term] [directory]
Find:
Search for files by name:
find [directory] -name [file-pattern]
Example:
find /home/ubuntu -name "*.log"
AWK:
Print lines containing a term:
awk '/[search-term]/' [file-name]
Print specific columns:
awk '/[search-term]/ {print $1, $2, $3, $5}' [file-name]
Print row numbers and specific columns:
awk '/[search-term]/ {print NR, $5}' [file-name]
Sed:
Sed is a stream editor used for modifying files:
Replace text in a file and save to another file:
sed 's/[old-text]/[new-text]/g' [input-file] > [output-file]
Example:
sed 's/dev+1234/bank+1234/g' dev.env > prd.env
This replaces
dev+1234
withbank+1234
and saves the result inprd.env
.
Adding Storage to a Server
When your server is running out of space, you can add more storage using EBS (Elastic Block Storage):
Create and attach a new EBS volume to your instance.
Format the new volume:
sudo mkfs -t ext4 /dev/xvdf
List block devices:
lsblk
Create a mount point:
sudo mkdir /mnt/new_volume
Mount the new volume:
sudo mount /dev/xvdf /mnt/new_volume
Now, the new storage is ready for use!
Feel free to experiment with these commands and practices to get comfortable with managing files, services, and storage on your server.