Day 3: Few Advanced Linux Concepts

Day 3: Few Advanced Linux Concepts

Table of contents

No heading

No headings in the article.

Hello Everyone!

I have completed my Day 3 learning part. Please find my below Notes. Hope it will be helpful.

Concepts this Article covers:

  1. Setting up an SSH client

  2. SCP (Secure Copy)

  3. User Management

  4. Group Management

  5. Linux File System Permissions

  6. grep (Global regular expression print), find, awk

These are the topics that covered as part of Day3 learning. Please find below notes.

Setting up SSH client

SSH is a secure shell. A protocol typically used for connecting to Linux servers. The command-line SSH tool lets you log into your server and run commands remotely to perform any required task.

ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. It is intended to provide secure encrypted communications between two untrusted hosts over an insecure network.

We can use ssh in windows. For that, it needs to be enabled. 👇

How to Enable and Use SSH Commands on Windows 10 (winbuzzer.com)

In AWS Linux, it will be already installed by default.

Connecting through SSH Client in windows CMD:

In windows, go to path where pem file is download and give command mentioned in SSH client page of aws -->ssh -i "NodePair.pem" ubuntu@ec2-3-110-182-104.ap-south-1.compute.amazonaws.com then enter

Here -i is for giving your pem file

SCP: Secure Copy

“secure copy” allows us to secure transferring of files between localhost and remote host or between two remote hosts.

Local host------------>Remote Hosts

sudo scp -i NodePair.pem localfile.txt ubuntu@ec2-3-110-182-104.ap-south-1.compute.amazonaws.com :/home/ubuntu/aasifa

User Management:

Creating users and groups.

#To check username who is currently logged in - whoami

#to add user, you need permissions to add user, -m creates users home directory

sudo useradd ayman -m

#to view if user is added

cat /etc/passwd

#setting up password for ayman user

sudo passwd ayman

#you cant enter into ayman by cd ayman, you either need to switch to ayman or give permissions

su ayman

⬤ Group Management

Creating a Group:

#we need few more users to add them to a group

useradd rosy

useradd jasmine

#first creat a group and name it then add all the users into it

sudo groupadd developers

#adding users into developers groups

#here -M overrides the group

sudo gpasswd -M ayman,rosy,jasmine developers

#here -M overrides the existing group

#to add a user to a group -a

sudo gpasswd -a ayman developers

#Now when we give permission to a group then it applies to all the users inside it.

#to remove a user from the group

sudo gpasswd -d ayman developers

#to delete the group

sudo groupdel developers

⬤ Linux File System Permissions

We can set permission as below

  1. chmod u+r /filename (here giving read permission to the user for the file)

you can also give as

  1. chmod g+rwx/filename

  2. #to remove permission

    chmod g-rw/filename

chown:

#To change ownership of the directory or a file

sudo chown jasmine testfile.txt

  1. We can set permission using numeric values also

r(read) - 4

w(write) - 2

x(execute) - 1

#giving rwx permissions to user, rx to group, x to others

sudo chmod 750 testfile.txt

#Other is every one that is not the owner or in the group.

For example, if you have a file that is root: root then the root is the owner, users/processes in the root group have group permissions, and you are treated as other.

⬤ ACL : (Access Control List)

#To work with ACL you need to install it

sudo apt install acl

Hadoop - File Permission and ACL(Access Control List) - GeeksforGeeks

permissions only for the owner, one group, and others for any share point or folder or file. ACLs allow you to grant permissions to multiple individuals or groups on a shared item.

Unlike the basic and regular way of giving permissions to one user that is the owner of a file and one group that is the group owner of a file using the “chmod” command, if you have to give additional permissions to another user or another group on a file without making the user a member of the group, you will have to use ACL to do it.

When permission is set on a file or directory using ACL, it displays a “+” sign when a list command is used.

Lets say I have 3 users ayman, rosy and jasmine and a group developers

#checking if permissions applied to the folders,users and group using getfacl using ACL

Lets create a directory and add users into it

#we added users in acldir

#first check permission in acldir

#Now give permissions to acluser1 which was created under acldir

#set read and execute permission to the acluser1 which is a user present in acldir

setfacl -m u:acluser1:rx acldir

#check if permissions applied or not

getfacl acldir

#Now create a group " aclgrp " and add all these aclusers1,2,3 into it

#giving rwx permission to aclgrp

⬤ grep (global regular expression print)

Here regular expression is used to search data, matching complex patterns

#Install grep if you don't have

sudo apt-get install grep

You can search in a particular folder or a file

Examples:

grep devops /home/ubuntu -- displays devops word in ubuntu folder

grep INFO test.txt

#search the string in output of first command

cat sample.txt | grep [options] string

Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.

-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.

Interview Question:

To know if user is created or not use

sudo grep ayman /etc/passwd

Find:

The Major difference is FIND is for searching files and directories using filters while GREP is for searching a pattern inside a file or searching process(es).

Find can perform,

searching by file, folder, name, creation date, modification date, owner and permissions.

Syntax:

find [where to start searching from] [-options] [what to find]

examples:

#it will find(display) name of the file called example.txt in the current dir

find . -name example.txt

#it will find all the files which are .jpg files in the home dir

find /home -name *.jpg

-exec CMD: The file being searched which meets the above criteria and returns 0 for as its exit status for successful command execution.
-ok CMD : It works same as -exec except the user is prompted first.
-inum N : Search for files with inode number ‘N’.
-links N : Search for files with ‘N’ links.
-name demo : Search for files that are specified by ‘demo’.
-newer file : Search for files that were modified/created after ‘file’.
-perm octal : Search for the file if permission is ‘octal’.
-print : Display the path name of the files found by using the rest of the criteria.
-empty : Search for empty files and directories.
-size +N/-N : Search for files of ‘N’ blocks; ‘N’ followed by ‘c’can be used to measure size in characters; ‘+N’ means size > ‘N’ blocks and ‘-N’ means size < ‘N’ blocks.
-user name : Search for files owned by user name or ID ‘name’.
\(expr \) : True if ‘expr’ is true; used for grouping criteria combined with OR or AND.
! expr : True if ‘expr’ is false.

awk command:

Top 20 AWK Command in UNIX/LINUX with Examples [Updated] (hackr.io)

Used to make segments of the output. $1 prints the first column of df - H output.