0% found this document useful (0 votes)
5 views

unit2 notes

The document provides an overview of using shell prompts, terminal windows, and virtual consoles in Linux, along with commands for user and group management. It details administrative commands, configuration files, log files, and shell variables, including how to create, modify, and delete users and groups. Additionally, it covers basic file management commands and operations for navigating the file system.

Uploaded by

akshithreddy849
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

unit2 notes

The document provides an overview of using shell prompts, terminal windows, and virtual consoles in Linux, along with commands for user and group management. It details administrative commands, configuration files, log files, and shell variables, including how to create, modify, and delete users and groups. Additionally, it covers basic file management commands and operations for navigating the file system.

Uploaded by

akshithreddy849
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIT 2

About Shells and Terminal Windows


Using the Shell Prompt

 Shell Prompt:
o $ → Regular user prompt.
o # → Root user prompt.
 Usage: If the GUI is unavailable, the shell prompt is your primary interface for
interacting with the Linux system.

Using a Terminal Window

 Ways to open a terminal from the GUI:


o Right-click the desktop and select "Open in Terminal" or similar.
o Use a panel menu to launch the terminal application.

Using Virtual Consoles

 Virtual consoles provide multiple text-based terminal sessions (accessible via


Ctrl+Alt+F1 to Ctrl+Alt+F6).
 To return to the GUI:

Ctrl+Alt+F1

 Example: Press Ctrl+Alt+F3 to open a plain-text login prompt, then log in with your
credentials.

Choosing Your Shell

 Default shell: bash in most Linux systems.


 To check your default shell:

grep username /etc/passwd

Replace username with your actual username. The output shows the shell
configuration.

Using the root user account


Exploring administrative commands, configuration files,
and Log files
Category Command/File Description
Administrative sudo
Execute commands with superuser (root)
Commands privileges.
Category Command/File Description
su Switch user, often used to switch to root.
chmod Change file permissions.
chown Change file ownership.
useradd Add a new user to the system.
usermod Modify an existing user's attributes.
userdel Delete a user account.
groupadd Add a new group.
groupdel Delete a group.
passwd Change user password.
df Report disk space usage.
du Estimate file space usage.
Contains user account information (e.g.,
Configuration Files /etc/passwd
usernames, UIDs).
/etc/group Defines groups of users on the system.
/etc/fstab
Contains static information about disk
drives and partitions.
/etc/network/interfaces
Configuration file for network interfaces
(Debian-based systems).
/etc/hostname The system's hostname.
/etc/resolv.conf
Configuration for DNS resolution
(nameservers).
/etc/ssh/sshd_config Configuration file for the SSH daemon.
/etc/sudoers
Configuration file defining user
privileges for sudo.
/etc/sysctl.conf Kernel parameters to be set at boot time.
General system log file (records system
Log Files /var/log/syslog
events).
/var/log/auth.log
Log file for authentication attempts,
including sudo and login attempts.
/var/log/dmesg
Kernel ring buffer logs, often used for
boot diagnostics.
/var/log/messages
System messages, used for general logs
(often in RedHat-based distros).
Security-related log file (typically stores
/var/log/secure auth and sudo logs on RedHat-based
systems).
/var/log/boot.log Boot-related logs.
/var/log/kern.log
Kernel logs, stores messages from the
kernel itself.
/var/log/cron
Cron job logs, stores output from
scheduled cron jobs.
Shell Variables in Linux
Shell variables are placeholders used to store and manage information within a shell session.
These variables can hold values like strings, numbers, or command output and are used in
shell scripts or command-line operations.

Types of Shell Variables

1. Local Variables: Defined in the current shell session and not inherited by child
processes.
2. Environment Variables: Exported to child processes and used system-wide.
3. Special Variables: Predefined by the shell, such as $0, $1, $?.

Common Operations with Shell Variables

1. Assigning a Variable

To assign a value to a variable:

variable_name=value

 Example:

myvar="Hello, World!"
echo $myvar

Output:

Hello, World!

2. Accessing a Variable

Use the $ symbol before the variable name:

echo $variable_name

3. Environment Variables

To make a variable accessible to child processes, use export:

export variable_name=value

 Example:

export PATH=$PATH:/new/directory
echo $PATH

4. Viewing All Shell Variables

 Local Variables:

set

 Environment Variables:

env

5. Using Command Output as a Variable Value

Command substitution allows storing the output of a command in a variable:

variable_name=$(command)

 Example:

today=$(date)
echo $today

Output:

sql
CopyEdit
[Current date and time]

6. Deleting a Variable

Use unset to remove a variable:

unset variable_name

Examples for Practice

Example 1: Create and Use a Local Variable

greeting="Welcome to Linux Shell"


echo $greeting

Output:

Welcome to Linux Shell

Example 2: Using Environment Variables


export MY_ENV_VAR="Environment Variable Example"
echo $MY_ENV_VAR

Output:

Environment Variable Example

Example 3: Arithmetic with Variables

x=10
y=5
sum=$((x + y))
echo "The sum is $sum"

Output:

The sum is 15

Example 4: Concatenate Strings

first="Hello"
second="World"
combined="$first $second"
echo $combined

Output:

Hello World

Example 5: Command Substitution

current_dir=$(pwd)
echo "You are in $current_dir"

Output:

You are in /path/to/current/directory

Special Variables

 $USER: Current logged-in username.


 $HOME: Home directory of the user.
 $PWD: Current working directory.
 $?: Exit status of the last executed command.
 $#: Number of arguments passed to a script.

Getting Information About Commands and Help


In Linux, documentation for commands is typically available through the man (manual) pages
or the --help option.

Example:

 Using man to view command manuals:

man ls
This shows the manual for the ls command, which lists directory contents.

 Using --help for a summary:

ls --help
This gives a brief summary of how to use the ls command and its available options.

Managing User Accounts in Ubuntu


User Management in Linux

Task Command Example

Add a user sudo adduser username sudo adduser alice

Delete a user sudo userdel -r username sudo userdel -r alice

Create a group sudo groupadd groupname sudo groupadd developers

sudo usermod -aG groupname sudo usermod -aG developers


Add a user to a group
username alice

Delete a group sudo groupdel groupname sudo groupdel developers

View groups of a
groups username groups alice
user
1. What is User Management?

 User management involves creating, modifying, and deleting users on a Linux system.
 It ensures system security and proper access control for different users.

2. User Management Using CUI

a) Adding a New User

Command:

sudo adduser username

Example:

sudo adduser alice

This creates a new user alice and sets up their home directory (/home/alice).

b) Modifying User Details

Command:

sudo usermod [options] username

Example:

 Add a user to a supplementary group:

sudo usermod -aG groupname alice

c) Deleting a User

Command:

sudo userdel username

Example:

 Delete the user alice and their home directory:

sudo userdel -r alice

d) Switching Between Users


Command:

su - username

Example:

su - alice

This switches to user alice.

e) Checking Current User

Command:

whoami

3. User Management Using GUI

1. Open the Users or Settings → Users tool.


2. To add a new user:
o Click Add User.
o Provide a username, password, and role (e.g., Administrator or Standard
User).
3. To delete a user:
o Select the user from the list.
o Click Delete and confirm.

Group Management in Linux


1. What is Group Management?

 Group management allows managing permissions and access for multiple users at
once.

2. Group Management Using CUI

a) Creating a New Group

Command:

sudo groupadd groupname

Example:
sudo groupadd developers

b) Adding a User to a Group

Command:

sudo usermod -aG groupname username

Example:

sudo usermod -aG developers alice

c) Viewing Group Membership

Command:

groups username

Example:

groups alice

d) Removing a User from a Group

Command:

sudo gpasswd -d username groupname

Example:

sudo gpasswd -d alice developers

e) Deleting a Group

Command:

sudo groupdel groupname

Example:

sudo groupdel developers


3. Group Management Using GUI

1. Open Settings → Users and Groups.


2. Navigate to the Groups tab.
3. Create or edit groups:
o To add a new group: Click Add Group.
o To add users to a group: Select the group and add users from the list.

Practical Examples

Example 1: Adding a User and Group in CUI

# Create a new user

sudo adduser bob

# Create a new group

sudo groupadd testers

# Add the user to the group

sudo usermod -aG testers bob

# Verify group membership

groups bob

Example 2: Deleting a User and Group in CUI

# Delete a user and their home directory

sudo userdel -r bob

# Delete a group

sudo groupdel testers

MOVING AROUND THE FILE SYSTEM UBUNTU


Basic commands for file Management

1.ls

 Purpose: Lists the files and directories in the current directory.


 Example:

ls

Output:

file1 file2 dir1 dir2

2. ls -a

 Purpose: Lists all files, including hidden files (those starting with a dot .).
 Example:

ls -a

Output:

. .. .hidden_file file1 file2 dir1 dir2

3. ls -l

 Purpose: Displays detailed information about files and directories (permissions,


owner, size, modification date).
 Example:

ls -l

Output:

-rw-r--r-- 1 user user 1024 Jan 24 file1


drwxr-xr-x 2 user user 4096 Jan 24 dir1

Using ls –la for long listing the hidden files also.


4. ls -l collection/

 Purpose: Displays detailed information about the contents of the collection/ directory.
 Example:

ls -l collection/

Output:

-rw-r--r-- 1 user user 512 Jan 24 doc1.txt


drwxr-xr-x 2 user user 4096 Jan 24 images

5. ls -ld collection/

 Purpose: Displays detailed information about the collection/ directory itself, not its
contents.
 Example:

ls -ld collection/

Output:

drwxr-xr-x 5 user user 4096 Jan 24 collection/

6.ls –aF

 To classify the files and directories, you can use ls –aF command


 Note: which one is ended with “/” is considered as directories and not ended with
“/” considered as file

7. ls -i

 Purpose: Displays the inode number of each file and directory.


 Example:

ls -i

Output:

12345 file1 12346 file2 12347 dir1

8. ls -lt /home/student/
 Purpose: Lists the contents of /home/student/ sorted by modification time (newest
first).
 Example:

ls -lt /home/student/

Output:

-rw-r--r-- 1 user user 2048 Jan 24 report.pdf


drwxr-xr-x 3 user user 4096 Jan 20 projects

9. ls -ltr /home/student/

 Purpose: Lists the contents of /home/student/ sorted by modification time (oldest first).
 Example:

ls -ltr /home/student/

Output:

drwxr-xr-x 3 user user 4096 Jan 20 projects


-rw-r--r-- 1 user user 2048 Jan 24 report.pdf

10. ls -sh /home/student/

 Purpose: Displays the sizes of files in a human-readable format.


 Example:

ls -sh /home/student/

Output:

4.0K report.pdf 8.0K projects

11. ls -s /boot

 Purpose: Displays the size of each file in blocks (default block size).
 Example:

ls -s /boot

Output:

32 config-5.11.0 64 initrd.img

12. ls -R /home/
 Purpose: Recursively lists all files and subdirectories in /home.
 Example:

ls -R /home/

Output:

/home/user:
documents downloads

/home/user/documents:
file1.txt file2.pdf

13. tree /home

 Purpose: Displays the directory structure in a tree-like format. (You may need to
install tree first.)
 Example:

tree /home

Output:

/home
├── documents
├── downloads
└── pictures

14. history

 Purpose: Displays the history of all previously executed commands.


 Example:

history

Output:

1 ls
2 cd /home
3 pwd

2. Change Directory (cd)

 Change to /etc directory:


Example:
cd /etc
pwd

Output:

/etc

 Go back to the parent directory using cd ..:


Example:

cd ..
pwd

Output:

/
 Using cd – to change the directory to previous working directory

 Return to the home directory with cd :


Example:

cd
pwd

Output:

/root

 Using relative path to navigate to a subdirectory:


Example:

cd collection
pwd

Output:

/root/collection

3. Paths

 Absolute Path:
Example:

cd /root/collection
pwd

Output:

/root/collection

 Relative Path:
Example:

cd collection
pwd

Output:

/root/collection

4. File Creation (touch)

 Create a single file:


Example:

touch app
ls

Output:

app

 Create multiple files at once:


Example:

touch data apple grapes orange


ls

Output:

app apple data grapes orange

 Using filename extensions:


Example:

touch app{1..5}.txt
ls

Output:

app1.txt app2.txt app3.txt app4.txt app5.txt

5. Creating a Directory (mkdir)


 Create a single directory:
Example:

mkdir project
ls

Output:

project

 Create multiple directories at once:


Example:

mkdir dir1 dir2 dir3


ls

Output:

dir1 dir2 dir3

 Create nested directories:


Example:

mkdir -p office/departments/{Sales,Marketing,Accounts}
ls office/departments

Output:

Accounts Marketing Sales

6. Copy, Move, and Delete

6.1 Copy Commands

 Copy a single file:


Example:

cp app1.txt dir1/
ls dir1

Output:

app1.txt

 Copy multiple files:


Example:

cp app2.txt app3.txt dir2/


ls dir2
Output:

app2.txt app3.txt

 Copy a directory (recursively):


Example:

cp -r dir1 dir3/
ls dir3

Output:

dir1

6.2 Move Commands

 Move a file to another directory:


Example:

mv app4.txt dir2/
ls dir2

Output:

app2.txt app3.txt app4.txt

 Rename a file:
Example:

mv app5.txt renamed_app5.txt
ls

Output:

renamed_app5.txt

6.3 Delete Commands

 Delete a file:
Example:

rm app1.txt
ls

Output:

app2.txt app3.txt
 Delete multiple files:
Example:

rm app2.txt app3.txt
ls

Output:

dir1 dir2

 Delete a directory and its contents:


Example:

rm -r dir1
ls

Output:

dir2

System related commands

Use the date command to display the current time and date.

Use the file command to determine its file type.

Use lsblk command to check the hard disk and partition informations
Use uptime to check the machine load average.

Use cal, cal -3, cal –y (year) to list all the information of calendar in monthly

Use bc command is for opening the basic calculator (use ctrl+c or ctrl+d to close)

Use free to list the memory information


Understanding File Permissions and Ownership:
The nine bits assigned to each file for permissions define the access that you
and others have to your file.
Permission bits for a regular file appear as -rwxrwxrwx. Those bits are used to
define who can read, write, or execute the file.
Of the nine-bit permissions, the first three bits apply to the owner’s permission,
the next

Permission Management
Key Notes
 chmod manages file permissions:
o Symbolic: u (user), g (group), o (others), a (all)
o Numeric: r=4, w=2, x=1
 chown changes file or directory ownership.
 chgrp changes group ownership.

1. Managing file permissions using Symbolic Method


1.1 Create a file perm and add execute (x) permission for the user section
touch perm # Create the file 'perm'
chmod u+x perm # Add execute permission for the user section
1.2 Add write (w) permission for the group section and execute (x) permission
for others
chmod g+w,o+x perm # Add write permission for group and execute
permission for others
1.3 Remove execute (x) permission for the user, write (w) for the group, and
execute (x) for others
chmod u-x,g-w,o-x perm # Remove execute for user, write for group, and
execute for others
1.4 Change the permission for the user section exactly (=)
chmod u=r perm # Set the user section to read-only
1.5 Add write (w) permission to all sections
chmod a+w perm # Add write permission for user, group, and others
1.6 Modify permissions for all sections exactly (=) to read, write, execute
chmod u=rwx,g=rwx,o=rwx perm # Set read, write, execute for user, group,
and others

2. Managing file permissions using Numeric Method


2.1 Create a directory dir1 and modify the permission to 754
mkdir dir1 # Create the directory 'dir1'
chmod 754 dir1 # Set permissions to 754 (rwx for user, r-x for group, r--
for others)
2.2 Modify the directory for full permission
chmod 777 dir1 # Set full permissions (rwx for user, group, and others)

3. Changing ownership of the file


3.1 Create a directory dir2 and change its ownership (as root user)
mkdir dir2 # Create the directory 'dir2'
sudo chown newuser dir2 # Change ownership to 'newuser'
3.2 Change the group ownership of the directory dir2
sudo chgrp newgroup dir2 # Change group ownership to 'newgroup'
3.3 Use chown to change both ownership and group ownership
sudo chown newuser:newgroup dir2 # Change both ownership and group
ownership

Access Control Lists (ACLs)


Access Control Lists (ACLs) allow users to define fine-grained permissions for
files and directories, enabling specific users or groups to have customized
access rights beyond the traditional user-group-other (ugo) model.

Key Points About ACLs


1. Enabling ACLs on Filesystems:
o ACLs are enabled by default on most filesystems during the
installation of Ubuntu and similar Linux distributions.
o For manually added filesystems, the acl option must be specified in
the /etc/fstab file during mounting.
2. Commands for Managing ACLs:
o setfacl: Used to set or modify ACLs.
o getfacl: Used to view ACLs of a file or directory.
3. Syntax of setfacl:
o Add or modify ACLs: setfacl -m [u|g]:[user|group]:[permissions]
filename
o Remove ACLs: setfacl -x [u|g]:[user|group] filename
4. Viewing ACLs:
o Use ls -l to see a + at the end of the permissions, indicating ACLs
are applied.
o Use getfacl to see the detailed ACL settings for a file or directory.

Example: ACL Management


1. File Permissions
File: /tmp/memo.txt
Command output:
[mary]$ ls -l /tmp/memo.txt
-rw-rw-r--+ 1 mary mary 0 Jan 21 09:27 /tmp/memo.txt
 The + at the end of the permissions indicates ACLs are applied.
2. Viewing ACLs with getfacl
Command output:
[mary]$ getfacl /tmp/memo.txt
# file: tmp/memo.txt
# owner: mary
# group: mary
user::rw- # File owner (mary) has read and write permissions.
user:bill:rw- # User 'bill' has read and write permissions.
group::rw- # Group 'mary' has read and write permissions.
group:sales:rw- # Group 'sales' has read and write permissions.
mask::rw- # Maximum effective permissions for users/groups.
other::r-- # Others have read-only permissions.

Explanation of Output
1. user::rw-: Default file owner (mary) has read and write permissions.
2. user:bill:rw-: Specific user bill has been granted read and write
permissions.
3. group::rw-: Default group (mary) has read and write permissions.
4. group:sales:rw-: Additional group sales has been granted read and write
permissions.
5. mask::rw-: The mask defines the maximum allowable permissions for
users and groups. If the mask restricts a permission, it will override the
user/group ACL settings.
6. other::r--: All other users not explicitly listed can only read the file.

Common ACL Operations


1. Grant Full Permissions to a User
setfacl -m u:username:rwx filename
Example: Grant rwx permissions to john for /tmp/memo.txt:
setfacl -m u:john:rwx /tmp/memo.txt
2. Remove Permissions for a User
setfacl -x u:username filename
Example: Remove permissions for bill:
setfacl -x u:bill /tmp/memo.txt
3. Grant Permissions to a Group
setfacl -m g:groupname:rw filename
Example: Grant rw permissions to devs group:
setfacl -m g:devs:rw /tmp/memo.txt
4. Remove ACLs Completely
To remove all ACLs from a file:
setfacl -b filename
Example:
setfacl -b /tmp/memo.txt

You might also like