Friday, 21 July 2023

LINUX 1st Module Labs

                                         LINUX LABS 2023 1st Module

Create a New Directory and Subdirectories:


Task: Create a directory named "project" and subdirectories "docs," "src," and "tests" inside it.


Copy Files Between Directories:


Task: Copy all files with the extension ".txt" from the "documents" directory to the "backup" directory.


List Files and Directories:


Task: List all files and directories in the "home" directory.


Move and Rename Files:


Task: Move the file "report.txt" from the "downloads" directory to the "documents" directory and rename it to "monthly_report.txt."


View File Contents:


Task: View the contents of the file "README.md" using a pager (e.g., less or more).


Create and Edit a Text File:


Task: Create a new text file named "notes.txt" and add some text to it.


Search for a Pattern in Files:


Task: Search for the word "error" in all files within the "logs" directory.


Check System Resource Usage:


Task: Check the CPU and memory usage of the system.


View System Information:


Task: View information about the Linux distribution and kernel version.


Check Disk Space Usage:


Task: Check the disk space usage of the "home" directory.



Create a Directory:

Task: Create a new directory named "my_folder" in your home directory.



List Files and Directories:

Task: List all the files and directories in the "/etc" directory.



Change Directory:

Task: Change your current directory to "my_folder" created in Task 1.



Create a Text File:

Task: Create a new text file named "my_file.txt" in the "my_folder" directory.



View File Contents:

Task: View the contents of the "my_file.txt" using a pager (e.g., less or more).



Append Text to File:

Task: Add the line "Hello, Linux!" to the end of "my_file.txt."



Copy a File:

Task: Create a copy of "my_file.txt" and name it "backup_file.txt" in the same directory.



Rename a File:

Task: Rename "backup_file.txt" to "archive.txt."





Linux 4 Types of installation

 

1. Graphical with non custom partition

2. Minimal with non custom partition

3. Graphical with  custom partition

4. Minimal with custom partition


INSTALL BELOW OPERATING SYSTEM ON VMWARE ESXI HOST WITH CUSTOM PARTITION AS MINIMAL AND GRAPHICAL OS

 

REDHAT 8

REDHAT9

CENTOS 8 

Alma Linux 9

RockyLinux 8



CUSTOM PARTITION


/boot/efi >> 1600MB 

swap >> 1600MB

/    >> 9000MB

/emails >> 2000MB

/emailbackup >> 1000MB


https://docs.google.com/document/d/1W1H36PWNlXkJBo5SdbfsxY3z0tVrSiKogikY52i_9ek/edit

https://linuxadminforu.blogspot.com/2021/02/linux-vs-windows.html

https://linuxadminforu.blogspot.com/2021/02/advantages-of-linux_22.html





Linux Lab Module 1


* Create a user redhat with password India1234%$#@!

* Create a folder data 

* Create a file resume

* Setup IP address as follows:

  IPaddress : 16.16.16.100

  Netmask : 255.0.0.0

  Gateway: 16.16.16.1

*Disable Screen lock on Server

* Change the Operating System background to Redhat logo

* Create a new folder /nux/images


* Create files image1 image2 on directory /nux/images 




Linux OS Installtion Labs:


Install a Redhat 8 OS SERVER EDITION Graphical and minimal with the below custom partition as follows.

/boot/efi 1900

/ 8500

swap 1500

/databackup 1000

/imagebackups 1000


Installl a Redhat 9 OS SERVER EDITION Graphical and minimal with the below custom partition as follows.

/boot/efi 1000

/ 10500

swap 1500

/red 2200

/backup 1000


Installl a Redhat 9 OS Workstation EDITION Graphical with all client tools installed.


Install Redhat 8 OS Workstation EDITION with only office products installed.



Move a File:

Task: Move "archive.txt" to the parent directory of "my_folder."



Remove a Directory and Its Contents:

Task: Delete the entire "my_folder" directory and its contents.




Wednesday, 5 July 2023

How to Install vsftpd (ftp server) on CentOS 8 / RHEL 8

 FTP, short for File Transfer Protocol, is a protocol that provides access to files residing on a server. It’s one of the earliest protocols that enabled users to download files over the internet.  With the FTP protocol, users can download and upload files on servers with ease.

Vsftpd, short for Very Secure FTP daemon, is a secure FTP daemon that is an upgrade of FTP protocol. It enforces secure connections to FTP servers by encrypting traffic send to and from the server, and by so doing, the file transfer is kept safe and secure from hackers.

In this topic, we shine the spotlight on the installation of vsftpd on CentOS 8 / RHEL 8.

Step 1) Install vsftpd using dnf command

Right off the bat, we are going to install vsftpd. To achieve this, we will run the command below:

$ sudo dnf install vsftpd

dnf-install-vsftpd-centos8

Press ‘y’ and hit ENTER to get underway with the installation. The installation takes a few seconds and will complete in no time. The output below confirms that vsftpd has been successfully installed.

Successfully-installed-vsftpd-centos8

The output indicates that we have installed vsftpd version 3.0.3-31.el8.x86_64. To confirm this, execute the following command:

[linuxtechi@centos8-vsftpd ~]$ rpm -q vsftpd
vsftpd-3.0.3-31.el8.x86_64
[linuxtechi@centos8-vsftpd ~]$

The output should tally with the version printed on the terminal upon successful installation. To retrieve more detailed information about Vsftpd, append the -i flag at the end as shown:

$ rpm -qi vsftpd

This will print additional information on the screen such as the Architecture, install date, license and signature to mention a few.

rpm-qi-vsftpd-centos8

With vsftpd installed, we need it running to facilitate access to file shares.

To start the vsftpd service, run the command:

$ sudo systemctl start vsftpd

You may also want to enable it to start automatically upon a reboot. To achieve this, run the command

$ sudo systemctl enable vsftpd --now

To verify the status of vsftpd on your system, run:

$ sudo systemctl status vsftpd

vsftpd-service-status-centos8

If you see the “active: (running)” directive in green as indicated on the terminal, then the vsftpd service is up and running.

Step 2) Create a ftp user and its directory

Next, we will create a user that we will use to access the FTP server. In this case, the user will be ftpuser but feel free to give your user a name of your choice.

$ sudo adduser ftpuser
$ sudo passwd ftpuser

With the FTP user in place, we will proceed and create the FTP directory and assign the following permissions and directory ownership.

$ sudo mkdir -p /home/ftpuser/ftp_dir
$ sudo chmod -R 750 /home/ftpuser/ftp_dir
$ sudo chown -R ftpuser: /home/ftpuser/ftp_dir

We also need to add the FTP user to the /etc/vsftpd/user_list file to allow the user access to the vsftp server.

$ sudo bash -c 'echo ftpuser >> /etc/vsftpd/user_list'

Step 3) Configure vsftpd via its configuration file

So far, we have managed to install and confirm that vsftpd is up and running.  Further adjustments are necessary for Vsftpd to allow users access to the server.

The default configuration file for vsftpd is the /etc/vsftpd/vsftpd.conf file. The file is replete with directives that help fortify your FTP server’s security.

In this section, we will make a few tweaks to the configuration file and allow users to access the server.

To allow local users to access the FTP server remotely, and block anonymous users, ensure you have the directives as shown:

anonymous_enable=NO
local_enable=YES

To grant users rights to run any FTP command & make changes such as uploading, downloading and deleting files, have the following line in place.

write_enable=YES

For security purposes, you may opt to restrict users from accessing any files & directories outside their home directories. Therefore, have the following directive in place.

chroot_local_user=YES

To grant users write access to their respective home directories, ensure you have this directive.

allow_writeable_chroot=YES

Next, we are going to define custom ports to enable Passive FTP connections. In this case, we will specify ports 30000 and 31000. We shall later open these on the firewall.

pasv_min_port=30000
pasv_max_port=31000

Next, we are going to only allow the users defined in the /etc/vsftpd/user_list access to the server and block the rest. To achieve this, have the lines below.

userlist_file=/etc/vsftpd/user_list
userlist_deny=NO

Finally, save and close the file. For the changes to persist, restart the Vsftpd service.

$ sudo systemctl restart vsftpd

At this point, you can test for FTP connectivity by running

$ ftp ip-address

Specify the username of the ftp user and later provide the password. You should get the output as shown.

ftp-command-linux

Though we have established connectivity to the vsftpd server. The connection is not secure, and information sent is in plain text and not encrypted. We, therefore, need to take extra steps to encrypt communications sent to the server.

Step 4) Configure SSL / TLS for vsftpd

To encrypt communications between the server and a client system, we need to generate a TLS certificate and later configure the server to use it.

To generate the certificate, run the command below:

$ sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/vsftpd.pem -out /etc/vsftpd/vsftpd.pem

This will be followed by a series of prompts where you will be required to provide a few details such as country name, state or province, and organizational name to mention a few. Fill out all the details accordingly as shown.

SSL-Certs-vsftpd-CentOS8

We also need to tell the server where the certificate files are stored. So, head back to the configuration file /etc/vsftpd/vsftpd.conf and specify the path to the certificate files.

rsa_cert_file=/etc/vsftpd/vsftpd.pem
rsa_private_key_file=/etc/vsftpd.pem

And then, instruct the server to turn on SSL.

ssl_enable=YES

Save and exit the configuration file. To make above changes into the effect, restart vsftpd service,

$ sudo systemctl restart vsftpd

Step 5) Allow ftp server (vsftpd) ports in the firewall

If you are running a firewall, you need to allow these salient ports”

  • 20 – to allow FTP traffic
  • 21 – FTP data port
  • 30000-31000 – To allow passive communication with the FTP server.

Therefore, run the commands below:

$ sudo firewall-cmd --permanent --add-port=20-21/tcp
$ sudo firewall-cmd --permanent --add-port=30000-31000/tcp

Then reload the firewall for the changes to come into effect.

$ sudo firewall-cmd --relo­ad

Step 6) Test your vsftpd or FTP server

With all settings done, it’s time to test our connectivity. In this example, we are using an FTP client known as FileZilla which is a free FTP client for both client and server systems. It supports both plain FTP and FTP over TLS which is what we are going to test.

When launched, the interface looks as shown. Provide the IP address of the host (vsftpd), username and password of the ftp user and then click on the ‘Quickconnect’ button.

Connect-ftpserver-filezilla

Shortly after, a pop-up will appear displaying the FTP server’s certificate & session details. To proceed with the connection, click on “Always trust this certificate in future session” and then hit enter.

SSL-Certs-vsftpd-filezilla

If you all your configurations are correct, you should gain entry without any issues as shown. On the bottom right pane, the remote server’s home directory as shown. You can now upload, download and edit the files as you deem fit.

Access-ftp-server-filezilla

This concludes our topic on the installation of vsftpd on CentOS 8. It’s our hope that you can now comfortably set up your own vsftpd (secure ftp) server. Please do share it among your technical friends and also share your valuable feedback and comments.

Sunday, 25 June 2023

How To Create Your First Python AWS Lambda Function

 When creating a small Python script, the question that eventually comes up is where the code will be hosted and run. For some small scripts, you might be able just to run the code locally. But, what if you want the code to be run on a regular schedule, even when your laptop is off? Or, what if you want the script to be part of an endpoint that other scripts can access?

One option would be to set up a server and running the code there. You could spin up a droplet on Digital Ocean, set up the Python environment, and run your scripts there. But, what if, you do not want to have to set up and manage servers?

This is where serverless functions can help! Serverless functions are scripts that you can set up without needing to manage the underlying infrastructure. Additionally, these systems auto-scale so you can easily run the function a few times a year or thousands of times per day without you needing to figure out how to scale.

AWS's Lambda is a great serverless function system that tightly integrates with the rest of the AWS ecosystem. AWS Lambda allows you to create serverless functions in various environments, including Python, NodeJS, and Go. Even better, the AWS free tier includes 1 million Lambda requests per month!

Writing Your First Python Lambda Function

For the most part, Python code within AWS Lambda is almost the same as writing normal Python scripts. However, within Lambda, you will need to specify a handler function which is then called by the environment. In most of my Lambda functions, I call this something similar to handler or lambda_handler. This looks like this:

def handler(event, context):
    '''Gets called by the Lambda environment.'''
    return true

For the handler function, we have two parameters: event and context.

The "event" parameter is an object that contains data that was passed to the Lambda function. For example, if the Lambda was attached to an endpoint, then we could access the data sent to the endpoint with something like event['body'].

The "context" parameter is an object that contains data about the Lambda function itself and its runtime environment.

Let's go ahead and write a very basic Lambda function that will accept a value and then return its square root.

import math

def handler(event, context):
    '''Gets called by the Lambda environment.'''
    return math.sqrt(event)

Creating Your Lambda Function

To get started, you will need to sign in to your AWS account.

Once in your AWS console, you can either click on "Services" to go to "Compute"->"Lambda" or use their search bar to search for Lambda.

AWS console with focus on the top search bar with "lambda" entered.

Once inside Lambda, you will find your Lambda dashboard as shown here:

AWS Lambda dashboard with navigation along the left side with links for dashboard, applications, and functions.

Click on the "Functions" page from within the menu to get to your functions.

AWS Lambda functions list with 0 listed. There is a button on the far right that says "Create function"

Next, click on the "Create function" button in the top right.

Create a new function screen with fields for function name and runtime. The "Author from scratch" option is selected.

Use the "Author from scratch" option. Name your function anything you want. For the runtime, select a version of Python.

If this function were interacting with other parts of the AWS ecosystem, we'd want to set up permissions or an AWS role. But, for this example, we can keep the defaults.

Next, click the final "Create function" button.

Once created, you will be taken to the function's overview and code. Here you can review any triggers or layers attached to the function. For this one, we will not be using these.

Scroll down to the code section and open the default lambda_function.py file. Copy and paste our basic Python code from above into the file. Then, click the "Deploy" button.

Code source section of the function with the lambda function file opened and some python code inside including a function called handler.

Next, scroll down to the "Runtime settings" section.

Runtime settings section with runtime set to Python 3.8 and handler set to lambda_function.handler

We want to edit the "Handler" field. This is how Python will try to import your function. So, it should be the file name followed by the function name. In this case, it should be updated to lambda_function.handler because we kept the default file but used our function called handler.

Now, scroll back up to the top and switch to the "Test" tab. Replace the example JSON with any number, such as 16. Next, click the "Test" button.

Test event with the number 16 entered as the value.

A box will appear with the execution results, which will include the square root value. This area will also display some information about the request, such as duration time.

Execution results with a "succeeded" message with the value of 4 shown as the result returned.

Congratulations! Your very first Lambda function has been created.

Next Steps

While it's useful to explore how the function works, you probably want to do a lot more with your Lambda function. In the next article in this series, I'll look at setting up a Lambda function on a recurring schedule. In future articles, I'll look at safely passing credentials to it and add other Python libraries to it.

Be sure to subscribe below to get notified when the next article is published!