20 Linux Commands Every Sysadmin Should Know
20 Linux Commands Every Sysadmin Should Know
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.
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
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
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
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
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.,
$cat filename
2) To view multiple files
Command:
$cat -n filename
4) Create a file
Command:
$cat -s geeks.txt
7) Cat command can append the contents of one file to the end of
another file.
Command:
$tac filename
$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"
$cat -- "-dashfile"
12) Cat command if the file has a lot of content and can’t fit in the
terminal.
Command:
14) Cat command to display the content of all text files in the folder.
Command:
$cat *.txt
$ tail state.txt