Linux Basic All Command

Working with Linux CLI:
=======================

[student@hostX Desktop] $
[root@hostX    Desktop] #
  1       2        3    4

  1: user name
  2: hostname
3: user's current location
4: user types (root: #, regular user: $)

Linux User's Types:
-------------------
 => root user: Administrator (#)
 => system user: service (mail/ftp/games/daemon)-cannot login
 => regular user: student, guest, sakib ($)

Working with Linux Shells & Terminal:
------------------------------------
[student@hostX Desktop] $ echo $SHELL  [for show shell is whice Mode bash/sh/other]
[student@hostX Desktop] $ chsh -l    [change login shell or how many shell mode working on osllrn] [L]

Working with linux CLI:
-----------------------
 => Physical Consoles (Alt + Ctrl+ F1 - Alt + Ctrl + F6)
=> Alt + Ctrl + F1: GUI
=> Alt + Ctrl + F2-F6: CMD

 => Pseudo Consoles (/dev/pts/x) or connection from GUI
 => Telnet (unencrypted, TCP port 23)
 => SSH (encrypted, TCP Port 22)

 Alt + Ctrl + F1 => GUI                       - :0
 Alt + Ctrl + F2 => new CMD terminal (F2-F6)  - tty2-tty6

 => Press Ctl + Alt + F2

 Login: student
 pass: ******

 [student@hostXDesktop]$ su
  Password: ******

 [root@hostX ~]# exit
 [student@hostX ~]$ exit

 screen:
 ------
 $ ctrl + l = srceen clear
 $ ctrl + shift + "t" => new terminal (tab) (GUI)

Linux Command Syntax/Pattern:
-----------------------------
    # command [optoin (-)] argument

 ex # ping -c 4 172.25.11.254

[student@hostX Desktop]$ cd
[student@hostX ~]$ ls      ;list of files and dir.
[student@hostX ~]$ ll      ;file and dir properties
[student@hostX ~]$ ls -l
[student@hostX ~]$ ls -la  ; details list with hidden files and dir
[student@hostX ~]$ ls -R [show all folder and file under the directory]

ctl+d-----> in home directory from any location.

      blue - dir
      b&w - file
      red - compress (rpm/zip/rar)
      green - execute file
      yellow - device (terminal/cd/dvd/usb/hdd)
      cyan - link file
      magenta - Picture/image/media

[student@hostX ~]$ pwd   ; present working directory

  "~"   => home dir
  "/"   => root partition (My Computer)
  "/root" => root's home dir
  "/home" => user's home
    i.e.: /home/student

[student@hostX~]$ => user's home dir

 Working with Linux More Commands:
 ================================
[student@hostX ~]$ w
[student@hostX ~]$ who
[student@hostX ~]$ whoami
[student@hostX ~]$ hostname
[student@hostX ~]$ tty
[student@hostX ~]$ date
[student@hostX ~]$ cal
[student@hostX ~]$ cal 2017
[student@hostX ~]$ runlevel              ;(5-GUI, 3-CMD, n-none)
[student@hostX ~]$ uname -r            ; kernel version
[student@hostX ~]$ uname          ; OS name
[student@hostX ~]$ cat /etc/redhat-release     ; redhat/centos version
[student@hostX ~]$ top   ; task manager
[student@hostX ~]$ lastlog   ; login details
[student@hostX ~]$ free -m   ; RAM Info
[student@hostX ~]$ lscpu   [show CPU info]
[student@hostX ~]$ uptime   : system UPtiem info
[student@hostX ~]$ ip addr

[student@hostX ~]$ history  ; list of privious command
[student@hostX ~]$ !25 ; 45 no command
[student@hostX ~]$ history -c ; clear all previous history
[student@hostX ~]$ history

 Shutdown
===========
[root@hostX ~]# init 0
[root@hostX ~]# poweroff
[root@hostX ~]# shutdown -h now
[root@hostX ~]# shutdown -h 5 now  ; shutdown after 5 min

restart:
=======
[root@hostX ~]# reboot
[root@hostX ~]# init 6
[root@hostX ~]# shutdown -r now
[root@hostX ~]# shutdown -r 5 now    ; restart after 5 min
 
Linux Directory Structure:
-------------------------
[student@hostX ~]$ cd /
[student@hostX /]$ ls

bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

 bin - user binary files ( executed by regular user)
 boot - system boot related file
 dev - system device files (dvd/cd/hdd/fd)
 etc - all server & system configuration file
 home - regular user home dir
 lib - system libary files locations. libraries needed to execute the binaries in /bin/ and /sbin/.
 media - system defaut mount point (DVD/ISO/SOFTware)
 mnt - mount point (DVD/HDD/USB)
 opt - optional (empty)
 proc - Also called 'proFS' system process related info (CPU,RAM, Process, Driver, Modules and Kernel)
 root - root user (superuser) home dir.
 run - service running data. Runtime data for processes started since the last boot.
 sbin  - system binary ( used by root user)
 srv - Sort for Service. User's (/home/*) service related data. Like WWW, FTP etc.
 sys - Sort for system. '/sys' directory as a virtual filesystem (sysfs) mounted under /sys. similar as proc.
 tmp - temporary files (deleted after 10 days)
 usr - thirdparty software install location
 var - varibale file (mail/log/hosting/ftpdata)

[student@hostX ~]$ cd  [enter]  ; back to home dir
[student@hostX ~]$ cd /
[student@hostX /]$ ls
[student@hostX /]$ cd

        =>  cd  /dir1/dir2/dir3 [path]  ; Linux

=>  C:\di1\dir2\dir3>       ; Windows

[student@hostX ~]$ cd /var/log
[student@hostX log]$ ls
[student@hostX log]$ cd  ..  ; one step/dir back
[student@hostX var]$ cd  -  ; back to previous dir
[student@hostX ~]$ cd
[student@hostX ~]$ ls
[student@hostX ~]$ cd Music
[student@hostX Music]$ cd ../Videos
[student@hostX Videos]$ ls
[student@hostX Videos]$ pwd
[student@hostX Videos]$ cd
[student@hostX ~]$ mkdir linuxY
[student@hostX ~]$ ls
[student@hostX ~]$ cd linuxY
[student@hostX linuxY]$ ls
[student@hostX linuxY]$ pwd
[student@hostX linuxY]$ mkdir lesson02
[student@hostX linuxY]$ cd lesson02
[student@hostX lesson02]$ ls
[student@hostX lesson02]$ pwd

Test: Graphically (home/...........)

 class work:
 -----------
[student@hostX lesson02]$ touch file1              ; file create
[student@hostX lesson02]$ ll
[student@hostX lesson02]$ touch test1 test2 test3    ; multiple files create with single command
[student@hostX lesson02]$ ll
[student@hostX lesson02]$ mkdir dir1 dir2 dir3 ; multiple dirs create with single command
[student@hostX lesson02]$ ll
[student@hostX lesson02]$ touch file{1..10}
[student@hostX lesson02]$ touch user{1,2,3}
[student@hostX lesson02]$ touch user-{tarek,lima,liza}
[student@hostX lesson02]$ ll
[student@hostX lesson02]$ mkdir -p dir1/dir2/dir3
[student@hostX lesson02]$ ll dir1
[student@hostX lesson02]$ touch dir1/dir2/test
[student@hostX lesson02]$ cd dir1/dir2/
[student@hostX dir2]$ pwd
[student@hostX dir2]$ ll
[student@hostX dir2]$ cd /home/student/linuxY/lesson02

or

[student@hostX dir2]$ cd ../../
[student@hostX lesson02]$ rm -rf *
[student@hostX lesson02]$ ls

Working with Hidden file/dir:
----------------------------
[student@hostX lesson02]$ mkdir .training
[student@hostX lesson02]$ touch .csl
[student@hostX lesson02]$ ll
[student@hostX lesson02]$ ls -la   ; a for hidden file/dir
[student@hostX lesson02]$ mv .training training ; file rename
[student@hostX lesson02]$ ll

[student@hostX lesson02]$ touch test1
[student@hostX lesson02]$ mkdir linux1

[student@hostX lesson02]$ ll

 d rwx rwx  r-x .  2   student student   6 Feb  4 18:06 linux1
 - rw- rw-  r-- .  1   student student   0 Feb  4 18:06 testY

 1 2a  2b   2c 2d  3    4        5       6     7           8

 1: file/dir types
 2: file/dir permission:   2a: user permission, 2b: group permission, 2c: others permission 2d: ACL Permission
 3: file/dir link (Hard Link)
 4: file/dir owner
 5: file/dir group owner
 6: file/dir size (byte)
 7: file/dir modify date
 8: file/dir name

Linux FIle & Dir types:
------------------------------
 d = directory     : regular directory
 l = link file    : /dev/stdin
 s = socket        : /dev/log   
 - = regular file : text/any file
 p = Pipe file  : /dev/initctl     
 b = device CD/DVD/HDD : /dev/sdb, /dev/sr0
 c = character device (serial/prallel/printer): /dev/tty

s - A socket file is used to pass information between applications/process for communication purpose

[root@hostXlesson02]$ cd /dev
[student@hostX dev]$ ll
[student@hostX dev]$ cd -

 FIle/dir Permission:
 ---------------------------
 r = read
 w = write
 x = execute
 - = no permission
 . = Special Permission (+)

Copy/paste/remove/rename/delete
===============================
[student@hostX lesson02]$ touch file1
[student@hostX lesson02]$ mkdir dir1
[student@hostX lesson02]$ ls
[student@hostX lesson02]$ cp file1 file2          ; file copy
[student@hostX lesson02]$ ll
[student@hostX lesson02]$ cp file1 /home/student       ; same name
[student@hostX lesson02]$ cp file1 /home/student/file3  ; diffrent name
[student@hostX lesson02]$ cd
[student@hostX ~]$ ls
file1
file3
[student@hostX ~]$ cd -
[student@hostX lesson02]$ cp /etc/passwd  .  ; copy to current dir [copy a file from other directory]
[student@hostX lesson02]$ ls
[student@hostX lesson02]$ cp /etc/hostname  /home/student
[student@hostX lesson02]$ cd
[student@hostX ~]$ ls
[student@hostX ~]$ cd -
[student@hostX lesson02]$ cp dir1 linux99      ; wrong command
[student@hostX lesson02]$ cp -r dir1 linux99   ; copy directory
[student@hostX lesson02]$ cp -r /etc/ . [bring etc directory in present directory]
[student@hostX lesson02]$ rm file1            ; file remove
[student@hostX lesson02]$ ls
[student@hostX lesson02]$ mv file2 file4   ; move or rename
[student@hostX lesson02]$ ls
[student@hostX lesson02]$ mv file4 /home/student
[student@hostX lesson02]$ ls
[student@hostX lesson02]$ rm linux99     ; delete empty dir
[student@hostX lesson02]$ rm -r linux99     ; delete empty dir
[student@hostX lesson02]$ rm -r etc   ; delete dir with content and confirmation
 C^
[student@hostX lesson02]$ rm -rf etc   ; delete dir with contains and without confirmation

[student@hostX lesson02]$ ls
[student@hostX lesson02]$ rm -rf *  ; delete everything from curent dir
[student@hostX lesson02]$ ls

 ===================== The End =======================


 Linux Text Editor:
-------------------
 Windows Text Editor: notepad, notepadd++
 Linux Text Editor: vi/vim, nano, gedit (GUI), emacs, pico

  Most propular/Advanced Text Editor: Vi/Vim
        => vi - old, b&w, default
=> vim - advanced, colorfull (Package must be installed)

 [student@hostX ~]$ cd
 [student@hostX ~]$ mkdir linux/lesson04 -p
 [student@hostX ~]$ cd linux/lesson04
 [student@hostX lesson04]$ ls
 [student@hostX lesson04]$ touch test          ; create new file
 [student@hostX lesson04]$ cp /etc/passwd  .   ; copy passwd file to cureent dir
 [student@hostX lesson04]$ ls
  passwd  test

 [student@hostX lesson04]$ vim file1    ; edit newfile
  welcome to csl training

 :x

 [student@hostX lesson04]$ cat file1

 [student@hostX lesson04]$ nano file2
   welcome to cslcbt training

 => Ctrl+X then 'Y'

 [student@hostX lesson04]$ cat file2

 [student@hostX lesson04]$ gedit file3

 welcome to cslcbt training

 [student@hostX lesson04]$ cat file3

 Working with vi/vim Mode:
 ------------------------
=> Insert Mode : press "i" or "insert" button
        => Exit Mode : press "esc" button form keyboard
        => Command Mode: file navigation, cut, paste, undo, redo
=> Visual Mode: press "ctrl + v" form keyboard

 [student@hostX lesson04]$  vim myfile

      => Press "i" for insert mode
      => write something as your requirments ; (Hello world !!)
      => press "esc" button exit from "insert" mode
      => :x      ; save and quit

 [student@hostX lesson04]$ ls
 [student@hostX lesson04]$ cat myfile
  Hello world !!

 [student@hostX lesson04]$ vim myfile

  => Press "i" for insert mode
  => write "welcome to linux training"
  => press "esc" quit from insert mode
  => :w      ; only save not quit

  => Press "i" for insert mode
  => write "goodbye"

  => :q!    ; quit without save

 [student@hostX lesson04]$ cat file1

 [student@hostX lesson04]$  vim passwd
   :set nu

Cursor Movement:
----------------
   h j k l - Left, down, up, right
   ^ - Go to the beginning of the line
   $ - Go to the end of the line
   gg - Go to the frst line
   G - Go to the last line
   :n - Go to line n

Replacing Text:
---------------
   r - Replace character

Copy/Paste:
-----------
   yl      - Single Character copy
   nyl        -  no of Character copy
   yy      -  Yank line
   p         -  Paste after cursor
   P      -  Paste before cursor
   yw        -  word copy
   nyy      -  n lines copy 

Inserting Text:
---------------
   i - Insert at cursor
   I - Insert at the beginning of the line
   a - Append after cursor
   A - Append at the end of the line
   o - Open a new line below the current line
   O - Open a new line above the current line

Deleting Text:
--------------
  x  - delete (cut) character
  dd - delete (cut) line
  ndd           - delete (cut) n lines
  dw - delete (cut) word
  D             - delete (cut) to the end of the line

Undo/Redo:
---------
  u - Undo
  Ctrl-r - Redo

Searching:
----------
  /{pattern} - Forward search for {pattern}
  ?{pattern} - Reverse search for {pattern}
  n - Repeat the last search
  N - Repeat the last search in the opposite direction

Find and Replace:
-----------------
 :%s/{old}/{new}/gc  - Search and Replace with confirmation  [without any space]
 :%s/{old}/{new}/g  - Search and Replace without confirmation

Save and Quit:
-------------
 :w - Write (save)
 :wq - Write and quit
 :q - Quit
 :q! - Force quit, don’t save changes
 :wq! Force write and quit
-I for case sendative
-i case insensative

 #vimtutor

 ======================== Thank you ===================


Lesson: User and Group Administration
-------------------------------------
[root@desktopX ~]# less /etc/passwd

 UID
 ------
 root : 0
 system user: 200 - 999
 regular user: 1000 +

[root@desktopX ~]# useradd tarek
[root@desktopX ~]# tail /etc/passwd 
[root@desktopX ~]# grep tarek /etc/passwd

tarek: x: 1001: 1001:   :/home/tarek  :/bin/bash
  1    2   3     4    5       6           7

1 - username
2 - user password info (/etc/shadow)
3 - userid (UID)
4 - groupid (GID): primary
5 - user's comment/descriptions
6 - user's home dir
7 - user's shell

[root@desktopX ~]# id tarek
uid=1001(tarek) gid=1001(tarek) groups=1001(tarek)

[root@desktopX ~]# tail /etc/shadow    ; user password related info
[root@desktopX ~]# grep tarek /etc/shadow    ; tarek password related info

[root@desktopX ~]# useradd mahfuz   ; user create
[root@desktopX ~]# useradd mamun

[root@desktopX ~]# passwd mahfuz
[root@desktopX ~]# tail /etc/shadow

Alt+Ctrl+F2 -- Alt+Ctrl+F6

login: mahfuz        ; login as regular user
pass: 123            ; (note: please numlock on)

[mahfuz@desktopX ~]$ exit


[root@desktopX ~]# groupadd trainer      ; group add
[root@desktopX ~]# groupadd staff         ; group add

[root@desktopX ~]# tail /etc/group     ; group related info

trainer :x:  1003:
   1     2    3   4
 1 - group name
 2 - group password info (/etc/gshadow)
 3 - gid
 4 - group members

[root@desktopX ~]# grep trainer /etc/group  ; check trainer group
trainer:x:1003:

[root@desktopX ~]# usermod -G trainer tarek  ;existing user modify
[root@desktopX ~]# useradd -G trainer  belal  ; newuser to group

[root@desktopX ~]# grep trainer /etc/group
trainer:x:1003:tarek,belal

[root@desktopX ~]# useradd ikbal
[root@desktopX ~]# passwd ikbal

[root@desktopX ~]# usermod -G trainer,staff  ikbal ; single user assign to multiple groups
[root@desktopX ~]# grep staff /etc/group
[root@desktopX ~]# grep trainer /etc/group

[root@desktopX ~]# id ikbal

[root@desktopX ~]# useradd -u 3000 roman ; user careate with UID
[root@desktopX ~]# grep roman /etc/passwd

[root@desktopX ~]# groupadd -g 3100 admin
[root@desktopX ~]# tail /etc/group

[root@desktopX ~]# groupmod -n faculty trainer  ;change group name
[root@desktopX ~]# tail /etc/group

[root@desktopX ~]# gpasswd -d ikbal staff       ; remove from group
[root@desktopX ~]# grep staff /etc/group

[root@desktopX ~]# tail /etc/shadow
[root@desktopX ~]# passwd -d ikbal               ; password remove
[root@desktopX ~]# tail /etc/shadow

Login regular user:
-------------------
Linux GUI terminal: 1   (Alt + Ctrl + F1)
Linux Command Terminal: (Alt+Ctrl+F2  -  Alt + Ctrl + F6)

Login as root user
-----------------
[root@desktopX ~]# id rafat
[root@desktopX ~]# useradd rafat
[root@desktopX ~]# passwd rafat
[root@desktopX ~]# grep rafat /etc/passwd
rafat:x:1003:1003::/home/rafat:/bin/bash

[root@desktopX ~]# usermod -c "Linux X student" rafat

[root@desktopX ~]# grep rafat /etc/passwd
rafat:x:1000:1000:Linux X student:/home/rafat:/bin/bash

[root@desktopX ~]# grep rafat /etc/passwd
rafat:x:1003:1003::/home/rafat:/bin/bash

[root@desktopX ~]# mkdir -p /newhome/rafat2
[root@desktopX ~]# usermod -d /newhome/rafat2 rafat

[root@desktopX ~]# grep rafat /etc/passwd
rafat:x:1003:1003: :/newhome/rafat2:/bin/bash

[root@desktopX ~]# cat /etc/shells

[root@desktopX ~]# id student

[root@desktopX ~]# grep student /etc/passwd
[root@desktopX ~]# usermod -s /sbin/nologin  student     ;change user shell
[root@desktopX ~]# grep student  /etc/passwd
student :x:1002:1002::/home/student :/sbin/nologin

Check: Alt + Ctrl +  F3 (use username password)

[root@desktopX ~]# usermod -s /bin/bash student    ;shell enable

Check: Alt + Ctrl +  F3  (use username password)

[root@desktopX ~]#  usermod -L student     ; user  account    lock
[root@desktopX ~]# grep student /etc/shadow
student: ! $.............../:16106:10:30:7:::

Check: Alt + Ctrl +  F3 (use username password)

[root@desktopX ~]#  grep student /etc/shadow
[root@desktopX ~]# tail /etc/shadow

[root@desktopX ~]#  usermod -U student    ; user  account  unlock
[root@desktopX ~]#  grep student /etc/shadow
student: $.............../:16106:10:30:7:::

[root@desktopX ~]# userdel rafat ; user delete without home dir

or

[root@desktopX ~]# userdel -r rafat  ; delete user with home dir
[root@desktopX ~]# cat /etc/passwd

[root@desktopX ~]# groupdel admin ; groupdel
[root@desktopX ~]# tail /etc/group     

[root@desktopX ~]# useradd mahedi
[root@desktopX ~]# passwd mahedi

Alt+Ctrl+F3

Login:  mahedi
 Pass: *****

[mahedi@desktopX ~]$ useradd rasel
 -bash: /usr/sbin/useradd: Permission denied

 What is SUDO do ?
 -----------------
 Sudo allows a permitted user to execute a specifc command or a
 group of commands or all commands as the superuser.

  regular user: rumon, rony, lucky
=> rm,cp,mv,
=> mkdir,touch
=> pwd,free -m,
=> ping, df -HT
=> ip addr, tail

 Command run from: /bin/

  super user: root
=> useradd, passwd, groupadd
=> reboot, systemctl
=> shutdown, poweroff
  => setenforce

 Command run from: /sbin

Note:  [root@desktopX ~]# which useradd  ; command for location of   useradd command
       [root@desktopX ~]# which pwd

 Editing sudo configuration File:
 --------------------------------
 Rules 1: permit for all
 -----------------------
 [root@desktopX ~]# visudo

 :set nu

 91   root    ALL=(ALL)    ALL
 92   mahedi  ALL=(ALL)    ALL    ; mahedi allow for any command

:x (save and exit)

Test:
-----
Press: Alt + Ctrl + F3

[mahedi@desktopX ~]$ useradd rasel

[mahedi@desktopX ~]$ sudo useradd rasel
[sudo] password for mahedi: ****
[mahedi@desktopX ~]$ tail /etc/passwd
[mahedi@desktopX ~]$ exit

Working with /etc/shadow file:
------------------------------
[root@desktopX ~]# useradd lucky
[root@desktopX ~]# passwd lucky

[root@desktopX ~]# tail /etc/shadow | grep lucky
lucky:$6$ciiMIfom$cPpqBIf2NOwan2byi5BUA.G6D0iM/g.tw7fcUyLDWIs.nbp0:17711:0:99999:7:::

Alt+F2

Login: lucky
passL: 123

[root@desktopX ~]# chage -l lucky          ;password info

Last password change                                    : MM DD, YYYY
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

password: P@ssword123  (new password)

[root@desktopX ~]# chage lucky
 Minimum Password age [0]: 3
 Maximum Password age [99999]: 30
 Last Password Changed (YYYY-MM-DD): Press Enter (today)
 Password Expiration Warning [7]: 5
 Password Inactive [-1]: 5
 Account Expiration Date (YYYY-MM-DD) [-1]: YYYY-MM-DD

 note: If press Enter account never expire

[root@desktopX ~]# date
[root@desktopX ~]# date MMDDHHMMYY
[root@desktopX ~]# date

[root@desktopX ~]# vim /etc/login.defs  ;user password related info

 25  PASS_MAX_DAYS   99999
 26  PASS_MIN_DAYS   0
 27  PASS_MIN_LEN    5
 28  PASS_WARN_AGE   7

[root@desktopX ~]# chage -d 0 lucky

===================================  x  ===========================

Extra:

 Rules 2: shutdown disallow
 --------------------------
 52  Cmnd_Alias SHUTDOWN = /usr/sbin/shutdown, /usr/sbin/poweroff, /usr/sbin/reboot
 92  mahedi ALL=(ALL) ALL, !SHUTDOWN

  Rules 3: permit for specific command
 ------------------------------------
 52  Cmnd_Alias MAHEDI = /usr/sbin/useradd, /usr/sbin/userdel
 92  mahedi ALL=(ALL)  MAHEDI

Rules 4: permit group (support) for specific command
 ----------------------------------------------------

 52  Cmnd_Alias SUPPORT = /usr/sbin/fdisk, /usr/sbin/passwd,
 109 %support  ALL=(ALL)       SUPPORT

 =================== The End ===============


No comments:

Post a Comment