Part 2: System Level Commands and User Management in Linux

Part 2: System Level Commands and User Management in Linux

System Information Commands

  1. Display system information:

     uname
    
  2. Show how long the system has been running:

     uptime
    
  3. Display the current date and time:

     date
    
  4. Show who is currently logged in:

     who
    
  5. Show the current user:

     whoami
    

Bash and User ID Commands

  1. Start a new bash session:

     bash
    
  2. Locate the bash executable:

     which bash
    
  3. Locate the cp command:

     which cp
    
  4. Display user ID and group ID:

     id
    

Using sudo

  1. Show the contents of the /etc/passwd file:

    cat /etc/passwd
    
  2. Navigate to the root directory (requires root permissions):

    cd /root
    
  3. Shutdown the system:

    sudo shutdown
    
  4. Cancel a scheduled shutdown:

    sudo shutdown -c
    
  5. Reboot the system:

    sudo reboot
    
  6. Update package lists:

    sudo apt-get update
    
  7. Upgrade all packages:

    sudo apt-get upgrade
    
  8. Install Docker:

    sudo apt install docker.io
    sudo apt-get install docker.io
    
  9. Remove Docker:

    sudo apt remove docker.io
    

Package Managers for Different Distributions

  1. Package manager for CentOS:

    yum
    
  2. Package manager for Fedora:

    dnf
    
  3. Package manager for Arch Linux:

    pacman
    
  4. Package manager for Gentoo:

    portage
    

User and Group Management

Creating and Managing Users

  1. Add a new user named "rider":

    sudo useradd -m rider
    
  2. Navigate to the home directory and list users:

    cd ..
    ls home/
    

    Output:

    dhruv rider
    
  3. Set a password for the user "rider":

    sudo passwd rider
    
  4. Switch to the user "rider":

    su rider
    whoami
    

    Output:

    rider
    
  5. Navigate and list directories:

    ls ..
    cd rider
    
  6. Exit the user session and return to the previous user:

    exit
    whoami
    

    Output:

    dhruv
    

Deleting Users

  1. Delete the user "rider":

    sudo userdel rider
    
  2. Try switching to the deleted user (should fail):

    su rider
    

Creating and Managing Groups

  1. Add new users "u1", "u2", "u3", and "u4":

    sudo useradd u1
    sudo useradd u2
    sudo useradd u3
    sudo useradd u4
    
  2. Create a new group named "devops":

    sudo groupadd devops
    
  3. List groups to verify the creation:

    cat /etc/group
    
  4. Create another group named "tester":

    sudo groupadd tester
    
  5. Add users "u1" and "u2" to the "devops" group:

    sudo gpasswd -a u1 devops
    sudo gpasswd -a u2 devops
    
  6. Add users "u3" and "u4" to the "tester" group:

    sudo gpasswd -M u3,u4 tester
    
  7. Delete the "tester" group:

    sudo groupdel tester
    

File Permissions Table

Permission Breakdown

DirectoryUser (Owner)GroupOther
drwxrwxr-x

Permission Values

ValuePermissionUserGroupOther
0------
1--x--x
2-w--w-
3-wx-wx
4r--r--
5r-xr-x
6rw-rw-
7rwxrwx

Legend

  • r: read

  • w: write

  • x: execute

In the above tables, the permission breakdown shows the different levels of access for directories, users, groups, and others. The permission values table provides a detailed view of the numerical representation of permissions and what they stand for.

  1. Understanding file permissions:

    • Directory: d

    • User (Owner): rwx

    • Group: rwx

    • Other: r-x

Permission values:

  • r (read): 4

  • w (write): 2

  • x (execute): 1

  1. Display file permissions with ls -l:

    ls -l
    
  2. Change file permissions:

    chmod 777 one.txt
    ls -l
    

Umask Values

  1. Understanding Umask settings:

    | Umask | User Access | Group Access | Other | | --- | --- | --- | --- | | 0000 | all | all | all | | 0002 | all | all | read, execute | | 0007 | all | all | none | | 0022 | all | read, execute | read, execute | | 0027 | all | read, execute | none | | 0077 | all | none | none |

Changing Ownership and Group

  1. Create a new file and display its owner:

    mkdir riderfile.txt
    ls -l
    
  2. Change the file owner to "rider":

    sudo chown rider riderfile.txt
    ls -l
    
  3. Change the file group to "devops":

    sudo chgrp devops riderfile.txt
    

Compression Commands

  1. Compress a directory using zip:

    zip -r lfd.zip linuxfordevops/
    cp lfd.zip copy/unzipfiles/
    ls copy/unzipfiles/
    cd copy/unzipfiles/
    unzip lfd.zip
    
  2. Compress a directory using tar:

    tar -cvzf linuxfordevops.tar.gz linuxfordevops
    ls -l
    
  3. Extract a tar file:

    tar -xvzf linuxfordevops.tar.gz
    

tar Command Options and Descriptions

OptionDescription
-cCreate a new archive
-vVerbosely list files processed
-fSpecify the filename of the archive
-zCompress the archive using gzip
-jCompress the archive using bzip2
-xExtract files from an archive
-tList the contents of an archive
-rAppend files to the end of an archive
-uOnly append files newer than copy in archive
-WVerify the archive after writing it
-kDo not overwrite existing files
--deleteDelete files from an archive
--excludeExclude files matching a pattern
-CChange to directory
--strip-componentsStrip leading components from file names on extraction

Common Examples

  1. Create a compressed archive:

     tar -cvzf archive-name.tar.gz /path/to/directory
    
  2. Extract a compressed archive:

     tar -xvzf archive-name.tar.gz
    
  3. List contents of an archive:

     tar -tvf archive-name.tar.gz
    
  4. Append files to an archive:

     tar -rvf archive-name.tar /path/to/file
    
  5. Delete files from an archive:

     tar --delete -f archive-name.tar /path/to/file
    
  6. Extract files to a specific directory:

     tar -xvzf archive-name.tar.gz -C /path/to/destination
    

These options and examples cover a range of typical tar command use cases for creating, managing, and extracting archive files.

File Transfer with SCP

  1. Create a test directory on the local system:

    cd Documents
    mkdir scp_test
    
  2. Transfer a file from a remote server to the local system:

    scp -i "/User/dhruv/Downloads/linux-for-devops-key.pem" secret.file.txt ubuntu@ec2-3-15-221-86.us-east-2.compute.amazonaws.com:/home/dhruv
    
  3. Verify the transferred file:

    ls
    cat secretfile.txt
    
  4. Recursively transfer a directory from a remote server to the local system:

    scp -i "/User/dhruv/Downloads/linux-for-devops-key.pem" -r ubuntu@ec2-3-15-221-86.us-east-2.compute.amazonaws.com:/home/dhruv/linuxfordevops .
    

Synchronization with Rsync

  1. Synchronize files between local and remote systems using rsync:

    rsync -avz source_directory/ user@remote_host:/destination_directory/
    

This guide continues from Part 1, diving deeper into system-level commands, user and group management, file permissions, and essential file transfer techniques in Linux.