DevOps / Sys Admin Q & A #0 : Linux system admin's day to day tasks
To check hostname, issue the following command which will show short hostname:
The current hostname, if any, will be displayed.
Then, to check the existing fully qualified domain name (FQDN), if any, execute:
$ hostname -f
To change hostname, modify /etc/hostname.
To change the time zone, we must find the proper zone file in /usr/share/zoneinfo/ and link that file to /etc/localtime:
$ ln -sf /usr/share/zoneinfo/US/Pacific /etc/localtime ## PST/PDT (Pacific Time) $ ln -sf /usr/share/zoneinfo/EST /etc/localtime ## for Eastern Standard Time $ ln -sf /usr/share/zoneinfo/US/Central /etc/localtime ## for American Central time (including DST) $ ln -sf /usr/share/zoneinfo/US/Eastern /etc/localtime ## for American Eastern (including DST) $ ln -sf /usr/share/zoneinfo/UTC /etc/localtime ## for Universal Coordinated Time
This file has static association info between IP addresses and hostnames, with higher priority than DNS.
Every hosts file should begin with the line 127.0.0.1 localhost.localdomain localhost.
127.0.0.1 is the loopback address, and is used to send IP traffic internally on the system.
We can leave this line alone.
The ping command tests the connection between the local machine and a remote address or machine.
It sends a bit of data (i.e. an ICMP packet) to the remote host, and wait for a response. If the system is able to make a connection, for every packet it will report on the "round trip time":
$ ping google.com PING google.com (126.96.36.199) 56(84) bytes of data. 64 bytes from sfo07s13-in-f14.1e100.net (188.8.131.52): icmp_seq=1 ttl=54 time=28.4 ms ...
In this case sfo07s13-in-f14.1e100.net is the reverse DNS (resolving an IP address into a domain name) for this IP address. The time field specifies in milliseconds that the round trip takes for an individual packet. When we've gathered the amount of information we need, send Control+C to interrupt the process. At this juncture, we'll be presented with some statistics. This will resemble:
--- google.com ping statistics --- 14 packets transmitted, 14 received, 0% packet loss, time 13021ms rtt min/avg/max/mdev = 26.844/29.143/35.526/2.564 ms
The traceroute command expands on the functionality of the ping command. It provides a report on the path that the packets take to get from the local machine to the remote machine.
Route information is useful when troubleshooting a networking issue: if there is packet loss in one of the first few "hops" the problem is often related to the user's local area network (LAN) or Internet service provider (ISP).
By contrast, if there is packet loss near the end of the route, the problem may be caused by an issue with the server's connection.
$ traceroute google.com traceroute to google.com (184.108.40.206), 30 hops max, 60 byte packets 1 homeportal (192.168.1.254) 131.172 ms 133.163 ms 133.148 ms 2 108-239-132-2.lightspeed.frokca.sbcglobal.net (220.127.116.11) 133.099 ms 132.982 ms 132.904 ms 3 18.104.22.168 (22.214.171.124) 132.851 ms 132.781 ms 132.664 ms 4 126.96.36.199 (188.8.131.52) 132.625 ms 184.108.40.206 (220.127.116.11) 145.620 ms 145.572 ms 5 18.104.22.168 (22.214.171.124) 132.441 ms 135.641 ms 145.281 ms 6 126.96.36.199 (188.8.131.52) 229.625 ms 184.108.40.206 (220.127.116.11) 232.860 ms 83.888 ms 7 18.104.22.168 (22.214.171.124) 86.622 ms 86.662 ms 126.96.36.199 (188.8.131.52) 86.615 ms 8 184.108.40.206 (220.127.116.11) 86.565 ms 86.515 ms 86.463 ms 9 sfo03s01-in-f14.1e100.net (18.104.22.168) 99.304 ms 99.119 ms 99.207 ms
The mtr command, like the traceroute tool, provides information about the route that Internet traffic takes between the local system and a remote host.
However, mtr provides additional information about the round trip time for the packet. We can think of mtr as a combination of traceroute and ping.
$ mtr --report google.com Start: Tue Jun 28 10:31:09 2016 HOST: laptop Loss% Snt Last Avg Best Wrst StDev 1.|-- 2602:306:cef8:7280::1 10.0% 10 156.2 249.1 156.2 352.8 63.2 2.|-- ?? 100.0 10 0.0 0.0 0.0 0.0 0.0 3.|-- ?? 100.0 10 0.0 0.0 0.0 0.0 0.0 4.|-- ?? 100.0 10 0.0 0.0 0.0 0.0 0.0 5.|-- ?? 100.0 10 0.0 0.0 0.0 0.0 0.0 6.|-- sfo07s13-in-x0e.1e100.net 10.0% 10 323.8 278.0 202.5 331.4 48.7
If we need to check how much memory our system is using at the current moment, we may want to issue the following command:
$ free -m total used free shared buffers cached Mem: 3545 3295 249 50 270 206 -/+ buffers/cache: 2818 726 Swap: 3681 849 2832
This output takes a little bit of careful reading to interpret correctly.
Out of a total 3545 megabytes of memory (RAM), the system is using 3295 megabytes, and has 249 megabytes free.
However, the system also has 2818 megabytes of "stale" data buffered and stored in cache. The operating system will "drop" the caches when and if it needs the space, but retains the cache if there is no other need for the space.
It is totally normal for a Linux system to leave old data in RAM until the space is needed, and we should not be alarmed if only a small amount of memory is actually "free."
In the above example, there are 726 megabytes of memory that are actually free. This means 726 megabytes are available to our system when we start an additional process or a running application needs more memory.
The vmstat provides information about memory, swap utilization, IO wait, and system activity.
It is particularly useful for diagnosing I/O-related issues:
$ vmstat 1 3 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 1 870044 273424 277816 211416 76 35 227 119 54 92 36 6 53 4 0 1 0 870044 271148 277816 211440 0 0 0 4 630 1153 16 3 81 1 0 0 0 870044 268904 277816 211428 0 0 0 336 812 1345 23 5 73 0 0
As we can see from the output, this runs a vmstat every second, three times.
The memory and swap columns provide the same kind of information provided by the "free -m" command.
The most salient information produced by this command is the wa column, which is the final column in most implementations.
This field displays the amount of time the CPU spends waiting for IO operations to complete.
If this number is consistently and considerably higher than 0, we might consider taking measures to address our IO usage. However, if the vmstat output resembles the above, we can be sure in the knowledge that we're not experiencing an IO-related issues.
If we are experiencing an intermittent issue, we will need to run vmstat when we experience the issue in order to properly diagnose or rule out an IO issue. vmstat output can sometimes help support diagnose problems.
For a more organized and real-time view of the current state of our system, we can use htop. However, this is not installed by default on most systems. So, we need to install htop:
$ sudo apt-get install htop
To get the list of installed packages:
$ dpkg -l
apt-cache search may be helpful if we're looking for a specific piece of software but don't know what it's called:
$ apt-cache search elasticsearch php-horde-elasticsearch - Horde ElasticSearch client kibana - Explore and visualize your Elasticsearch data. elasticsearch - Elasticsearch is a distributed RESTful search engine built for the cloud. Reference documentation can be found at https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html and the 'Elasticsearch: The Definitive Guide' book can be found at https://www.elastic.co/guide/en/elasticsearch/guide/current/index.html
This will search the local package database for a 'elasticsearch' and generate a list with brief descriptions.
To see the full record on a package:
$ apt-cache show elasticsearch
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization