Linux stuffs

echo "Hello world"

We are going to learn:

  1. User / group
  2. grep, awk, find.
  3. File permission
  4. ssh/scp
  5. systemctl
  • Groups: collection of users

    sudo groupadd devops
    

    use the /etc/group file to get groups

    Whenever you create a user it creates groups also with the same name.

    • Add user to group

      whenever you add a user to a group you modify the user.

      sudo usermod -aG devops user1
      
    • To add multiple users, we can also use gpasswd to add user

      sudo gpasswd -M user1,user2,user3 testers
      
    • Delete user form group

      sudo gpasswd -d user2 testers
      
    • Delete group

      sudo groupdel testers
      
    • Use chgrp to change group of file

      sudo chgrp tester file.txt
      

  • File permissions:

    Numeric Permissions:

    • r - read = 4
    • w - write = 2
    • x - execute = 1

     

    • There are three set of permissions for:

      • U - User/Owner
      • G - Group
      • O - Others

     

    • Combinations

      _ U _ . _ G _ . _ O _ => rwx rwx rwx

      Example:

      U - read+write = 4+2 => 6

      G - read+executable = 4+1 => 5

      O - read = 4 => 4

      Final permission numeric == 654

     

    • User chmod command to modify permissions

      chmod 700 file.txt
      

 

  • Find files

    • Grep: To find anything inside file or name of files.

      • Find some keyword or filename in directory.
      grep -r keyword /home/ubuntu/
      

      -r is for searching recursively inside directotries

      • grep something inside file
      grep keyword file.txt
      
      • Search case insensitive
      grep -i keyword file.txt
      

      -i stands for case insensitive, by default grep is case sensitive.

       

    • AWK:

      • Find TRACE from log and print column 1,2,5
      awk '/TRACE/ {print $1,$2,$5}' errors.log
      
      • Get the exact line number
      awk '/TRACE/ {print NR,$1,$2,$5}' errors.log
      
      • User condition using NR => row number
      awk 'NR>=1 && NR<=20 && /TRACE/ {print NR,$1,$2,$5}' errors.log
      

     

    • Find:

      • Find files with specific extension

        find *.txt
        
      • Find files in some directory with specific ending

        find dir/ *.log
        
      • Find files by a particular owner

        find dir/ -user ubuntu
        
      • Find files by a particular group

        find dir/ -group devops
        

     

  • SSH:

    • Keys:

      • Public key (id_rsa.pub)
      • private key (id_rsa)

        Public key must be known by the server where you want to connect, and you must also have private key to connect.

       

    • Create keys:

      ssh-keygen
      

      Keys are stored in ~/.ssh/ directory.

    • Connect using keys:

      1. Permission must be 400

        chmod 400 key.pem
        
      2. connect

        ssh -i /path/to/key.pem user@ipaddress
        
      3. Types yes when asked.
    • scp from host system to server

      scp -i "key.pem" file.txt user@ipadd:/home/user/file.txt
      
    • scp from server to our host system

      scp -i "key.pem" user@ipadd:/home/user/file .
      

       

  • Systemctl

    It is a Service controller, services like docker, apache2, sshd, nginx, etc.

    • Start any service

      sudo systemctl start <service> 
      
    • Stop any service

      sudo systemctl stop <service>
      
    • Check status of any service

      sudo systemctl status <service>