20 Linux Commands Every Sysadmin Should Know

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

20 Linux commands every sysadmin

should know

1. curl
curl transfers a URL. Use this command to test an application's endpoint or
connectivity to an upstream service endpoint. curl can be useful for
determining if your application can reach another service, such as a
database, or checking if your service is healthy.

Ex
$ curl -I -s https://opensource.com

2. Cat
Ex $ cat test.json | jq

3. ls
ls lists files in a directory.
4. tail
tail displays the last part of a file. You usually don't need every log line to
troubleshoot. Instead, you want to check what your logs say about the most
recent request to your application. For example, you can use tail to check
what happens in the logs when you make a request to your Apache HTTP
server.

Ex $ tail -n 100 /var/log/httpd/access_log

5. cat
cat concatenates and prints files. You might issue cat to check the
contents of your dependencies file or to confirm the version of the
application that you have already built locally.

Ex $ cat requirements.txt
6. grep
grep searches file patterns. If you are looking for a specific pattern in the
output of another command, grep highlights the relevant lines

7. ps
The ps command, part of the procps-ng package which provides useful
commands for investigating process IDs, shows the status of a running
process. Use this command to determine a running application or confirm
an expected process

Ex $ ps -ef

Note For even more legibility, use ps and pipe it to grep.


$ ps -ef | grep tomcat

8. env
env allows you to set or print the environment variables. During
troubleshooting, you may find it useful for checking if the wrong
environment variable prevents your application from starting

Ex $ env

9. top
top displays and updates sorted process information. Use this monitoring
tool to determine which processes are running and how much memory and
CPU they consume. A common case occurs when you run an application
and it dies a minute later. First, you check the application's return error,
which is a memory error.

Ex $top

10. netstat
netstat shows the network status. This command shows network ports in
use and their incoming connections. However, netstat does not come out-
of-the-box on Linux. If you need to install it, you can find it in the net-
tools package.

Ex $ netstat -tulpn
11. ip
If ip address does not work on your host, it must be installed with
the iproute2 package. The subcommand address (or just ip a for short)
shows the interfaces and IP addresses of your application's host

$ ip address show eth0

12. lsof
lsof lists the open files associated with your application. On some Linux
machine images, you need to install lsof with the lsof package. In Linux,
almost any interaction with the system is treated like a file. As a result, if
your application writes to a file or opens a network connection, lsof will
reflect that interaction as a file.

$ lsof

13. df
You can use df (display free disk space) to troubleshoot disk space issues.

$ df -h

14. du
To retrieve more detailed information about which files use the disk space
in a directory, you can use the du command. If you wanted to find out
which log takes up the most space in the /var/log directory, for example,
you can use du with the -h (human-readable) option and the -s option for
the total size.

$ du -sh /var/log/*

15. id
To check the user running the application, use the id command to return
the user identity

$ dnf -y install httpd

Then $ id

16. chmod
When you run your application binary for the first time on your host, you
may receive the error message "permission denied." As seen in the
example for ls, you can check the permissions of your application binary.
$ ls -l

then

$ chmod +x test.sh

17. dig / nslookup


A domain name server (DNS) helps resolve a URL to a set of application
servers. However, you may find that a URL does not resolve, which causes
a connectivity issue for your application.

$ nslookup mydatabase or $ dig mydatabase

18. firewall-cmd
Traditionally, firewalls were configured on Linux with
the iptables command, and while it retains its ubiquity it has actually been
largely replaced by nftables

$ sudo firewall-cmd

19. sestatus
You usually find SELinux (a Linux security module) enforced on an
application host managed by an enterprise.

$ sestatus

20. history
When you issue so many commands for testing and debugging, you may
forget the useful ones! Every shell has a variant of the history command.

$ history
1 clear
2 df -h
3 du
Netstat command in Linux
Netstat command displays various network related information such as
network connections, routing tables, interface statistics, masquerade
connections, multicast memberships etc.,

1 -a -all : Show both listening and non-listening sockets. With the –


interfaces option, show interfaces that are not up
# netstat -a | more : To show both listening and
non-listening sockets.

2. List all tcp ports.

# netstat -at : To list all tcp ports.

3 List all udp ports.


# netstat -au : To list all udp ports.

4List only listening ports.


# netstat -l : To list only the listening ports.

5List only listening TCP ports.


# netstat -lt : To list only the listening tcp ports

6 List only listening UDP ports.


# netstat -lu : To list only the listening udp ports.

7 List only the listening UNIX ports


# netstat -lx : To list only the listening UNIX ports

8 List the statistics for all ports.


# netstat -s : To list the statistics for all ports.

9 List the statistics for TCP (or) UDP ports.


# netstat -st(TCP) : To list the statistics for TCP ports.

10 Display PID and program names in the output.


# netstat -pt : To display the PID and program names.
11 Print the netstat information continuously.

netstat will print information continuously every few seconds.


# netstat -c : To print the netstat information continuously.

12 The non-supportive address families in the system.


# netstat --verbose : To get the non-supportive
address families in the system.

13 The kernel routing information.


# netstat -r : To get the kernel routing information.

14 The port on which a program is running.


# netstat -ap | grep ssh : To get the port
on which a program is running

15 Which process is using a particular port:


# netstat -an | grep ':80' : To get the process
which is using the given port.

16 List of network interfaces.


# netstat -i : To get the list of network interfaces.

17 Display extended information on the interfaces


(similar to ifconfig) using netstat -ie:

# netstat -ie : To display extended information


on the interfaces

here main points (1 a=all 2 l =listening 3 s= statics 4


i=networkinterface)

Cat command in Linux with


examples
Cat(concatenate) command is very frequently used in Linux. It reads
data from the file and gives their content as output. It helps us to
create, view, concatenate files. So let us see some frequently used cat
commands.
1) To view a single file
Command:

$cat filename
2) To view multiple files
Command:

$cat file1 file2

3) To view contents of a file preceding with line numbers.


Command:

$cat -n filename

4) Create a file
Command:

$ cat > newfile

5) Copy the contents of one file to another file.


Command:

$cat [filename-whose-contents-is-to-be-copied] > [destination-


filename]

6) Cat command can suppress repeated empty lines in output


Command:

$cat -s geeks.txt

7) Cat command can append the contents of one file to the end of
another file.
Command:

$cat file1 >> file2

8) Cat command can display content in reverse order using tac


command.
Command:

$tac filename

9) Cat command can highlight the end of line.


Command:

$cat -E "filename"

10) If you want to use the -v, -E and -T option together, then instead of
writing -vET in the command, you can just use the -A command line
option.
Command

$cat -A "filename"

11) Cat command to open dashed files.


Command:

$cat -- "-dashfile"

12) Cat command if the file has a lot of content and can’t fit in the
terminal.
Command:

$cat "filename" | more

13) Cat command to merge the contents of multiple files.


Command:

$cat "filename1" "filename2" "filename3" > "merged_filename"

14) Cat command to display the content of all text files in the folder.
Command:

$cat *.txt

15) Cat command to write in an already existing file.


Command :
$cat >> geeks.txt
The newly added text.

df Command in Linux with examples


df command that displays the amount of disk space available on the file
system containing each file name argument.

Tail command in Linux with examples


The tail command, as the name implies, print the last N number of data of
the given input. By default it prints the last 10 lines of the specified files. If
more than one file name is provided then data from each file is precedes by
its file name.

$ tail state.txt

You might also like