Table of contents
System Information Commands
Display system information:
uname
Show how long the system has been running:
uptime
Display the current date and time:
date
Show who is currently logged in:
who
Show the current user:
whoami
Bash and User ID Commands
Start a new bash session:
bash
Locate the bash executable:
which bash
Locate the
cp
command:which cp
Display user ID and group ID:
id
Using sudo
Show the contents of the
/etc/passwd
file:cat /etc/passwd
Navigate to the root directory (requires root permissions):
cd /root
Shutdown the system:
sudo shutdown
Cancel a scheduled shutdown:
sudo shutdown -c
Reboot the system:
sudo reboot
Update package lists:
sudo apt-get update
Upgrade all packages:
sudo apt-get upgrade
Install Docker:
sudo apt install docker.io sudo apt-get install docker.io
Remove Docker:
sudo apt remove docker.io
Package Managers for Different Distributions
Package manager for CentOS:
yum
Package manager for Fedora:
dnf
Package manager for Arch Linux:
pacman
Package manager for Gentoo:
portage
User and Group Management
Creating and Managing Users
Add a new user named "rider":
sudo useradd -m rider
Navigate to the home directory and list users:
cd .. ls home/
Output:
dhruv rider
Set a password for the user "rider":
sudo passwd rider
Switch to the user "rider":
su rider whoami
Output:
rider
Navigate and list directories:
ls .. cd rider
Exit the user session and return to the previous user:
exit whoami
Output:
dhruv
Deleting Users
Delete the user "rider":
sudo userdel rider
Try switching to the deleted user (should fail):
su rider
Creating and Managing Groups
Add new users "u1", "u2", "u3", and "u4":
sudo useradd u1 sudo useradd u2 sudo useradd u3 sudo useradd u4
Create a new group named "devops":
sudo groupadd devops
List groups to verify the creation:
cat /etc/group
Create another group named "tester":
sudo groupadd tester
Add users "u1" and "u2" to the "devops" group:
sudo gpasswd -a u1 devops sudo gpasswd -a u2 devops
Add users "u3" and "u4" to the "tester" group:
sudo gpasswd -M u3,u4 tester
Delete the "tester" group:
sudo groupdel tester
File Permissions Table
Permission Breakdown
Directory | User (Owner) | Group | Other |
d | rwx | rwx | r-x |
Permission Values
Value | Permission | User | Group | Other |
0 | --- | - | - | - |
1 | --x | - | - | x |
2 | -w- | - | w | - |
3 | -wx | - | w | x |
4 | r-- | r | - | - |
5 | r-x | r | - | x |
6 | rw- | r | w | - |
7 | rwx | r | w | x |
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.
Understanding file permissions:
Directory:
d
User (Owner):
rwx
Group:
rwx
Other:
r-x
Permission values:
r
(read): 4w
(write): 2x
(execute): 1
Display file permissions with
ls -l
:ls -l
Change file permissions:
chmod 777 one.txt ls -l
Umask Values
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
Create a new file and display its owner:
mkdir riderfile.txt ls -l
Change the file owner to "rider":
sudo chown rider riderfile.txt ls -l
Change the file group to "devops":
sudo chgrp devops riderfile.txt
Compression Commands
Compress a directory using
zip
:zip -r lfd.zip linuxfordevops/ cp lfd.zip copy/unzipfiles/ ls copy/unzipfiles/ cd copy/unzipfiles/ unzip lfd.zip
Compress a directory using
tar
:tar -cvzf linuxfordevops.tar.gz linuxfordevops ls -l
Extract a tar file:
tar -xvzf linuxfordevops.tar.gz
tar
Command Options and Descriptions
Option | Description |
-c | Create a new archive |
-v | Verbosely list files processed |
-f | Specify the filename of the archive |
-z | Compress the archive using gzip |
-j | Compress the archive using bzip2 |
-x | Extract files from an archive |
-t | List the contents of an archive |
-r | Append files to the end of an archive |
-u | Only append files newer than copy in archive |
-W | Verify the archive after writing it |
-k | Do not overwrite existing files |
--delete | Delete files from an archive |
--exclude | Exclude files matching a pattern |
-C | Change to directory |
--strip-components | Strip leading components from file names on extraction |
Common Examples
Create a compressed archive:
tar -cvzf archive-name.tar.gz /path/to/directory
Extract a compressed archive:
tar -xvzf archive-name.tar.gz
List contents of an archive:
tar -tvf archive-name.tar.gz
Append files to an archive:
tar -rvf archive-name.tar /path/to/file
Delete files from an archive:
tar --delete -f archive-name.tar /path/to/file
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
Create a test directory on the local system:
cd Documents mkdir scp_test
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
Verify the transferred file:
ls cat secretfile.txt
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
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.