Introduction to Linux Assignments

Start your DevOps Journey

Prerequisites of the Linux Assignments.

Some amazing blogs to get you there.

OS Info

#1 Go to your terminal and find out information of your operating system.

  • Get info of OS

    lsb_release -a

  • Another way to get info of OS

    cat /etc/os-release

  • Display OS info

    uname -a

  • CPU info

    lscpu

  • RAM info

    lsmem

Sys log

#2 View Syslog of your system.

Services, kernel, daemons on our OS are constantly doing something, so to keep track of them, Linux have syslog.Syslog is kind of journal of event happenings inside Linux OS. It lies inside var directory.

  • View syslog

    cat /var/log/syslog

  • Search in syslog using grep

    cat /var/log/syslog | grep yourQuery

User Management

#3 Add a new user to the system and create password for this user

Sometimes when you are working in a team, one system have mutiple users with different permissions given by Admin. Create a new user with a name as you like.

  • Add a new user

    useradd bob

  • See the manual of useradd man useradd
    When invoked without the -D option, the useradd command creates a new user account using the values specified on the command line plus the default values from the system. Depending on command line options, the useradd command will update system files and may also create the new user's home directory and copy initial files. By default, a group will also be created for the new user (see -g, -N, -U, and USERGROUPS_ENAB).

    Now create a user with a default shell of BASH

    useradd -D -s /bin/bash

    -s

    flag use for the shell

  • Add a new user with its home directory using -m flag

    useradd -m bob

  • Create a password for this user

    passwd bob

  • List of all users in the system

    cat /etc/passwd

Group Management

#4 Create a new group docker and add our user to this group

In a system we can have multiple groups which can have multiple users.

  • Create a new group 'docker'

    groupadd docker

  • Get info of this group

    getent group docker

  • Add user to this group

    usermod -aG docker bob

  • Check group of the user

    groups bob


  • aditional excercise

  • Switch from the current user to this new user

    su - bob

Workshop #2

Will be live soon.

Change the Prompt of the user.

Normally we have username@OSname $, but we can change the prompt.It is saved in PS1 environment variable in .bashrc file.

  • Change prompt

    echo "PS1 = "MyNewPrompt $ "" >> .bashrc

    >> it append the new variable at the end of .bashrc file.

  • Permanently change it using VIM editor

    vim .bashrc

    Go to the end of file and add the following

    PS1=" MyNewPrompt $ "

    flag use for the shell

  • Load the .bashrc file to see changes in terminal

    source .bashrc

Making Directories

  • Create two new directories

    mkdir dir1 dir2

  • Copy first directory in 2nd

    cp -r dir1 dir2

Creating, copying,moving files.

  • Create a file

    touch file.txt

  • Copy file to a directory

    cp file.txt /dir2

  • Now move this file out of folder to previous directory

    mv file ..

    Install packages

    For Ubuntu Based Distros

  • Install a package

    sudo apt install packageName

  • For Arch Linux

  • Install a package

    sudo pacman -S packageName

Wokshop #3

Learn about PIPE, changing permissions and writing scripts using VIM. Open your terminal and just follow along.

PIPE

| take output from one command and give it to another command as input.

  • search in syslog file

    cat /var/log/syslog | grep error | less


    cat will display syslog, pipe will take this output and give it to grep as input

Write a script using VIM.

We will be going through VIM editor basics, but for now just follow along.

    Create a empty file using 'touch' command.

  • Edit it using VIM

    vim file.txt


    VIM is available in almost every Linux Distribution

  • Now you see VIM interface, btw you can't do anything using mouse.

  • Enter Insert Mode

    i

    press i, and you will enter VIM insert mode and now you can write.

Changing permissions of a file.

Sometimes we don't want a certain user of group to have all permissions of a file, so we have to some modify permission of a user or a group.

Type ls -la in your terminal

drwxr-xr-x 5 root root 4096 Aug 7 08:02 ..
  • d denotes directory

  • rread permission

  • wwrite permission

  • xexecution permission

  • -no permission

  • rwxfrom left side are the permissions of user

  • r-xare the permission of group

  • r-xare the permissions of other users

    • Give permission of execution to user of a file.

      chmod u+x file.txt


      u is user

      g is group

      o are other users

      + giving permission

      - removing permission

    • Give all permission to all users and groups.

      chmod +777 file.txt

      Number notation of permissions.

      4read permission

      2 is write permission

      1 is execution permission

      first number is for users, followed by groups and other users.

Basics of VIM

Vim is a lightweight powerful kickass editor. Just bare with me and you will do just fine.


VIM folows keyboard only rule, means we are not going to touch mouse. Keeping your fingers only on keyboard will increase your efficency as a developer.


VIM have 3 modes.

  • Command Mode
  • As name suggest, work like a terminal and open by default when you open VIM.

  • Insert Mode
    • Pressi to enter Insert mode, in this mode we can write our code in VIM .

    • Press esc to exit Insert Mode and by default you will be in Command Mode
  • Visual Mode

    Press vin command mode to enter Visual Mode, here you can mainly select text to cut, copy,and delete.

    • Select text by arrow keys
    • x to cut selected text, also used for deleting.
    • dd Delete the current line.
    • y Copy the selected text.
    • yy copy the current line.
    • ypaste the copied text before cursor.
  • Now comes the tough part, exiting VIM.

    First enter the Command Mode using esc

    • :x saves and quit.
    • :wq write and the quit
    • :q! Quit without saving.
    • u undo your last action
    • ctrl-r redo your last action