{"id":1530,"date":"2013-01-02T08:26:32","date_gmt":"2013-01-02T16:26:32","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1530"},"modified":"2013-02-04T09:23:51","modified_gmt":"2013-02-04T17:23:51","slug":"command-line-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1530","title":{"rendered":"Command line tips and tricks"},"content":{"rendered":"<p>While cleaning up my server recently I found that stringing together several commands with pipes made it easier to check logs, find defaults, and remember Linux commands.<\/p>\n<p><b>history<\/b><br \/>\nI don&#8217;t do a whole lot of things from the command line, so when I want to do something that I\u2019ve done recently, I just use the up arrow key to find the command from the last time I used it. If I\u2019ve done a lot of work on the command line, the history command will save some scrolling. Here\u2019s the tail end of a recent history command.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n&nbsp;&nbsp;499&nbsp;&nbsp;exit\n&nbsp;&nbsp;500&nbsp;&nbsp;history\n&nbsp;&nbsp;501&nbsp;&nbsp;sudo grep sshd:session \/var\/log\/messages \n&nbsp;&nbsp;502&nbsp;&nbsp;sudo tail -n 1000 \/var\/log\/php\/error.log\n&nbsp;&nbsp;503&nbsp;&nbsp;sudo grep Authentication \/var\/log\/messages | wc -l\n&nbsp;&nbsp;504&nbsp;&nbsp;history\n<\/code><\/pre><\/p>\n<p>If I want to check the error log I can just up arrow a couple of times and then hit return. Or I could type !502 and hit return.<\/p>\n<p><b>grep and pipes<\/b><br \/>\nI can never remember all the options for tarring up a file, so I almost always find the last time I used it and use the same command again. But it was a while ago and searching through 500 lines of history isn\u2019t particularly efficient. That\u2019s where grep and pipes come in.<\/p>\n<p><code class=\"preserve-code-formatting\">history | grep tar<\/code><\/p>\n<p>history normally displays on the default output, in this case the terminal. But you can redirect the output to another command or a file. I used the pipe | to redirect the 500 lines of history into the grep command and looked for the characters tar. Rather than displaying 500 lines, I got a few with restart and a coupe of with tar. The target characters are highlighted in red on the terminal.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n&nbsp;&nbsp;279&nbsp;&nbsp;sudo \/etc\/init.d\/apache2 restart\n&nbsp;&nbsp;280&nbsp;&nbsp;sudo \/etc\/init.d\/mysql restart \n&nbsp;&nbsp;428&nbsp;&nbsp;sudo tar -czvf .\/mysql-backup.sql.tgz mysql-backup.sql \n<\/code><\/pre><\/p>\n<p>While checking the messages log, I noticed that someone was trying to break into the server by sending login requests every second or so. I was curious about how many attempts there were, so I looked for the words &#8216;Authentication failure&#8217; in the logs. Note that there is a space in the text I\u2019m looking for so I need to put the search text in quotes. I then piped the result to the wc -l command to count the number of lines. There were almost 10,000 all from the same IP address. We changed our iptables config to only allow 3 attempts from the same IP address and then disable logins for a while.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n493&nbsp;&nbsp;sudo grep &#039;Authentication failure&#039; \/var\/log\/messages\n503&nbsp;&nbsp;sudo grep &#039;Authentication failure&#039; \/var\/log\/messages | wc -l<\/code><\/pre><\/p>\n<p>The output of 493 was thousands of lines like this\u2014all with different users.<br \/>\n<pre><code class=\"\u2019smallest\u2019 preserve-code-formatting\">\nJan&nbsp;&nbsp;1 11:04:56 server sshd[12551]: error: PAM: Authentication failure for illegal user testtest from 218.25.99.148\nJan&nbsp;&nbsp;1 11:05:05 server sshd[12564]: error: PAM: Authentication failure for root from 218.25.99.148\n<\/code><\/pre><\/p>\n<p>You can have multiple pipes as well. Here I want to check just the Authentication&#8217;s for Jan 3 so I cat the messages file to a grep that looks for Jan  3 (note the quotes) and then pipe that to a grep that looks for Authentication.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nsudo cat \/var\/log\/messages | grep &#039;Jan&nbsp;&nbsp;3&#039;| grep Authentication<\/code><\/pre><\/p>\n<p>Now that the logs are cleaned up, I check for successful logins with this command><br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nsudo grep Accepted \/var\/log\/auth.log | tail -20<\/code><\/pre><\/p>\n<p>If there have been more than 20 logins since I last checked, I can make the number larger.<\/p>\n<p><b>php error log<\/b><br \/>\nWe have our server set up to put error messages into an error log rather than displaying them on the screen. Visitors to the site don&#8217;t care about the error messages and crackers can take advantage of the messages to exploit vulnerabilities so there is no reason to display them. However, if you are writing a new page or changing an existing one, you as a programmer can benefit from knowing where your code failed. When I\u2019m coding I always have a terminal window open with this command running.<\/p>\n<p><code class=\"preserve-code-formatting\">tail -f \/var\/log\/php\/error.log<\/code><\/p>\n<p><b>One more grep command<\/b><br \/>\nWhen we updated to the latest version of PHP we started getting messages in the logs for deprecated commands. We fixed most of them, but the locations of others weren\u2019t obvious from the error messages. Specifically, we needed to replace all of the places we used PEAR to access the MySQL database. So starting at the root of our website code I look for every file where we use the PEAR initialization code for MySQL. The -R in the grep command means to recursively search through all folders. You start with the current location and traverse the entire directory tree. Notice the * at the end of the command. I don\u2019t want to look at a specific file, like I did in other examples, but want to look at all files.<\/p>\n<p><code class=\"preserve-code-formatting\">grep -R initialize_db.inc *<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While cleaning up my server recently I found that stringing together several commands with pipes made it easier to check logs, find defaults, and remember Linux commands. history I don&#8217;t do a whole lot of things from the command line, so when I want to do something that I\u2019ve done recently, I just use the &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1530\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Command line tips and tricks<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1530","post","type-post","status-publish","format-standard","hentry","category-computers"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1530"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1530\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}