GNU Linux/Tips

From Wiki
< GNU Linux
Revision as of 11:27, 3 August 2012 by Deoren (talk | contribs) (Added rsync example)
Jump to: navigation, search

Distribution Independent

Tip/Tricks/Guides that work for any distribution (or at least should).

Output Redirection

  • Save both standard output and error message: >&
  • Save standard output only: > or 1>
  • Save error messages only: 2>

These can be combined, like so:

 gcc test.c 2> error.msg > /dev/null

This saves error messages but tosses standard output

Background and Foreground processes

Putting a command into the background

Example: yum -y update & Output: [1] 21457

This only works when the user is logged on.

Bringing a command into the foreground

Use fg.

Example: fg %1 or fg 21457

Misc: kill %1 nukes it.


Converting video file to sequence of still images

 ffmpeg -i video_file directory/filename.%04d.png

Piping (transferring) contents from one system to another via ssh

Instead of creating an archive on one system and then moving it, you have the option of piping the content through ssh and handling the contents how you choose on the other end. For these examples I'll use System1 and System2 to represent two different systems you want to transfer content between; rsync is another option.

Using tar/ssh to transfer current files from System1 to System2

# Logged into
cd /home
ssh "cd /home && tar cf - myaccount" | tar xf -

Using tar/bzip2/ssh to backup current files from System1 and place on System2

# Logged into
cd /home
tar cf - my_home_dir | bzip2 -z --best |ssh "cd /mnt/backups && cat > my_home_dir-yymmdd.tar.bz2"

Hostname configuration

Sad to say that I was probably improperly configuring system hostnames for years, but it turns out that it's pretty straightforward. Essentially, you configure the unqualified hostname in /etc/hostname and set the full qualified (FQDN) name in /etc/hosts.

cat /etc/hostname


cat /etc/hosts     localhost    node



Using curl to check web server headers

Let's say for example that you want to make sure that you have the mime type configured properly for a Microsoft Word 2007 [1] or later document, and you have curl installed.

curl -I --user "USER_ACCOUNT"

You'll get back something that looks like:

Enter host password for user 'USER_ACCOUNT':
HTTP/1.1 200 OK
Date: Thu, 26 Jul 2012 13:10:20 GMT
Server: Apache/2.0.55 (Red Hat)
Last-Modified: Thu, 17 Nov 2011 14:12:00 GMT
ETag: "43d230-47cc-cdcdec00"
Accept-Ranges: bytes
Content-Length: 18380
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document

In this case, that is the Content-Type header that we want to provide so we're set.


Making a connection to an untrusted SSL site

Say for example that you need to access content on a SSL-enabled server that you trust, but you don't happen to have the CA certificate on that box (perhaps it is self-signed) and you want to connect anyway.

If the site requires a username/password, then this is how you would do it:

curl --user "USER_ACCOUNT" --insecure

If it doesn't, this is how you would connect:

curl --insecure

Using rsync to mirror content

This synchronizes directory content between two systems, deleting any older content that is no longer present on the destination system.

rsync -arv --delete /home/user/

Distribution Specific

Enabling/Disabling Services starting at boot

Red Hat & CentOS

  1. Find out the name of the service's script from /etc/init.d/ directory. For example, mysqld or httpd [1]
  2. Enable it via chkconfig
    sudo /sbin/chkconfig --add mysqld
  3. Make sure it has been enabled
    sudo /sbin/chkconfig --list mysqld
  4. Set it to autostart
    sudo /sbin/chkconfig mysqld on
sudo /sbin/chkconfig mysqld off



For example, let's say that we would like to set Apache v2.x to start automatically upon system boot.

  1. Get a list of services [2] via service --status-all
  2. We see that apache2 is the correct service name to reference and not apache or httpd.
  3. We set apache2 to auto-start [3] with
    update-rc.d apache2 defaults
  1. Get a list of services [2] via service --status-all
  2. We see that apache2 is the correct service name to reference and not apache or httpd.
  3. We disable apache2 from starting at boot [3] with
    update-rc.d apache2 disable


Using a local mirror

For example if you have a large number of servers that you're responsible for maintaining, it may be useful to mirror content locally and update all of your systems from that source instead of system outside of your network. One reason is a possible bandwidth savings, and another is a faster update source if you find yourself applying a lot of patches during maintenance hours. I found myself in that situation, and I'd often find remote update sources to be slow and unreliable.