Forum

Notifications
Clear all

DevOps Training 25-Sept-2024

0
Topic starter
File Permissions
-----------------
Permissions to files / directories are applied in 3 level
1. user level
2. group level
3. other users level
 
Total 9 permissions. First 3 are user permissions, next 3 are group permissions and next 3 are others permissions
 
d rwx   rw-   r-x 2 ec2-user ec2-user 6 Nov  7 03:06 dir1
d rwx   rw-   r-x          
  ---   ---   ---
   |     |     |
  user   |     |
       group   |
             others   
  rwx   rw-   r-x          
 ---   ---   ---
r=read     ==> use commands such as cat, more, less, head / tail for a file
w=write    ==> use commands such vi(to modifiy), rm (to delete) for a file
x=execute  ==> you can run any command, scripts / run a file as a programm
 
r=read=4
w=write=2
x=execute=1
 
current        desired
- rw- rw- r-- ==> rwx     rwx      rw-
  420 420 400     421     421      421
                 4+2+1   4+2+1    4+2+1
   6   6   4       7       7        7
 
 to give only read permissions to all
 
 - r-- r-- r--
   400 400 400
    4   4   4
 
 to give write permisions to user
 - rwx r-x r--
   421 401 400
    7   5   4
 
examples:

list of some common settings, numerical values and their meanings:

-rw------- (600) -- Only the user has read and write permissions.

-rw-r--r-- (644) -- Only user has read and write permissions; the group and others can read only.

-rwx------ (700) -- Only the user has read, write and execute permissions.

-rwxr-xr-x (755) -- The user has read, write and execute permissions; the group and others can only read and execute.

-rwx--x--x (711) -- The user has read, write and execute permissions; the group and others can only execute.

-rw-rw-rw- (666) -- Everyone can read and write to the file. Bad idea.

-rwxrwxrwx (777) -- Everyone can read, write and execute. Another bad idea.

 
chmod (change mode)
-----------------
chmod command is used to change the permissions of file / directory
 
 
syntax:
  for file--->    chmod <Permission_in_Number> <FileName>
                     eg: 1. chmod 777 <FileName> ==> it will give all permissions(rwx) to everyone
  for directory:  chmod -R <Permission_in_Number> <DirectoryName>
                     -R ==> will change/applies mentioned permissions of all directories & sub directories
 
examples:
 
   list of some common settings, numerical values and their meanings:
   
   -rw------- (600) -- Only the user has read and write permissions.
   
   -rw-r--r-- (644) -- Only user has read and write permissions; the group and others can read only.
   
   -rwx------ (700) -- Only the user has read, write and execute permissions.
   
   -rwxr-xr-x (755) -- The user has read, write and execute permissions; the group and others can only read and execute.
   
   -rwx--x--x (711) -- The user has read, write and execute permissions; the group and others can only execute.
   
   -rw-rw-rw- (666) -- Everyone can read and write to the file. Bad idea.
   
   -rwxrwxrwx (777) -- Everyone can read, write and execute. Another bad idea.
 
 
__________________________________________________________________________________________________
 
chown(change ownership)
-----------------------
chown command is used to change ownership of file / directory
 
syntax: chown <usersName>:<groupName> fileName
        chown -R <usersName>:<groupName> directoryName
        
Note:
-R is mandatory for directories
for running chown / chmod may require sudo permissions
 
SHELL:
 
variables:
----------
Variables are memory location which can store some values.

syntax: <variable_name>=<variable_value>
eg: MYNAME=devops_user

How to print / check / substitute / call values of a variable?
--------------------------------------------------------------

$ (dollar) is mandatory while substituting varaible ==> $<variable_name>

echo $<variable_name>

ex: PLAYER=viratKohli
echo " my favorite sportsmen is $PLAYER "

Variable types:
---------------
1. Local variables / user defined variables.
variables created by user

2. system / predefined variables.
variables created by system
env ==> command to check system defined variables

user defined variables are only valid till your terminal(session) exists or your servers running.

to store variables irrespective of terminal running or restarts

How to set variable permanately in all shell sessions & also server restarts ?
------------------------------------------------------------------------------
Answer: By adding the variable inside startup script ( ~/.bashrc ).

*******IMPORTANT********************

EXIT CODE
-------------
every command /script generates an auomatic code which signifies, previous command execution is succesfull or not
syntax: echo $?

if output is 0 zero ==> command executed succesfully
if output is non-zero ==> Command not executed succesfully

 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SHELL:
------
1 Shell is responsible to read command provided by user.
2. Shell will check whether the command is valid or not.
3. Shell interpretes (converts) that command into kernel understandable form & Kernel execute that command with the help of hardware.

Shell + kernel = Linux OS.

Types of shell:
---------------
1. sh ==> basic shell, earlier / olden days is used in unix ==> /bin/sh
2. bash ==> adavnced shell, which is used in linux ==> /bin/bash
others: c-shell, k-shell etc

Note: echo $SHELL ==> Command to check which shell we are using

Steps to create & execute shell script:
----------------------------------------
step1. shell script will have extension called <script-name>.sh
vi <script-name>.sh
step2. in the script first line we are going to write is called as shebang #!/bin/bash

step3. you add your commands (save the script file)

step4. execute the script
sh <script-name>.sh (or) bash <script-name>.sh (or) ./<script-name>.sh

what is shebang line, why its sused?
------------------------------------
shebang line is used to specifies interpreter (or) shell to be used for execution of script

can we run script without shebang line?
------------------------------------------
yes, in that case whatever default shell ia there, that will get used.

script1. write a shell script to print date & time.
---------------------------------------------------
vi date-script.sh

#!/bin/bash
echo "Current date and time...."
date

save & quit

execute script
sh date-script.sh

script2. write a shell script to CHECK Storage(HDD) of server.
--------------------------------------------------------------

vi STORAGE-script.sh

#!/bin/bash
echo "Current storage used is ...."
df -kh | head -5 | tail -1 | awk -F " " '{print$5}'

save & quit

execute script
sh STORAGE-script.sh

script3: write a script to create a variable (sportsman=viratkohli) & verify the variable creation using echo command:
----------------------------------------------------------------------------------------------------------------------
[ec2-user@ip-172-31-6-171 ~]$ cat vars_check.sh
#!/bin/bash
sportsman=viratkohli
echo "my fav sportsamen is $sportsman"

script4: write a script to have employee data coming as variables & verify the variable creation using echo command:
----------------------------------------------------------------------------------------------------------------------
[ec2-user@ip-172-31-93-35 ~]$ cat employee_data.sh

#!/bin/bash
EMP_NAME=alice
EMP_ID=54234
EMP_LOC=bangalore
EMP_STATE=karnataka
echo "Display employee name $EMP_NAME "
echo "Display emloyee id $EMP_ID "
echo "Display employee location $EMP_LOC "
echo "Display employee state $EMP_STATE "

script5: script to check harware resources:
-------------------------------------------
[ec2-user@ip-172-31-6-171 ~]$ cat ram_usage_check.sh

#!/bin/bash
echo "Current free ram available is..."

free -m | grep Mem | awk -F " " '{print $4}'
#free -m

echo "current disk usage is..."
df -kh .

echo "number of cpu...."
nproc

 
5 Answers
0

Updated the task.

This post was modified 1 year ago by RajeevRF
Harish September 25, 2024 11:29 pm

Good One Rajeev, however you have a catch in the code.  Guess what? its the time stamp. all have same.. we have defined the timestamp initially and used it for all. 


-rwxrwxrwx  1 root  staff  153 Sep 25 22:59 Rajeev.sh

-r--r--r--  1 root  staff    0 Sep 25 22:59 file12024.09.25-22.59.30.log

-r--r--r--  1 root  staff    0 Sep 25 22:59 file22024.09.25-22.59.30.log

-r--r--r--  1 root  staff    0 Sep 25 22:59 file32024.09.25-22.59.30.log

-r--r--r--  1 root  staff    0 Sep 25 22:59 file42024.09.25-22.59.30.log

-r--r--r--  1 root  staff    0 Sep 25 22:59 file52024.09.25-22.59.30.log

0

Task Updated.

Harish September 25, 2024 11:02 pm
This post was modified 1 year ago 2 times by Harish

Dinesh Interestingly you have used Seq , curious to know why you used Seq, also i see only the date and not timestamp.... 

-rwxrwxrwx  1 root  staff  151 Sep 25 23:01 Dinesh.sh

-r--r--r--  1 root  staff    0 Sep 25 23:01 file12024-09-25.log

-r--r--r--  1 root  staff    0 Sep 25 23:01 file22024-09-25.log

-r--r--r--  1 root  staff    0 Sep 25 23:01 file32024-09-25.log

-r--r--r--  1 root  staff    0 Sep 25 23:01 file42024-09-25.log

-r--r--r--  1 root  staff    0 Sep 25 23:01 file52024-09-25.log

0

Assignment Task

Harish September 25, 2024 11:05 pm
This post was modified 1 year ago by Harish

Good that you have printed the message , However the timestamp is not formatted properly. Good Try!
Also need to think about using loops, the program looks raw. 

The 5 files are created with timestamp successfully

The file permissions are modified with read-only for all the users successfully.


-rwxrwxrwx  1 root  staff  424 Sep 25 23:03 Muruguraj.sh

-r--r--r--  1 root  staff    0 Sep 25 23:04 file11727285646.log

-r--r--r--  1 root  staff    0 Sep 25 23:04 file21727285646.log

-r--r--r--  1 root  staff    0 Sep 25 23:04 file31727285646.log

-r--r--r--  1 root  staff    0 Sep 25 23:04 file41727285646.log

-r--r--r--  1 root  staff    0 Sep 25 23:04 file51727285646.log

0

Task Completed.

Harish September 25, 2024 11:09 pm

I see you have done it, pls thing about using loops.. Good Try.. pls start thinking more like programmer. 

-rwxrwxrwx  1 root  staff  275 Sep 25 23:05 Anand.sh

-r--r--r--  1 root  staff    0 Sep 25 23:05 file11727285754.log

-r--r--r--  1 root  staff    0 Sep 25 23:05 file21727285754.log

-r--r--r--  1 root  staff    0 Sep 25 23:05 file31727285754.log

-r--r--r--  1 root  staff    0 Sep 25 23:05 file41727285754.log

-r--r--r--  1 root  staff    0 Sep 25 23:05 file51727285754.log

0

Task Completed

Karthik September 25, 2024 10:52 pm
This post was modified 1 year ago by Karthik

Assignment completed.

Harish September 25, 2024 11:12 pm

@Jeeva - Not getting expected output, pls try it again

#!/bin/bash

timestamp=$(date +%s);

#create the files
touch file1$timestamp.log file2$timestamp.log file3$timestamp.log file4$timestamp.log file5$timestamp.log

previous=$?
if [ $previous -eq 0 ]
then
	echo "Files are created successfully"
fi

#Change the files Permission
chmod 444 file1$timestamp.log file2$timestamp.log file3$timestamp.log file4$timestamp.log file5$timestamp.log

previous=$?
if [ $previous -eq 0 ]
then
	echo "read only permissions updated to all the files"
fi
Harish September 25, 2024 11:27 pm

Karthik you have some interesting stuff, however your program is not scalable and bit raw. Good Try. 

-rwxrwxrwx  1 root  staff  484 Sep 25 23:13 Karthik.sh

-r--r--r--  1 root  staff    0 Sep 25 23:16 file11727286371.log

-r--r--r--  1 root  staff    0 Sep 25 23:16 file21727286371.log

-r--r--r--  1 root  staff    0 Sep 25 23:16 file31727286371.log

-r--r--r--  1 root  staff    0 Sep 25 23:16 file41727286371.log

-r--r--r--  1 root  staff    0 Sep 25 23:16 file51727286371.log

Answer
© Copyright 2024, All rights reserved by HeyCloud Innovations LLP | designed by ColorWhistle | Privacy Policy | Terms and Conditions