Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Linux Adm Lab Commands

linux administration commands

Uploaded by

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

Linux Adm Lab Commands

linux administration commands

Uploaded by

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

IILM University, Greater

Noida (U.P.)
Session 2024-25

Linux Administration Lab


Basic Commands in Linux
1. pwd command: Use the pwd command to find out the
path of the current working directory (folder) you’re in. An
example of an absolute path is /home/username.

2. cd command: To navigate through the Linux files and


directories, use the cd command. It requires either the full path
or the name of the directory, depending on the current working
directory that you’re in.
For example: cd /home/username/Movies.
There are some shortcuts to help you navigate quickly:
● cd .. (with two dots) to move one directory up
● cd to go straight to the home folder
● cd- (with a hyphen) to move to your previous directory

3. ls command: The ls command is used to view the contents


of a directory. By default, this command will display the
contents of your current working directory.
For example, enter ls /home/username/Documents to view the
content of Documents.
There are variations you can use with the ls command:
● ls -R will list all the files in the sub-directories as well
● ls -a will show the hidden files
● ls -al will list the files and directories with detailed
information like the permissions, size, owner, etc.
4. cat command: cat (short for concatenate) is one of the
most frequently used commands in Linux. It is used to list the
contents of a file on the standard output (sdout). To run this
command, type cat followed by the file’s name and its
extension. For instance: cat file.txt.
Here are other ways to use the cat command:
● cat > filename creates a new file
● cat filename1 filename2>filename3 joins two files (1 and
2) and stores the output of them in a new file (3)
● to convert a file to upper or lower case use, cat filename |
tr a-z A-Z >output.txt
● cat –n filename to show the line no
● cat –e filename show the “$” for space
● use ctrl+d to come out
● cat file1 > file2 existing contents of file1 will be
overwritten by contents of file2 file
● cat file1 >> file2 contents of file1 file will be appended at
the end of file2 file

5. cp command: Use the cp command to copy files from the


current directory to a different directory. For instance, the
command cp scenery.jpg /home/username/Pictures would
create a copy of scenery.jpg (from your current directory) into
the Pictures directory.
6. mv command: The primary use of the mv command is to
move files, although it can also be used to rename files. The
arguments in mv are similar to the cp command. You need to
type mv, the file’s name, and the destination’s directory. For
example: mv file.txt/home/username/Documents.
To rename files, the Linux command is:
mv oldname.ext newname.ext

7. mkdir command: Use mkdir command to make a new


directory — if you type mkdir Music it will create a directory
called Music.

There are extra mkdir commands as well:


● To generate a new directory inside another directory, use
this Linux basic command mkdir Music/Newfile
● use the p (parents) option to create a directory in between
two existing directories. For example, mkdir -p
Music/2020/Newfile will create the new “2020” file.

8. rmdir command: If you need to delete a directory, use


the rmdir command. However, rmdir only allows you to delete
empty directories.

9. rm command: The rm command is used to delete


directories and the contents within them. If you only want to
delete the directory — as an alternative to rmdir — use rm -r.

10. touch command: The touch command allows you to


create a blank new file through the Linux command line.

As an example, enter
touch /home/username/Documents/Web.html to create an
HTML file entitled Web under the Documents directory.
11. locate command: You can use this command
to locate a file, just like the search command in Windows.
What’s more, using the -i argument along with this command
will make it case-insensitive, so you can search for a file even if
you don’t remember its exact name.

To search for a file that contains two or more words, use an


asterisk (*). For example, locate -i school*note command will
search for any file that contains the word “school” and “note”,
whether it is uppercase or lowercase.

12. find command: Similar to the locate command,


using find also searches for files and directories. The
difference is, you use the find command to locate files within a
given directory.

As an example, find /home/ -name notes.txt command will


search for a file called notes.txt within the home directory and
its subdirectories.

Other variations when using the find are:


● To find files in the current directory use, find . -name
notes.txt
● To look for directories use, / -type d -name notes. Txt

13. grep command: It lets you search through all the text in
a given file.

To illustrate, grep blue notepad.txt will search for the word


blue in the notepad file. Lines that contain the searched word
will be displayed fully.
14. sudo command: Short for “SuperUser Do”, this
command enables you to perform tasks that require
administrative or root permissions.

15. df command: Use df command to get a report on the


system’s disk space usage, shown in percentage and KBs. If
you want to see the report in megabytes, type df -m.

16. du command: If you want to check how much space a


file or a directory takes, the du (Disk Usage) command is the
answer. However, the disk usage summary will show disk
block numbers instead of the usual size format. If you want to
see it in bytes, kilobytes, and megabytes, add the -h argument
to the command line.

17. head command: The head command is used to view


the first lines of any text file. By default, it will show the first ten
lines, but you can change this number to your liking. For
example, if you only want to show the first five lines, type head
-n 5 filename.ext.

18. tail command: This one has a similar function to the


head command, but instead of showing the first lines,
the tail command will display the last ten lines of a text file. For
example, tail -n filename.ext.

19. diff command: Short for difference, the diff command


compares the contents of two files line by line. After analyzing
the files, it will output the lines that do not match.

The simplest form of this command is diff file1.ext file2.ext


20. tar command: The tar command is the most used
command to archive multiple files into a tarball — a common
Linux file format that is similar to zip format, with compression
being optional.

21. chmod command: chmod is another Linux command,


used to change the read, write, and execute permissions of
files and directories.

22. chown command: In Linux, all files are owned by a


specific user. The chown command enables you to change or
transfer the ownership of a file to the specified username. For
instance, chown linuxuser2 file.ext will make linuxuser2 as
the owner of the file.ext.

23. jobs command: jobs command will display all current


jobs along with their statuses. A job is basically a process that
is started by the shell.

24. kill command: If you have an unresponsive program,


you can terminate it manually by using the kill command. It will
send a certain signal to the misbehaving app and instructs the
app to terminate itself.

There is a total of sixty-four signals that you can use, but


people usually only use two signals:

● SIGTERM (15) — requests a program to stop running and


gives it some time to save all of its progress. If you don’t
specify the signal when entering the kill command, this
signal will be used.
● SIGKILL (9) — forces programs to stop immediately.
Unsaved progress will be lost.
Besides knowing the signals, you also need to know the
process identification number (PID) of the program you want
to kill. If you don’t know the PID, simply run the command ps
ux.

After knowing what signal you want to use and the PID of the
program, enter the following syntax:
kill [signal option] PID.

25. ping command: Use the ping command to check your


connectivity status to a server. For example, by simply
entering ping google.com, the command will check whether
you’re able to connect to Google and also measure the
response time.

26. wget command: You can download files from the


internet with the help of the wget command. To do so, simply
type wget followed by the download link.

27. uname command: The uname command, short for


Unix Name, will print detailed information about your Linux
system like the machine name, operating system, kernel, and
so on.

28. top command: As a terminal equivalent to Task


Manager in Windows, the top command will display a list of
running processes and how much CPU each process uses.

29. history command: Running history command is


particularly useful if you want to review the commands you’ve
entered before.
30. man command: You can easily learn how to use any
command right from Linux’s shell by using the man command.
For instance, entering man tail will show the manual
instruction of the tail command.

31. echo command: This command is used to move some


data into a file. For example, if you want to add the text, “Hello,
my name is John” into a file called name.txt, you would
type echo Hello, my name is John >> name.txt

32. zip, unzip command: Use the zip command to


compress your files into a zip archive, and use
the unzip command to extract the zipped files from a zip
archive.

33. hostname command: If you want to know the name


of your host/network simply type hostname. Adding a -I to the
end will display the IP address of your network.

34. useradd, userdel command: useradd is used to


create a new user, while passwd is adding a password to that
user’s account. To add a new person named John
type, useradd John and then to add his password
type, passwd 123456789.

To delete the users account type, userdel UserName


Group Administration
Commands
– Use the groupadd command to add a new group:
# groupadd [options] group_name
– Use the groupmod command to modify an existing group:
# groupmod [options] group_name
– Use groupdel to delete the group. You can remove a group
even if there are users in the group. But you cannot remove the
primary group of an existing user. You must remove the user
before removing the group.
# groupdel group_name
– Use the gpasswd command to administer the groups:
# gpasswd [options] group_name
For example: to add user test in group student –
# gpasswd -a test student
– The newgroup command executes a new shell and changes a
user’s real group information.
$ newgrp group_name

Command to Reboot and Shutdown


a system normally
- $ reboot will reboot the system.
- $ shutdown will shutdown the system.
Add new users in Linux using
useradd command
Step1: Switch to root user by running the sudo su command.
In the following example I am switching from my account to root
account.
vikaskhan@vikaskhan-ubuntu:~$ sudo su
[sudo] password for vikaskhan:
root@vikaskhan-ubuntu:/home/vikaskhan#

Step2: To add a new user we will use the useradd command.


In the following example we are creating a new user account by
the login name IILM.

# useradd IILM

Step3: passwd - Set password


When a new user is successfully created it is in a locked state.
To unlock a new user account, we have to set the password for
the user account using the passwd command.

In the following example we are going to set the password for the
new user IILM.

# passwd IILM
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
⮚ /etc/passwd - User info file.
When a new user is added an entry is made in the /etc/passwd. In
the following example we are listing all the entries in the passwd
file.
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
...
...
...
IILM:x:1001:1001::/home/IILM:/bin/sh

We can see that we have the new entry for user name IILM.

IILM:x:1001:1001::/home/IILM:/bin/sh

The line is colon : separated and consists of 7 parts which are the
following.

● Username
● Password
● User ID or UID
● Group ID or GID
● User Info
● Home Directory
● Shell
⮚ useradd -u - Assigning User ID
If we want to assign specific User ID to a user then we can use
the -u option. In the following example we are creating a new user
IILM with userid 500.

# useradd -u 500 IILM

⮚ useradd -d - Assigning home directory


By default, when we create a new user a home directory is also
created inside the /home directory by the username.

So, if we create a new user let’s say, alice then we will get a new
home directory /home/alice. If we want to assign specific home
directory to a user then we can use the -d option.
In the following example we are creating a new user IILM with
home directory /workspace/IILM.

# useradd -d /workspace/IILM IILM

⮚ useradd -M - Don't create home directory


If we don't want to assign home directory to a user then we can
use the -M option. In the following example we are creating a new
user IILM without home directory.

# useradd -M IILM
⮚ useradd -g - Assigning Group ID
If we want to assign specific Group ID to a user then we can use
the -g option. In the following example we are creating a new user
bob with groupid 500.

# useradd -g 500 bob

⮚ useradd -G - Assigning multiple groups


If we want to add a user to multiple groups then we use the -G
option.

In the following example we are creating a new user eve and


assigning the account to developer and tester groups.

# useradd -G developer,tester eve

⮚ useradd -e - Creating temporary account


If we want to create an account that expires after a given date
then we use the -e option followed by the date in YYYY-MM-DD
format.

Where, YYYY represents year. MM represents month and DD


represents day.

In the following example we are creating a temporary user


tempuser that will expire after 2018- 12-31 i.e. 31st December
2018.

# useradd -e 2018-12-31 tempuser


Delete user account in Linux
Step1: Switch to root user by running the sudo su command.
In the following example I am switching from my account to root
account.
IILM@IILM-ubuntu:~$ sudo su
[sudo] password for IILM:
root@IILM-ubuntu:/home/IILM#

Step2: passwd --lock - To lock user account


We start the deletion process by first locking the user account so
that the user can't login again. In the following example we are
locking IILM account.
# passwd --lock IILM
passwd: password expiry information changed.

Step3: killall - Kill all processes of the user


Now we will kill all the processes by the user using the killall
command. In the following example we are listing all the
processes by user IILM.
# pgrep -u IILM
4076
4081
4101
4177
...
...
To kill all the processes by user IILM we will run the following
command.
# killall -u IILM

Step4: Backup - Creating backup copy of the user data

We have successfully stopped the user from logging in and we


have also killed all the processes connected with the user.
Now, we will backup the data files of the user before deleting the
account.

This is an optional step. If you think you are not going to need
the data of the user then you can jump to the next step.

In the following example we are creating a backup copy of


IILM user account and saving it inside /user-account-backup
directory.
# tar -zcvf /user-account-backup/IILM-user-
account-home-directory backup.tar.gz /home/IILM

Step5: userdel - Delete user

To delete the user, we will use the userdel command.

In the following example we are deleting user account of IILM.


# userdel --remove IILM

On Debian and its derivative system try using deluser command.


# deluser --remove-home IILM
Piping in Unix or Linux
The Pipe is a command in Linux that lets you use two or more
commands such that output of one command serves as input to
the next.

In short, the output of each process directly as input to the next


one like a pipeline.

It can also be visualized as a temporary connection between two


or more commands/ programs/ processes. The command line
programs that do the further processing are referred to as filters.

The symbol '|' denotes a pipe.

This direct connection between commands/ programs/ processes


allows them to operate simultaneously and permits data to be
transferred between them continuously rather than having to pass
it through temporary text files or through the display screen.

Pipes are unidirectional i.e. data flows from left to right through
the pipeline.

Syntax:
command_1 | command_2 | command_3 | .... | command_N

For example, listing all files and directories and give it as input to
more command.
$ ls -l | more
archive, compress, unpack and
uncompressed files using tar,
star, gzip and bzip2

⮚ tar
Syntax to create an archive file:
tar [options] [archive-file] [file or directory to be archived]

Q. What is an Archive file?


Ans. An Archive file is a file that is composed of one or more files
along with metadata. Archive files are used to collect multiple data
files together into a single file for easier portability and storage, or
simply to compress files to use less storage space.

Create an archive.
# cd /tmp
# tar -cvf archive1.tar test-dir
Check the contents.
# tar -tvf /tmp/archive1.tar
Extract it.
# cd /tmp/extract-dir
# tar -xvf /tmp/archive1.tar
⮚ gzip
The gzip command compresses the specified files, giving them a
".gz" extension. In this case we will use it to compress a ".tar" file.
# cd /tmp
# tar -cvf archive3.tar test-dir
# gzip archive3.tar
The "-z" option of the tar command allows you to do this directly.
# cd /tmp
# tar -cvzf archive3.tar.gz test-dir
The files are uncompressed using the gunzip command.
# gunzip archive3.tar.gz
The "-z" option of the tar command allows you to directly ungzip
and extract a ".tar.gz" file.
# cd /tmp/extract-dir
# tar -xvzf /tmp/archive3.tar.gz

⮚ bzip2
The bzip2 command is similar to the gzip command. It
compresses the specified files, giving them a ".bz2" extension. In
this case we will use it to compress a ".tar" file.
# cd /tmp
# tar -cvf archive4.tar test-dir
# bzip2 archive4.tar
The "-j" option of the tar command allows you to do this directly.
# cd /tmp
# tar -cvjf archive4.tar.bz2 test-dir
The files are uncompressed using the bunzip2 command.
# bunzip2 archive4.tar.bz2
The "-j" option of the tar command allows you to directly bunzip2
and extract a ".tar.bz2" file.
# cd /tmp/extract-dir
# tar -xvjf /tmp/archive4.tar.bz2

⮚ zip
Create an archive.
# cd /tmp
# zip -r archive5.zip test-dir
Check the contents.
# unzip -l archive5.zip
Extract it.
# cd /tmp/extract-dir
# unzip /tmp/archive5.zip
⮚ cpio
Create an archive.
# cd /tmp
# find test-dir | cpio -ov > archive6.cpio
Check the contents.
# cpio -t < /tmp/archive6.cpio
Extract it.
# cd /tmp/extract-dir
# cpio -idmv < /tmp/archive6.cpio

⮚ star
The star command may not be installed by default, but you can
install it with the following command.
# yum install star

Create an archive.
# cd /tmp
# star -cv f=archive2.star test-dir

Check the contents.


# star -tv f=/tmp/archive2.star

Extract it.
# cd /tmp/extract-dir
# star -xv f=/tmp/archive2.star
Soft and Hard links in Unix/Linux
A link in UNIX is a pointer to a file. Like pointers in any
programming languages, links in UNIX are pointers pointing to a
file or a directory. Creating links is a kind of shortcuts to access a
file. Links allow more than one file name to refer to the same file,
elsewhere.

There are two types of links:


1. Hard Link
2. Soft Link or Symbolic links

1. Hard Links: Each hard linked file is assigned the same


Inode value as the original, therefore they reference the same
physical file location.
Hard links more flexible and remain linked even if the original or
linked files are moved throughout the file system, although hard
links are unable to cross different file systems.
ls -l command shows all the links with the link column shows
number of links.
Links have actual file contents
⮚ Removing any link, just reduces the link count, but doesn’t
affect other links.
⮚ Even if we change the filename of the original file then also the
hard links properly work.
⮚ We cannot create a hard link for a directory to avoid recursive
loops.
⮚ If original file is removed then the link will still show the content
of the file.
⮚ The size of any of the hard link file is same as the original file
and if we change the content in any of the hard links then size
of all hard link files are updated.
⮚ The disadvantage of hard links is that it cannot be created for
files on different file systems and it cannot be created for
special files or directories.

Command to create a hard link is:


$ ln [original filename] [link name]

2. Soft Links: A soft link is similar to the file shortcut feature


which is used in Windows Operating systems.
Each soft linked file contains a separate Inode value that points to
the original file.
As similar to hard links, any changes to the data in either file is
reflected in the other.
Soft links can be linked across different file systems, although if
the original file is deleted or moved, the soft linked file will not
work correctly (called hanging link).
ls -l command shows all links with first column value l? and the
link points to original file.
● Soft Link contains the path for original file and not the
contents.
● Removing soft link doesn’t affect anything but removing
original file, the link becomes “dangling” link which points to
nonexistent file.
● A soft link can link to a directory.
● Size of a soft link is equal to the name of the file for which
the soft link is created. E.g., If name of file is file1 then size
of its soft link will be 5 bytes which is equal to size of name
of original file.
● If we change the name of the original file then all the soft
links for that file become dangling i.e. they are worthless
now.
● Link across file systems: If you want to link files across the
file systems, you can only use symlinks/soft links.
Command to create a Soft link is:
$ ln -s [original filename] [link name]

Create and manage Access


Control Lists (ACLs)
Q. What is ACL?
Ans. Access control list (ACL) provides an additional, more
flexible permission mechanism for file systems. It is designed to
assist with UNIX file permissions. ACL allows you to give
permissions for any user or group to any disc resource.

setfacl and getfacl are used for setting up ACL and showing ACL
respectively.
For example:
getfacl test/declarations.h

Output:
# file: test/declarations.h
# owner: vikas
# group: vikas
user::rw-
group::rw-
other::r—
List of commands for setting up ACL:
1) To add permission for user
setfacl -m "u:user:permissions" /path/to/file
2) To add permissions for a group
setfacl -m "g:group:permissions" /path/to/file
3) To allow all files or directories to inherit ACL entries from the
directory it is within
setfacl -dm "entry" /path/to/dir
4) To remove a specific entry
setfacl -x "entry" /path/to/file
5) To remove all entries
setfacl -b path/to/file

You might also like