Linux Tips

Below are tips that I’ve used throughout the years to get shit done on the command line. Every geek (apart from the fake ones who deploy on Windows) uses the shell every day; some of these tips I use every day, some only occasionally… anyway it’s my list of “things to remember”. Swan Dive.

Some of the commands below may require additional software that doesn’t come with your default server installation.

Disclaimer: if you’re stupid enough to do any of the below, don’t blame me.

General Things

Firewall / iptables

From netfilter.org:

iptables is the userspace command line program used to configure the Linux 2.4.x and 2.6.x IPv4 packet filtering ruleset

Basic info:

  • Config file: /etc/sysconfig/iptables
  • List rules: iptables -L
  • Open a port: iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT

Adding an IP address to an existing network adaptor

Stolen from here.

cd /etc/sysconfig/network-scripts
ls ifcfg-* # check for existing interfaces
cp ifcfg-eth0 ifcfg-eth0:0

Then edit the ifcfg-eth0:0 file and change the DEVICE and IPADDR settings, eg.

DEVICE=eth0:0
IPADDR=999.888.777.195 # whatever your IP address is

…then…

ifup eth0:0

Adding an additional loopback address in OS-X

ifconfig lo0 alias 127.0.0.2 up

Setting up SSH so you don’t need to enter a password

Stolen from here.

If you manage more than one or two hosts, you likely have to type the same password too often. This can get quite annoying. SSH allows you to setup a public and private keypair. Using these keys, you can connect to any host which has the public key, from any host which has the private key, typing your password only once.

So, the process goes like this:

  1. Generate a keypair
  2. Add your public key to the authorized_keys file on the remote host
  3. … errr that’s it

NB: make sure the .ssh directory exists in the user home directory on both your local and remote machine.

ssh-keygen -t rsa
cd ~/.ssh
cat id_rsa.pub > authorized_keys; chmod 600 authorized_keys # adds your public key to your localhost
chmod 600 authorized_keys
cat id_rsa.pub | ssh YourRemoteHost 'cd .ssh; cat >> authorized_keys; chmod 600 authorized_keys'

Alternatively, you can do that last bit manually by scp‘ing the public key up to the remote host and cat‘ing it onto the authorized_keys.


Command Line Mojo

Change to the Previous Working Directory

cd $OLDPWD # old way
cd - # new way

- on its own is the same as $OLDPWD… nice!


Which version of Linux?

lsb_release -a

You may need to add lsb support, just yum search lsb to find the package for your OS.


Run the last command

!!
sudo !! # when you forget to run the command as root

This is great is your coding up a command line script, and debugging, and continually running the same command one after the other. Using !! will run the last command but not pollute your history.


Open ports

nmap -sT -O localhost
netstat -tlnp

One of those commands usually gives me the info I’m looking for.


Create a local mirror of a site using wget

wget -m -k --adjust-extension http://url-of-site.com

Disk Usage

df -h # show disk usage on all partitions
du -sh /var/log/maillog # size of 1 directory / file
du -sh /var/* # size of all files / folders within a folder

Truncate a file

Useful emptying a log file. There are 2 situations when I want to do this.

Emptying a file from within VI

vi some_file.log
[esc]
:1 # go to top of the file
dG # delete all contents until the bottom

From the command line

cat /dev/null > some_file.log

Clearing the terminal

Any of the following

CTRL+l # lowercase L
clear
reset # resets the shell if it starts acting a bit weird

Setting your date & time using an NTP server

ntpdate time.apple.com