Linux Notes

List of common Linux things I’ve found useful.

SSH

If using a .pem file, don’t forget to add to ssh. Use ‘ssh-add {key}’

Ports and processes

ps aux to show all processes. ps f -p {id} to see details for a specific process id

port usage. lsof -i :8080 will show if something is using port. Can use process id to figure out exactly what it is. Also, netstat -lntp shows all listening ports with program name

Disk space

Use the df command for disk freestats.

Use du for disk usage statistics. Here are the common outputs. If want sorted, easiest is to use du --max-depth=1 | sort -n

  • -h human readable.
  • -a plus files.
  • -c grand total.
  • -s for summary.
  • –max-depth to control how deep into structure it goes.

SCP

Reference page SCP Examples

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents.
To copy the entire revenge directory from your deathstar.com account to your empire.gov account, enter:

scp -r dvader@deathstar.com:~/revenge ~/revenge

Terminal manager

Started using Console2 on windows, and like having multiple command prompts in one window. dvtm is available for most linux systems, and provides similar functionality.

all commands start with ctrl-g

c=create
x=close
h/l=decrease/increase master size
j/k=focus next/previous window
.=minimize current window
u/i=focus next/prev non-min window
m=maximize current window
t=vertical stacking
g=grid layout
s=show/hide status bar
enter=zoom/cycle current window to/from master
space=toggle defined layouts
b=others on bottom

Screen usage

Use screen to start a new screen.

Crtl-a d detaches from screen but does not terminate
Ctrl-a k to kill existing window. Use with caution.
screen -ls to list active screens

RAID

mdadm is the main command for interacting with RAID. No need to enable any RAID settings in your BIOS as Linux manages it all.

cat /proc/mdstat is your friend. Shows the current status of the raid arrays, and if there’s an issue, which array is having problems. If in the process of restoring, this file is also updated with the current status.

To add a drive to an array, use mdadm --add /dev/md0 /dev/sda1 where md0 is the name of the RAID array, and sda1 is the drive to be added to the array. –remove can be used for removing a bad disk

Find command and exec

This Stackoverflow question has great details on find with exec. Can use -and plus -or. Don’t forget the ; needs to be a separate parameter.