Commands any Linux Sysadmin should learn first.
These are the commands any Linux Sysadmin should learn first.
These are some of the most useful commands for system administration and devops on Linux.
They are command line tolls because most of the actions on system administration you would want to be automated and scheduled
uptime
Time passed since last restart and statistics about system load and users logged in since then.
[dragos@localhost ~]$ uptime
18:00:51 up 3 days, 20:59, 1 user, load average: 0.01, 0.04, 0.00
For a nicer look use the pretty option.
uptime -p
[dragos@localhost ~]$ uptime -p
up 3 weeks, 3 days, 21 hours
top
Shows system statistics and resource allocation.
CPU and RAM allocation overview list.
top
alias
alias command shortcut name = “the actual command”
alias sayhello="echo ""Hello World!"""
[dragos@localhost rex.red]$ alias sayhello="echo ""Hello World!"""
[dragos@localhost rex.red]$ sayhello
Hello World!
[dragos@localhost rex.red]$
This will last while you are still logged in. If you log out and logon again, the alias will be gone.
Permanent alias
Making the alias permanent for your user. You save it in your .bashrc file.
vim ~/.bashrc
Add the alias at the end of the file.
# User specific aliases and functions
#My Aliases
alias sayhello="echo ""Hello World!"""
Save the file – in vim Esc then :wq enter
Make sure you use the file
source ~/.bashrc
Your alias is there even after a reboot.
whereis
Powerful command to locate files or directories on your drive.
Most useful to find binaries of installed programs.
whereis httpd
free
free -h
RAM allocation and usage statistics.
[dragos@localhost ~]$ free
total used free shared buff/cache available
Mem: 1859812 613712 453892 53532 792208 1036984
Swap: 2158588 234220 1924368
[dragos@localhost ~]$ free -h
total used free shared buff/cache available
Mem: 1.8Gi 599Mi 443Mi 52Mi 773Mi 1.0Gi
Swap: 2.1Gi 228Mi 1.8Gi
[dragos@localhost ~]$
service
service service-name action
[dragos@localhost ~]$ service -h
Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]
It’s useful to find problems with a daemon or service.
It is also used to (start, stop, restart, try-restart, reload, force-reload, status) various services.
service httpd status
[dragos@localhost ~]$ service httpd status
Redirecting to /bin/systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/httpd.service.d
└─php-fpm.conf
Active: inactive (dead)
Docs: man:httpd.service(8)
[dragos@localhost ~]$
[dragos@localhost ~]$ sudo service httpd start
[sudo] password for dragos:
Redirecting to /bin/systemctl start httpd.service
[dragos@localhost ~]$ sudo service httpd enable
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
[dragos@localhost ~]$
You can start a service but to enable it on startup use systemctl.
systemctl
sudo systemctl restart httpd
sudo systemctl reload httpd
sudo systemctl stop httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd
[dragos@localhost ~]$ sudo service httpd start
[sudo] password for dragos:
Redirecting to /bin/systemctl start httpd.service
[dragos@localhost ~]$ sudo service httpd enable
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
[dragos@localhost ~]$ sudo systemctl restart httpd
[dragos@localhost ~]$ sudo systemctl reload httpd
[dragos@localhost ~]$ sudo systemctl stop httpd
[dragos@localhost ~]$ sudo systemctl start httpd
[dragos@localhost ~]$ sudo systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[dragos@localhost ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/httpd.service.d
└─php-fpm.conf
Active: active (running) since Tue 2021-08-31 18:36:43 EEST; 11s ago
Docs: man:httpd.service(8)
Main PID: 3778525 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11255)
Memory: 26.1M
CGroup: /system.slice/httpd.service
├─3778525 /usr/sbin/httpd -DFOREGROUND
├─3778526 /usr/sbin/httpd -DFOREGROUND
├─3778527 /usr/sbin/httpd -DFOREGROUND
├─3778528 /usr/sbin/httpd -DFOREGROUND
└─3778529 /usr/sbin/httpd -DFOREGROUND
Aug 31 18:36:43 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Aug 31 18:36:43 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
Aug 31 18:36:43 localhost.localdomain httpd[3778525]: Server configured, listening on: port 80
lines 1-21/21 (END)
dig
Complete DNS query to find any record related to a domain, name server, root nameserver
dig rex.red
[dragos@localhost ~]$ dig rex.red
; <<>> DiG 9.11.26-RedHat-9.11.26-4.el8_4 <<>> rex.red
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29461
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;rex.red. IN A
;; ANSWER SECTION:
rex.red. 299 IN A 172.67.218.197
rex.red. 299 IN A 104.21.38.42
;; Query time: 347 msec
;; SERVER: 10.4.60.20#53(10.4.60.20)
;; WHEN: Tue Aug 31 10:51:06 EEST 2021
;; MSG SIZE rcvd: 68
The any parameter will give you all the DNS records of that domain.
It’s useful to get an overview of the setup of a domain.
[dragos@localhost ~]$ dig rex.red any
; <<>> DiG 9.11.26-RedHat-9.11.26-4.el8_4 <<>> rex.red any
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28600
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 13
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;rex.red. IN ANY
;; ANSWER SECTION:
rex.red. 243 IN A 104.21.38.42
rex.red. 243 IN A 172.67.218.197
rex.red. 3597 IN NS chan.ns.cloudflare.com.
rex.red. 3597 IN NS alec.ns.cloudflare.com.
rex.red. 3786 IN HINFO "RFC8482" ""
;; ADDITIONAL SECTION:
chan.ns.cloudflare.com. 11396 IN A 172.64.32.82
chan.ns.cloudflare.com. 11396 IN A 173.245.58.82
chan.ns.cloudflare.com. 11396 IN A 108.162.192.82
chan.ns.cloudflare.com. 1707 IN AAAA 2803:f800:50::6ca2:c052
chan.ns.cloudflare.com. 1707 IN AAAA 2a06:98c1:50::ac40:2052
chan.ns.cloudflare.com. 1707 IN AAAA 2606:4700:50::adf5:3a52
alec.ns.cloudflare.com. 7750 IN A 172.64.33.59
alec.ns.cloudflare.com. 7750 IN A 173.245.59.59
alec.ns.cloudflare.com. 7750 IN A 108.162.193.59
alec.ns.cloudflare.com. 80123 IN AAAA 2a06:98c1:50::ac40:213b
alec.ns.cloudflare.com. 80123 IN AAAA 2606:4700:58::adf5:3b3b
alec.ns.cloudflare.com. 80123 IN AAAA 2803:f800:50::6ca2:c13b
;; Query time: 3322 msec
;; SERVER: 10.4.60.20#53(10.4.60.20)
;; WHEN: Tue Aug 31 10:52:02 EEST 2021
;; MSG SIZE rcvd: 408
[dragos@localhost ~]$
+nocomments option makes the response more compact by removing the comments of each section and giving you only the actual response.
[dragos@localhost ~]$ dig rex.red any +nocomments
; <<>> DiG 9.11.26-RedHat-9.11.26-4.el8_4 <<>> rex.red any +nocomments
;; global options: +cmd
;rex.red. IN ANY
rex.red. 10 IN A 172.67.218.197
rex.red. 10 IN A 104.21.38.42
rex.red. 3364 IN NS chan.ns.cloudflare.com.
rex.red. 3364 IN NS alec.ns.cloudflare.com.
rex.red. 3553 IN HINFO "RFC8482" ""
rex.red. 157 IN TXT "google-site-verification=C0S4gecD15hMlSjLUF0bGRNURsYoleGS-v_1zCJ6mr4"
chan.ns.cloudflare.com. 11163 IN A 173.245.58.82
chan.ns.cloudflare.com. 11163 IN A 108.162.192.82
chan.ns.cloudflare.com. 11163 IN A 172.64.32.82
chan.ns.cloudflare.com. 1474 IN AAAA 2a06:98c1:50::ac40:2052
chan.ns.cloudflare.com. 1474 IN AAAA 2606:4700:50::adf5:3a52
chan.ns.cloudflare.com. 1474 IN AAAA 2803:f800:50::6ca2:c052
alec.ns.cloudflare.com. 7517 IN A 173.245.59.59
alec.ns.cloudflare.com. 7517 IN A 108.162.193.59
alec.ns.cloudflare.com. 7517 IN A 172.64.33.59
alec.ns.cloudflare.com. 79890 IN AAAA 2606:4700:58::adf5:3b3b
alec.ns.cloudflare.com. 79890 IN AAAA 2803:f800:50::6ca2:c13b
alec.ns.cloudflare.com. 79890 IN AAAA 2a06:98c1:50::ac40:213b
;; Query time: 26 msec
;; SERVER: 10.4.60.20#53(10.4.60.20)
;; WHEN: Tue Aug 31 10:55:55 EEST 2021
;; MSG SIZE rcvd: 489
[dragos@localhost ~]$
Looking ony at the TXT DNS records of the domain.
[dragos@localhost ~]$ dig rex.red txt +nocomments
; <<>> DiG 9.11.26-RedHat-9.11.26-4.el8_4 <<>> rex.red txt +nocomments
;; global options: +cmd
;rex.red. IN TXT
rex.red. 26 IN TXT "google-site-verification=C0S4gecD15hMlSjLUF0bGRNURsYoleGS-v_1zCJ6mr4"
;; Query time: 0 msec
;; SERVER: 10.4.60.20#53(10.4.60.20)
;; WHEN: Tue Aug 31 10:58:06 EEST 2021
;; MSG SIZE rcvd: 117
[dragos@localhost ~]$
The default is a which returns the IPv4 of the domain.
q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,…) [default:a]
ifconfig
List all interfaces
ifconfig -a
Enable the network card interface
ifconfig eth0 down
Disable the network card
ifconfig eth0 up
Configure the network interface
ifconfig eth0 192.168.1.12
ifconfig eth0 netmask 255.255.255.
ifconfig eth0 broadcast 192.168.1.255
netstat
Shows active network connections
netstat
Show all sockets
netstat -a
Show statistics for all interfaces
netstat -s
Display interfaces
netstat -i
[dragos@localhost rex.red]$ netstat -h
usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help}
netstat [-vWnNcaeol] [<Socket> ...]
netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]
-r, --route display routing table
-I, --interfaces=<Iface> display interface table for <Iface>
-i, --interfaces display interface table
-g, --groups display multicast group memberships
-s, --statistics display networking statistics (like SNMP)
-M, --masquerade display masqueraded connections
-v, --verbose be verbose
-W, --wide don't truncate IP addresses
-n, --numeric don't resolve names
--numeric-hosts don't resolve host names
--numeric-ports don't resolve port names
--numeric-users don't resolve user names
-N, --symbolic resolve hardware names
-e, --extend display other/more information
-p, --programs display PID/Program name for sockets
-o, --timers display timers
-c, --continuous continuous listing
-l, --listening display listening server sockets
-a, --all display all sockets (default: connected)
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
-Z, --context display SELinux security context for sockets
vim
Command line text editors. Especially useful for editing configuration files in emergency situations.
vi file
vim file
nano file