0% found this document useful (0 votes)
37 views15 pages

Experiment 4 HSIS Lab

Uploaded by

gopalagovinda641
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views15 pages

Experiment 4 HSIS Lab

Uploaded by

gopalagovinda641
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Linux Usage Set by Set guide

Step 1: Opening the Terminal


Shortcut: Press `Ctrl + Alt + T` on most Linux distributions.
Or
Menu Search: You can also search for "Terminal" in your application menu or launcher.

Step 2: Basic Structure of a Linux Command


General Format: `command [options] [arguments]`
Command: The action you want to perform, like `ls`, `cd`, or `cp`.
Options (optional): Modify the behavior of the command (e.g., `l` for detailed listing in `ls`).
Arguments (optional): Specify files, directories, or other targets (e.g., `ls /home`).
Meaning of ls, cd,cp
1. `ls` — List Files and Directories
The `ls` command lists files and directories in the current directory (folder) or a specified
directory.
Syntax: `ls [options] [directory]`
Examples:
`ls` — Lists all files and folders in the current directory.
`ls l` — Lists files with detailed information (like permissions, owner, size, and modification
date).
`ls /home/user` — Lists files in the `/home/user` directory.

2. `cd` — Change Directory


The `cd` command changes the current working directory to another directory you specify.
Syntax: `cd [directory]`
Examples:
`cd Documents` — Moves to the `Documents` directory inside the current directory.
`cd ..` — Goes up one level in the directory hierarchy (to the parent directory).
`cd ~` or just `cd` — Goes to the user's home directory.
3. `cp` — Copy Files and Directories
The `cp` command copies files or directories from one location to another.
Syntax: `cp [options] source destination`
Examples:
`cp file1.txt /path/to/destination/` — Copies `file1.txt` to the specified destination.
`cp r folder1 /path/to/destination/` — Recursively copies `folder1` and its contents to the
destination.

Step 3: Navigating the File System


A Bash command in Linux refers to a directive that you can enter into the Bash shell to
perform a specific task or set of tasks. Each command is generally a program or script that
can execute functions ranging from file manipulation and system monitoring to network
operations and software
1. Find Your Current Location: Type `pwd` to print your current directory (folder).
bash
pwd

2. Change Directory: Use `cd` to move to a different directory.


Example: To go to the `Documents` directory, type:
bash
cd Documents

Go back to the home directory:


bash
cd ~

3. List Files and Directories: Type `ls` to list contents.


For a detailed list, use:
bash
ls l
Step 4: Managing Files and Directories
1. Create a New Directory: Use `mkdir`.
Example: Create a directory named "test_folder":
bash
mkdir test_folder
2. Create a New File: Use `touch`.
Example: Create a file named `example.txt`:
bash
touch example.txt

3. Copy Files: Use `cp`.


Example: Copy `example.txt` to the `test_folder` directory:
bash
cp example.txt test_folder/

4. Move or Rename Files: Use `mv`.


Example: Move `example.txt` to `test_folder`:
bash
mv example.txt test_folder/

5. Delete Files: Use `rm`.


Example: Delete `example.txt`:
bash
rm example.txt

6. Delete a Directory and its Contents: Use `rm r`.


Example: Delete `test_folder` and all its contents:
bash
rm r test_folder
Step 5: Viewing and Editing Files
1. View File Content: Use `cat`.
Example: View the contents of `example.txt`:
bash
cat example.txt

2. Edit a File: Use a text editor like `nano`.


Example: Open `example.txt` in `nano`:
bash
nano example.txt

Step 6: Getting Help with Commands


1. Manual Pages: Use `man` followed by the command name for detailed info.
Example: Learn about the `ls` command:
bash
man ls

2. Quick Help: Many commands support a `help` option.


Example: Get a summary of options for `ls`:
bash
ls help

Step 7: Closing the Terminal


Exit Command: Type `exit` and press `Enter`.
```bash
exit
```
BASIC LINUX COMMANDS
Section 1: User-Level Commands
Section 2: Admin-Level Commands
Section 3: Network-Level Commands

Section 1: User-Level Commands


1. Navigation Commands
`pwd` — Print the current working directory. This shows the absolute path of the directory you’re
currently in.
`cd` — Change directory. Moves between directories.
`cd ~` or simply `cd` — Go to the home directory.
`cd /path/to/directory` — Move to a specific directory.
`ls` — List files and directories.
`ls l` — List files and directories with detailed information.
`ls a` — Include hidden files in the listing.
`ls lh` — Display sizes in humanreadable format.

2. File and Directory Management Commands


`mkdir directory_name` — Create a new directory.
Example: `mkdir my_directory`
`rmdir directory_name` — Remove an empty directory.
Example: `rmdir my_directory`

3. File Manipulation Commands


`touch filename` — Create a new empty file.
Example: `touch file.txt`
`cp source destination` — Copy files or directories.
`cp file1.txt /path/to/destination/` — Copy `file1.txt` to another location.
`cp r dir1 /path/to/destination/` — Copy a directory and its contents.
`mv source destination` — Move or rename files and directories.
`mv file.txt /path/to/destination/` — Move `file.txt` to another location.
`mv oldname.txt newname.txt` — Rename `oldname.txt` to `newname.txt`.
`rm filename` — Remove (delete) a file.
`rm file.txt` — Delete `file.txt`.
`rm r directory_name` — Remove a directory and its contents.
Example: `rm r my_directory`

4. File Display Commands


`cat filename` — View the content of a file.
`less filename` — View a file with the option to scroll up and down.
`head filename` — Show the first 10 lines of a file.
`tail filename` — Show the last 10 lines of a file.

5. File and Directory Permissions


`chmod options filename` — Change the permissions of a file or directory.
Example: `chmod 755 file.txt` — Sets the permissions to `rwxrxrx`.

6. Getting Help with Commands


`man command_name` — Show the manual for a command.
Example: `man ls` — Display the manual for `ls`.
`command help` — Display basic help for a command.
Example: `ls help`
Here's a list of essential Linux commands for managing file permissions and ownership, along with
brief descriptions.
7. View File Permissions and Ownership
`ls l`
Lists files in long format, showing permissions, owner, group, file size, and modification date.

`stat filename`
Displays detailed information about a file, including permissions, ownership, and timestamps.

8. Change File Permissions Using Symbolic Notation


`chmod u+rwx filename`
Adds read (r), write (w), and execute (x) permissions to the user (owner) for `filename`.

`chmod gw filename`
Removes write permission from the group for `filename`.

`chmod o+x filename`


Adds execute permission to others for `filename`.

`chmod u=rw,go=r filename`


Sets specific permissions: user has read and write, group and others have read only.

9. Change File Permissions Using Numeric Notation


`chmod 755 filename`
Sets permissions to read, write, execute for the owner (7), and read and execute for group and
others (5).
`chmod 644 filename`
Sets permissions to read and write for the owner (6), and readonly for group and others (4).
10. Change Ownership
`chown new_owner filename`
Changes the owner of `filename` to `new_owner`.
`chown new_owner:new_group filename`
Changes both owner and group of `filename` to `new_owner` and `new_group`.
`chown R new_owner directory_name`
Recursively changes the owner of all files and subdirectories within `directory_name` to
`new_owner`.

11. Change Group Ownership


`chgrp new_group filename`
Changes the group of `filename` to `new_group`.

`chgrp R new_group directory_name`


Recursively changes the group ownership for all files and subdirectories within
`directory_name`.

12. Additional Commands for Special Permissions


`chmod u+s filename`
Sets the SUID (Set User ID) permission on `filename`, allowing the file to run as the file owner’s
permissions.
`chmod g+s filename`
Sets the SGID (Set Group ID) permission on `filename`, allowing the file to run with group
permissions.
`chmod +t directory_name`
Sets the sticky bit on a directory, restricting deletion of files inside it to the file owner.

13. Creating a Text File


`touch filename.txt`: Creates an empty text file named `filename.txt`.
`echo "text" > filename.txt`: Creates a file and writes "text" into `filename.txt`.
`nano filename.txt` or `vim filename.txt`: Opens `filename.txt` in a text editor to add content
manually.

14. Viewing File Contents


`cat filename.txt`: Displays the entire contents of `filename.txt`.
`less filename.txt`: Opens `filename.txt` in a scrollable view; use up/down arrows to navigate.
`more filename.txt`: Similar to `less`, but only allows forward navigation.
`head filename.txt`: Shows the first 10 lines of `filename.txt`.
`tail filename.txt`: Shows the last 10 lines of `filename.txt`.
`tail f filename.txt`: Continuously monitors `filename.txt` for new content, useful for log files.

15. Searching for Patterns in a File


`grep "pattern" filename.txt`: Searches for "pattern" in `filename.txt` and displays matching lines.
`grep i "pattern" filename.txt`: Caseinsensitive search for "pattern".
`grep r "pattern" /path/to/directory`: Searches recursively for "pattern" in all files within the
specified directory.
`grep n "pattern" filename.txt`: Shows line numbers along with matching lines.
`grep v "pattern" filename.txt`: Displays lines that do not contain "pattern".

16. Replacing Text in a File


`sed 's/oldtext/newtext/' filename.txt`: Replaces the first occurrence of "oldtext" with "newtext"
in each line of `filename.txt`.
`sed 's/oldtext/newtext/g' filename.txt`: Replaces all occurrences of "oldtext" with "newtext" in
each line.
`sed i 's/oldtext/newtext/g' filename.txt`: Replaces all occurrences of "oldtext" with "newtext" in
each line, saving changes directly to `filename.txt`.
`sed 's/oldtext/newtext/2' filename.txt`: Replaces the second occurrence of "oldtext" with
"newtext" in each line.
Additional Useful Commands
`awk '{print $1}' filename.txt`: Displays the first column of each line in `filename.txt`, useful for
structured data.
`tr '[:lower:]' '[:upper:]' < filename.txt`: Converts all lowercase letters to uppercase in
`filename.txt`.
`sort filename.txt`: Sorts lines in `filename.txt`.
`uniq filename.txt`: Removes duplicate lines from `filename.txt` (often used with `sort` to remove
adjacent duplicates).
`wc w filename.txt`: Counts words in `filename.txt`.

SECTION2- ADMIN LEVEL COMMANDS


User Management Commands
1. Create a New User
`sudo useradd <username>`: Creates a new user account with the specified username.
`sudo adduser <username>`: Interactive command for creating a new user (prompts for
password and user details).

2. Modify User Attributes


`sudo usermod l <new_username> <old_username>`: Changes a user's login name.
`sudo usermod d <new_home_directory> <username>`: Changes a user's home directory.
`sudo usermod s <shell> <username>`: Changes a user’s default shell.
`sudo passwd <username>`: Sets or changes the password for a user.
`sudo chage l <username>`: Lists password expiration information for a user.
`sudo usermod aG <group> <username>`: Adds a user to an additional group without removing
them from existing groups.

3. Delete a User
`sudo userdel <username>`: Deletes a user from the system.
`sudo userdel r <username>`: Deletes a user along with their home directory and mail spool.
Group Management Commands
4. Create a New Group
`sudo groupadd <groupname>`: Creates a new group with the specified name.

5. Assign Users to Groups


`sudo usermod aG <groupname> <username>`: Adds a user to a specified group.
`sudo gpasswd a <username> <groupname>`: Another way to add a user to a group.
`sudo gpasswd d <username> <groupname>`: Removes a user from a group.

6. Change Group Ownership


`sudo chgrp <groupname> <filename>`: Changes the group ownership of a file or directory.
`sudo chown :<groupname> <filename>`: Another way to change group ownership of a file.

View User and Group Information


7. List Users and Groups
`cat /etc/passwd`: Displays all user accounts on the system.
`cat /etc/group`: Displays all groups on the system.
`groups <username>`: Lists groups a specified user belongs to.
`id <username>`: Displays user ID (UID), group ID (GID), and group memberships for a user.

Manage User and Group Access


8. Lock and Unlock User Accounts
`sudo usermod L <username>`: Locks a user account.
`sudo usermod U <username>`: Unlocks a user account.

9. Change User Permissions


`sudo chmod u+w <filename>`: Adds write permission for the file owner.
`sudo chown <username>:<groupname> <filename>`: Changes ownership of a file or directory to
a specified user and group.

10. View System Information


`uname a`: Displays all system information, including kernel version, hostname, processor type,
and operating system.
`hostnamectl`: Provides details about the hostname and other system information.
`lsb_release a`: Shows the distributionspecific information (e.g., Ubuntu version).
`cat /etc/osrelease`: Displays information about the operating system.
`lscpu`: Displays CPU architecture and details.
`lshw short`: Lists hardware configurations for various devices.
`lsblk`: Shows information about block devices, including disks and partitions.
`df h`: Displays disk space usage in a humanreadable format.
`free h`: Shows memory usage, including free and used memory, in a humanreadable format.
`uptime`: Provides the system uptime and load averages.

2. Monitor System Resources


`top`: Realtime view of CPU and memory usage, with details for running processes.
`htop`: Enhanced version of `top` (requires installation on some systems) for easier navigation.
`vmstat`: Reports information about system processes, memory, paging, block I/O, and CPU
activity.
`iostat`: Displays CPU and disk I/O statistics.
`mpstat`: Provides CPU usage statistics for each processor.
`dstat`: Combines the information of multiple monitoring tools (requires installation).
`sar`: Collects and displays system resource usage statistics (requires `sysstat` package).
`ps aux`: Lists detailed information about currently running processes.
`du sh /path/to/directory`: Shows disk usage for a specific directory.
`iotop`: Monitors I/O usage by processes (requires root privileges).
`nmon`: An interactive system monitor that shows CPU, memory, disk, network, NFS, and top
processes (requires installation).
3. Configure System Date and Time
`date`: Displays the current date and time.
`timedatectl`: Shows and configures the date and time settings. Common subcommands:
`timedatectl status`: Shows the current system time and time zone.
`timedatectl settime YYYYMMDD HH:MM:SS`: Sets the system date and time.
`timedatectl settimezone <time_zone>`: Changes the time zone (e.g., `timedatectl settimezone
Asia/Kolkata`).
`timedatectl setntp true`: Enables Network Time Protocol (NTP) synchronization.
`timedatectl setntp false`: Disables NTP synchronization.

SECTION 3: Network-Level Commands


1. Network Configuration Commands
View Network Interface Information
`ip a` or `ifconfig` (requires installation in some distros): Display all active network interfaces and
their IP configurations.

Configure IP Address and Subnet Mask


`sudo ip addr add <IP_Address>/<Subnet_Mask> dev <interface_name>`: Set an IP address and
subnet mask for a specific interface.
Example: `sudo ip addr add 192.168.1.10/24 dev eth0`
`sudo ifconfig <interface_name> <IP_Address> netmask <Subnet_Mask>`: Another way to set IP and
subnet mask.
Example: `sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0`

Set up Static and Dynamic (DHCP) IP Addressing


Static IP:
Manually edit the network configuration file in `/etc/network/interfaces` (Debian/Ubuntu) or
`/etc/sysconfig/networkscripts/ifcfg<interface_name>` (RHEL/CentOS).
Example content for a static configuration:
```
`
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
```

Dynamic IP (DHCP):
`sudo dhclient <interface_name>`: Request an IP from a DHCP server.
Add `iface eth0 inet dhcp` to `/etc/network/interfaces` for Debianbased systems for a persistent
DHCP configuration.

2. Network Diagnostics Commands


Ping a Remote Host
`ping <hostname/IP_address>`: Check connectivity to a remote host.
Example: `ping google.com`

Check Network Connectivity


`ip route`: Display the current routing table.
`netstat rn` or `route n`: Show network routes and verify default gateway configuration.

Trace the Route to a Remote Host


`traceroute <hostname/IP_address>`: Display the route packets take to reach a remote host.
Example: `traceroute google.com`
Note: `traceroute` might need to be installed using `sudo apt install traceroute` (Debian) or `sudo
yum install traceroute` (RHEL/CentOS).
3. Firewall Configuration Commands
View Current Firewall Rules
`sudo iptables L` or `sudo ufw status` (if using UFW): View all current firewall rules.

Add a New Firewall Rule to Allow/Deny Traffic


`sudo iptables A INPUT p <protocol> dport <port> j ACCEPT`: Allow traffic on a specific port and
protocol.
Example to allow HTTP traffic: `sudo iptables A INPUT p tcp dport 80 j ACCEPT`
`sudo ufw allow <port>`: Allow traffic through a specific port with UFW (e.g., `sudo ufw allow
80/tcp` for HTTP).

Remove an Existing Firewall Rule


`sudo iptables D INPUT p <protocol> dport <port> j ACCEPT`: Remove an existing rule.
Example to remove an HTTP allow rule: `sudo iptables D INPUT p tcp dport 80 j ACCEPT`
`sudo ufw delete allow <port>`: Remove a UFW rule.

You might also like