Day 3-Advanced linux commands
Advanced Linux Commands
Why you cannot access /root directory and how to access root directory?
/root is the home directory of root user.Bydefault only root user has access to this directory
when you are loggedin as regular user,even if you can access other system directories(like /etc,/home)you are restricted from accessing /root for security reasons
How to access root directory?
sudo su-switch to root user
cd / - navigate to root of the filesystem(/)
cd root - entered home directory of the root user(/root)
ls - listed the contents of /root ,where you found the snap directory
User Management in Linux
1.Adding a user: sudo useradd -m username
example: sudo useradd -m kaleen
-m =when we add a user a folder for that user user is also created in home directory
2.Creating password : sudo passwd username
example: sudo passwd kaleen
3.switch to new user :su username
example:su kaleen
Users cannot switch to other directory.Type ‘exit’ to exit
In the case of organization whenever a new user joins. They will add them as non root user with username and password
Creating groups:
sudo groupadd groupname
sudo gpasswd -a username groupname
example:
sudo groupadd batch8
sudo gpasswd -a hari batch8
cat /etc/passwd - to see users list
cat /etc/passwd - to see group list
whenever a user is created a group is also created
sudo is a group which has privileges of root user
File permissions
In Linux, file permissions determine who can read, write, or execute a file or directory.When you run ls -l
in a directory, you get an output like this:
-rw-rw-r-- 1 ubuntu ubuntu 53 Oct 14 10:09 demo.txt
The Permission String Breakdown
The permission string -rw-r--r--
can be divided into four parts:
Symbol | Meaning |
- | File type (- = file, d = directory) |
rw- | user permissions: Read (r), Write (w), No execute (-) |
rw- | Group permissions: Read (r), write (w), No execute (-) |
r-- | Others (everyone else) permissions: Read (r), No write (-), No execute (-) |
Chmod permission table
values | r | w | x |
0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 |
2 | 0 | 1 | 0 |
3 | 0 | 1 | 1 |
4 | 1 | 0 | 0 |
5 | 1 | 0 | 1 |
6 | 1 | 1 | 0 |
7 | 1 | 1 | 1 |
r-- - - - - - - =400
To change permissions of a file : sudo chmod 400 file.txt
To change owner of a file :sudo chown newowner filename
sudo chown kaleen demo.txt
SSH (Secure Shell) – A Secure Way to Access Remote Systems
SSH (Secure Shell) is a network protocol that allows secure access to remote systems over an unsecured network. It provides encrypted communication between two machines, ensuring confidentiality and integrity.One of the key aspects of SSH is authentication using passwords or cryptographic keys to verify a user’s identity. This makes SSH both secure and reliable.
How to Set Up and Use SSH
1. Generating SSH Key Pair (Public and Private Keys)
Using public-private key authentication enhances security by eliminating the need for passwords.
ssh-keygen
connecting local system to server
1.Create private key and download .Open ssh client(local system)
2.Locate your private key .Run the command cat batch8jumpserverkey.pem.Then you can see the private key
3.chmod 400 "jumpserverkey.pem"
4.Connect to your instance using its Public DNS: use the below command
ssh -i "jumpserverkey.pem" ubuntu@ec2-44-243-95-101.us-west-2.compute.amazonaws.com
Connecting 2 servers :Conecting batch-8-server with batch-8-jump-server
1.cd .ssh in batch-8-server
2.ls
3.now you can see authorized_keys.ie,public key
4.Now vim jumpserverkey.pem in batch-8-server and paste the private key of batch-8-jump-server from downloads to it
5.sudo ssh -i "jumpserverkey.pem" ubuntu@ec2-44-243-95-101.us-west-2.compute.amazonaws.com
Now we connected to batch-8-jump-server by using private key of it
SCP(Secure Copy)
Connecting jumpserver to local system
1.ssh-keygen in batch-8-jump-server to create keypair(private,public)
2.cd .ssh
3.ls=Now we can see lists of authorized_keys,privatekey and public key
4.Now get that privatekey in local by copying from batch-8-jump-server using scp
Local system
1.mkdir jumpserverkey to create folder to store the private key
2.cd jumpserverkey
3.scp -i “../jumpserverkey.pem” ubuntu@ec2-44-243-95-101.us-west-2.compute.amazonaws.com:/home/ubuntu/.ssh/id_ed25519 .
(../ =previous directory ie,inside downloads there is private key of jumpserver
3.ls =Now you can see id_ed25519
Download a file from batch-8-jump-server to local
1.Add the public key of jumpserver in authorized_keys to authorize it
2.Create a file in jumpserver > vim sampleone
Local system
1.scp -i id_ed25519 ubuntu@ec2-44-243-95-101.us-west-2.compute.amazonaws.com:/home/ubuntu/.ssh/sample one .
3.Now sampleone will be added to the folder
Connecting batch-8-jump-server with batch-8-server using ssh
1.cd .ssh in batch-8-jump-server
2.ls
3.now you can see authorized_keys.ie,public key
4.Now vim batch-8-key.pem in batch-8-jump-server and paste the private key of batch-8-server from downloads to it
5.sudo ssh -i "batch-8-keykey.pem" ubuntu@ec2-44-243-95-101.us-west-2.compute.amazonaws.com
Now we connected to batch-8-server by using private key of it
Update,upgrade,systemctl,purge
1.To make update>sudo apt-get upadte
2.To install> sudo apt upgrade(downloaded updates will be installed)
Install nginx
1.Sudo apt-get install nginx
Uninstall nginx
1.sudo apt purge nginx
systemctl
1.To check status- systemctl status nginx
2.To stop nginx - sudo systemctl stop nginx
Installing docker
grep command
The grep
command in Linux is used to search for patterns within files or input streams. It stands for Global Regular Expression Print. This command is extremely useful when you need to search through files or outputs for specific words, phrases, or patterns.
grep keyword -ir /path/to/location
-i
: Case-insensitive search (ignores uppercase/lowercase differences).-r
: Recursively search through all files and subdirectories.
example: grep junoon -ir /home/ubuntu
Logs Analysis
using wget command copy the url of logs from github.we need to find warnings from this log
1.vim zookeeperlog-Add the log files here
2.grep WARN zookeeperlog
3.grep WARN -i zookeeperlog > warningsonlyzookeeperlog (we are adding WARN zookeeperlogs to warningsonlyzookeeperlog)
awk command
The awk
command in Linux is a powerful text-processing tool used for searching, pattern matching, and data extraction from files.It scans files line by line and allows you to search, filter, extract, and manipulate data based on patterns.
command - awk 'pattern { action }' filename
example :awk ‘/WARN/ {print $1}’ zookeeperlog (this will search for logs with WARN keyword and will print the first column )
To get line number
awk ‘/INFO/ {print NR,$1,$2}’ zookeeperlog (print the line numbers with keyword INFO and also print the column 1 and 2)
To get a specific keyword
awk '/INFO/ {print NR,$1,$2}' zookeeperlog | grep -ir QuorumCnxManager
To get 10 number of lines of WARN logs
awk 'NR>=1 && NR<=10 && /WARN/ {print $1,$2,$5}' zookeeperlog
To get specific number of rows based on paricular set of time interval
awk '$2>= "19:14:00" && $2<= "19:17:00" && /WARN/ {print $1,$2,$5}' zookeeperlog
Sed(Stream Editor)
sed
operates on text streams, meaning it can read input from files or standard input (stdin), apply transformations, and then output the result to standard output (stdout) or to a file.
basic syntax of sed : sed [options] 'command' file
options
: Various flags that modify the behavior ofsed
.command
: The editing operation you want to perform.file
: The input file thatsed
will process.
1.vim password.txt
2.Here after running the sed command sed -i 's/234/abc/g' password.txt
Here file will be modifed.But the original file is not modified.This will print the modified content using cat only
-i means inplace editing
3.But the original file is not modified
4.Here the output will be printed
5.used to mask password
6.Appending +91 to mobilenumbers
Find
The find
command allows users to search for files and directories within a specified directory hierarchy.
find [path] [options] [expression]
path
: The directory path where the search begins. Use.
for the current directory or/
for the entire filesystem.options
: Flags that modify the behavior of the command.expression
: Criteria used to filter the search results.command : find ubuntu/ -name *.txt
For a specific keyword
find ubuntu/ -name *.txt | grep -ir junoon
Subscribe to my newsletter
Read articles from Divya K Nair directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by