Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Welcome TechGurukul

to
Learn Linux Series:

Administrating Linux : Admin Commands

dwivedishashwat@gmail.com
Curl Command

1. Curl: :
We can test this command to test any application endpoint connectivity. This is very useful for
determining if our application can reach any another services, such as a database, Webserver,
or checking if our service is healthy
Examples:

curl -I –s application:8800

output shows 200 it means application/web able to connect and it healthy and running

There are many command line parameters for various option you can get those details
using “curl –help”
Grep : search string from files

1. Grep:
Grep command is very useful in searching string pattern from files, we can use this command
to get errors/exceptions etc. from file.

Example:
grep error /var/log/message
cat /var/log/*.log|grep –Ei “Error|fatal”
Ps

1. Ps:
This command is used to fetch information related various process runnin, we can use this
command to check status, process id, user etc related to process. Suppose if we need to check
if a process is running or not we can use this command. It basically report a snapshot of the
current processes running.

Example:
ps –ef : see every process on the system
ps –ejH --or-- ps axjf : print process tree with parent and child process
ps –eLf –or-- ps axms : get info about threads
for more info please use “man ps” or “info ps” or “ps –help”
top

1. Top:
This command is just like ps command but more like windows task manager which display Linux processes
in tabular format as displayed in above image. It basically displays and updates sorted process information.
This can be used to determine which processes are running and how much memory and CPU they consume.
Examples:
 execute top command and press M and then T to sort processes by process ID
 execute top command and press M and then T to sort processes by memory and cpu
 execute top command and press M and then T to sort processes running by time
 top –u Shashwat : will display process by user Shashwat
 Press c in top to see the absolute path of the processes
 Press r in top to change scheduling priority of a process

2. free –h: it displays total, used, and free system memory

3. cat /proc/meminfo : every information about mermoyr intalled

4. cat /proc/cpuinfo: every information about cpu installed


top

 Press 1 in top to see all the cpu cores and uses


 top –n 1 –b > topoutput.txt : this will write output of top command to a file
Press Shift+P to see the output by cup utilization
Press i to see list or idle processes
 top –n 10 : this will open top and quit after 10 repeatation(it refreses at a interval)
Press h inside top to get other helps
env

1. Env:
env allows us to set or print the environment variables. During troubleshooting, we may find it
useful for checking if the wrong environment variable prevents our application from starting.

Examples:
unset variable_name : this will unset or remove a environment variable
env : this will display list of environment variable currently assigned for current users.
Export variable_name=value: using this command you can add an entry to
environment variable
netstat

1. Netstat: Network statistics


This command shows the network status. This command shows network ports in use and their incoming
connections. This is very useful in troubleshooting issues related to port already being used, number of
connection to a ip/port.
Example:
netstat –a : this will display all connections
netstat –at : this will display all only tcp connections
netstat –ut : this will display udp connections
netstat –l : this will show all listening connections, application is up running and listening on a
port
netstat –lx : this will show all linux based process listning on a port
netstat –ap: list all connection with process id
netstat –ntlp: display all service name with pid and uid
netstat –s : displays by protocol statistics for the UDP, TCP, ICMP, and IP protocols.
netstat –i : display network adapter status
netstat –r : will display routing table use –ie for extended info
dig / nslookup

1. Dig (DNS lookup utility) / nslookup(query Internet name servers interactively):


this is very useful in troubleshooting dns mapping which meand if ip<->dns name is mapped
correctly or not. It is used for verifying and troubleshooting DNS problems and to perform
DNS lookups. Dig command replaces older tools such as nslookup and the host.
Example:
dig www.google.com
nslookup www.google.com
nslookup -type=ns www.google.com : shows name server records
nslookup -query=mx google.com :mx records for mail server
nslookup –debug: run in debug mode with lot of details

2. Dig is a big advance version of nslookup and host so these three commands dig, nslookup and
host can be used inter-changably
lsof

1. lsof:
This command is list of open files. An open file may be a regular file, a directory, a block special
file, a character special file, an executing text reference, a library, a stream or a network file
(Internet socket, NFS file or UNIX domain socket.) it shows all open files and the process name
opened it or using it.
Examples:
lsof : this will list down all the open files.
lsof –u Shashwat : this will display all files open by user Shashwat
lsof –i TCP:22 : this will display running on specific port
lsof –I TCP:1-1100: this will display process using port between 1 and 1100
lsof –I –u Shashwat : this will show user Shashwat is looking on which file or which command
lsof –I : this will list all network connections
lsof –p <pid> : list all open files by process belonging to <pid>
kill -9 `lsof –t –u Shashwat` : this will kill all process of user Shashwat
Finding out Linux distro from command line

1. There are many options to find out information about the linux distribution you are working on
here are some of them.

1. cat /etc/os-release

2. Hostnamectl

3. uname -a
df

1. df:
This command display free disk space. This is very useful command to troubleshoot disk
related issues.
Example:
df –h : display disk inform in human readable form
df –a : Include in the listing dummy, duplicate, or inaccessible file systems,
which are omitted by default
df –I : List inode usage information instead of block usage, data structure in a Unix-style
file system that describes a file-system object such as a file or a directory.
df --si : Append an SI-style abbreviation to each size like GB, MB, KB
df -T or df --print-type : this will pring type of file system for each partitions.
du

1. du:
This means disk utilization, this display file size in the current directory if no path has
specified. This displays detailed information about which files use the disk space in a
directory.
Example:
du –h: this will display disk uses by files and directories in human readable format
du –s: this displays summery of dises all file and directory on path provided
du --time : this will display uses and last modified time
du --one-file-system /var or du -x /var : Skip directories on different filesystems*
du -ahx --max-depth=1 /var : this will display maximum to 1 level up
du -ahx --max-depth=1 /var | sort -k1 –rh : get file size sorted by uses
id

1. id:
print real and effective user and group IDs, this can be useful in checking which user we are
what groups we belongs to and if we thing from group we are part of can not perform some
command then we can switch required user using su(switch user)

id: this will display userid, groupid and the groups current user belongs to.
chmod

1. chmod:
this can be used to correct the permission of a file or directory, define accessibility(readability,
writability and executability) of a file or a directory. Basically, permission based on
user:group:others, basically it changes the access permissions of the named files.
Note: never changes the permissions of symbolic links
Example:
chmod -c --or-- chmod --changes: this will display what chanes were made to a file.
chmod --reference=REF_FILE : this will change permission of file same as REF_FILE
chmod –R or chmod –recursive : this will change the permission recursively
chown

1. chown:
change file owner and group.
Example:
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...
chown Shashwat:Shashwat test: this will change the test file owner and group to
Shashwat
chown –v : changes the ownership and display in verbose mode
chmod --from=CURRENT_OWNER:CURRENT_GROUP : this will matches the
provided owner referenced and only change ownership if it matches.
chown --reference=RFILE : change the ownership as referenced file
chown –R : change the permission recursively
touch

1. touch:
change file timestamps. Update the access and modification times of each FILE to the current
time
Example:
touch -a(change access time) –m(change modification time) –t(use time stamp instead of
current time)

2. Type touch –help : for more options


stat

1. stat:
This display file or file system status.
Example:
stats <filename> : this will display very detailed information about a file as following:
history

1. history:
show the history of all command executed.
Example:
history: will display all the command executed by current user over the time
history 10: this will display last 10 command from history.
!#: hash represent number, this will execute number given from history entries
!4 : run entry number 4 from history
sudo !! : run the most recent command which ran, last command from history
!sudo su : this will run most recent command containing sudo su
history|grep text: search text from history entries
newcommand !$ : this will run a new command with parameter in last command from
history
history –c: clearup the history
uptime

1. uptime:
show the system uptime starting from a specific time. It displays the current time, how long
the system has been running, how many users are currently logged on, and the system load
averages for the past.
Example:
uptime –p: show uptime in pretty format
uptime –s : system up since, in yyyy-mm-dd HH:MM:SS format
1. users:
print the usernames of users currently logged in to the current host.

2. w:
Show who is logged on and what they are doing

3. who:
show who is logged on
tar

1. tar:
use to archive file to a compressed format. This can be used to compress file and save space.
Example:
tar –cvzf compressedfilename.tar.gz <list of files> : create compressed file out or list of files
specified.
tar –xvzf compressedfilename.tar.gz : uncompress the file to normal files out or archive
1. Ssh: 4. ftp:
using this we can connect remotely to other ssh connect to remote ftp system
enabled system and work as if we are on that
system itself. 5. sftp:
Ssh user@hostname OpenSSH secure file transfer, its like secure ftp file
copy
2. rsync:
a fast, versatile, remote (and local) file-copying tool 6. telnet: used to connect to telnet to remote server on
Example: specific port, great to check connectivity
Pull: rsync [OPTION...] [USER@]HOST:SRC...
[DEST] 7. Nc: Netcat is a utility that reads and writes data
Push: rsync [OPTION...] SRC... across network connections, using the TCP or UDP
[USER@]HOST:DEST protocol also can be used to check connectivity.

3. scp: 8. Note: Unlike SCP , which supports only file


OpenSSH secure file copy, secure remote file copy transfers, the SFTP allows you to perform a range
scp [OPTION] [user@]SRC_HOST:]file1 of operations on remote files and resume file
[user@]DEST_HOST:]file2 transfers.a
1. ss:
This command is used to dump socket statistics, similar to the legacy netstat utility. To include
both listening and non-listening sockets use -t and -a flags

2. netstat:
This command like ss but old version which print network connections, routing tables,
interface statistics, masquerade connections, and multicast memberships

3. Ip:
show / manipulate routing, network devices, interfaces and tunnels
1. find:
search for files in a directory hierarchy, this command is very userful in searching any file on
linux box. This Use it to search for files based on filenames, permissions, userid, groupid,
size, file type, besides other criteria.
Example:
find / -iname text : this will search file name text in /
find . -name a.txt : search for a.txt file
find . -type d : this will search all directories
find . -type d : this will search for all files
find . -size +20M : find files larger than 20MB

2. Play with this command there are many options


1. systemctl:
Control the systemd system and service manager. The systemctl command manages both
system and service configurations, enabling administrators to manage the OS and control the
status of services. Further, systemctl is useful for troubleshooting and basic performance
tuning.
Example:
systemctl restart/stop/start/mask/enable/disable {servicename} :
restart/stop/start/stop/enable/disable service from starting systemctl reboot : reboot
system
systemctl list-units --type=service : displays status of all services
systemctl list-units --type=service --state=active : list services which are active
systemctl list-units –failed : list failed services
systemctl is-active/is-enabled servicename : check if a service is active or a service is
enabled
1. crontab:
maintain crontab files for individual users. This can be used to schedule jobs to run
automatically and specified time interval, this is very useful in scheduling jobs or scripts.
Example:
crontab –e : this will opne crontab in default editor where we can put entry for scheduling a
job/script
crontab –l : this will display all schedule crontab
Play and learn with crontab : https://crontab.guru/

2. at : this command also can be used to schedule scripts/jobs


Example:
at 6pm + 6 days
at 6pm + 6 days –f <script.sh> : this script will run at 6pm 6days week
1. Tracepath/traceroute: traces path to a network host discovering MTU along this path

2. hostname : Query system hostname

3. Hostnamectl Query or change system hostname

4. Route: to check routing table


1. Tracepath/traceroute: traces path to a network host discovering MTU along this path

2. hostname : Query system hostname

3. Hostnamectl Query or change system hostname

4. Route: to check routing table


Thanks For Watching
TechGurukul
Please support our
channel by just
liking Subscribing and
pressing bell icon.
dwivedishashwat@gmail.c
om

You might also like