Toolbox
  • Printable version
 
Toolbox
LANGUAGES
Language
Personal tools
Categories
Wikipedia Affiliate Button
 

Linux Cheat Sheet

From BrightByte

Jump to: navigation, search

Some things handy to know when dealing with a Linux shell. Unsorted, incomplete, subjective list.

Contents

Screen

screen
starts a new screen session (vistual console)
ctrl-a d
disconnects from a session
screen -rd
reconnects to a session;
ctrl-a esc
copy mode, for selecting and scrolling. Hit esc again to get out.
ctrl-a 0
selects window 0. (works for all numbers to 9)
ctrl-a "
list windows
ctrl-a Ctrl-c
new window

TeX

texhash
refresh file database (after manual install) -- this is for tetex (and texlive?); MiKTeX uses initexmf -u
manual install of ctan packages
  • latex *.ins (extracts styles, etc)
  • latex *.dtx (makes documentation)
  • move styles, etc to /usr/share/texmf-*/tex/xxx
  • update file database
renaming auto-generated sections
\listofalgorithms
\renewcommand{\listalgorithmname}{Algorithmenverzeichnis}
\printglossary
\renewcommand{\nomname}{Glossar}

Network

arp -a
show everything on the local link
arp -na host
show ARP entry
arping host
ARP-ping host
mtr
Like ping and tracepath combined
netdiscover
look who's on the network
socat
netcat's big brother
the obvious
ping, tracepath, nmap, netstat, nettop
monitoring
tcpdump, tcpick, tcpspy, iperf
download mms stream
mplayer mms://.....foo.wmv -dumpstream -dumpfile foo.wmv
tcpdump -w test.pcap -s 1550 dst 91.198.174.2 and tcp port 80
capture traffix on port 80 to a given IP
tcpflow -i any -C -e port 12345
capture tcp traffic from/to port 12345
iptables -A OUTPUT -p tcp --dport 12345 -j REJECT --reject-with tcp-reset
kills connections to a specific port

= SSL

penssl s_client -state -debug -connect host:port
detailed debug trace of SSL handshake. Good way to find out what cypher is used.

ACPI

echo 105:95:85:75:70 > /proc/acpi/thermal_zone/THRM/trip_points
set thermal trip points
echo 5 > /proc/acpi/thermal_zone/THRM/polling_frequency
set polling frequency

To show thermal status:

cat /proc/acpi/thermal_zone/THRM/temperature
cat /proc/acpi/thermal_zone/THRM/state
cat /proc/acpi/processor/*/limit
cat /proc/acpi/processor/*/throttling

Couldn't figure out where to configure that, so i write myself a small init.d script to set trip points and polling (which was disabled per default, causing overheating & reboots - thanks Feisty...)

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
show available cpu frequencies. Use scaling_cur_freq to get current frequency, cpuinfo_cur_freq appears to get confused about cpu numbers.
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
show available cpu governors (scaling policies). Use scaling_governor to get current frequency.
/usr/bin/cpufreq-selector
CPU frequency settings, provided by package gnome-applets (for whatever reason)
-f: set frequency
-g: set governor

Power Management

Per default, GNOME uses the estimated time to discharge to trigger emergency hibernation. This sucks if you have a wonky battery. To force GNOME to use the percentage charged to trigger hibernation, use this:

gconftool-2 --type bool --set /apps/gnome-power-manager/general/use_time_for_policy false

To see all power management settings, use:

gconftool-2 --s /apps/gnome-power-manager/general
gconftool-2 --s /apps/gnome-power-manager/thresholds

init.d

initctl (start, stop, status)
control init jobs
/etc/inittab
control runlevels, inaddition to /etc/rc*.d
init, telinit
switch runlevel (0: halt; 1: single-user; 2-4: multi-user cli; 5: multi-user X; 6: reboot; S,s: swicth to single-user; a-c: on-demand)
update-rc.d
configure job start/stop at runlevel (or even remove job from rc*.d)
System administrators are not encouraged to use update-rc.d to manage runlevels. They should edit the links directly or use runlevel editors such as sysv-rc-conf and bum instead.
telinit level
sets the runlevel
  • S: single user, no network, no deamons
  • 0: halt
  • 1: single user, no network, no deamons
  • 2: multi-user, no network, no deamons
  • 3: multi-user, text
  • 4: unused
  • 5: multi-user, with X
  • 6: reboot

Apache

  • To temporarily disable some page: Redirect 503 /path/to/page.html

Modules

Update alsa from source

sudo apt-get install module-assistant
sudo m-a update
sudo m-a prepare
sudo m-a a-i alsa
sudo alsa force-unload
sudo depmod -ae
sudo modprobe snd-hda-intel

I needed to do this on Hardy Heron to get alsa to work properly

services

gpm
console mouse service (because i always forget what it's called)

kernel params

vga=791
80x50 characters vga text mode for virtual console (for some reason, this screws with the boot splash though)

console tricks

(some from [1])

fuser -k /media/cdrom
kill processes using /media/cdrom
reset
restore a hosed terminal to normal
ctrl-q
wake up terminal after suspend with ctrl-s
screen -S foo
start a screen session named "foo"
screen -x foo
join screen session named foo without disconnecting the other terminal. Input and putput works for both!.
tail -n +2
passes all but the first line of output. Useful for stripping headers
start-stop-daemon --start
starts a given program only if its not already running. Supports various options, see man page.
supervise
starts a program and restarts it when it dies. Supplied by daemontools/svtools.
sed -n '3~3p'
print every 3rd line
awk '{if (++count%3==0) print $0;}
print every 3rd line
perl -ne 'print if($.%3==0)'
print every 3rd line
readlink -f
resolve to absolute, real path. Poor man's realpath.
ls -1 | tr '\n' ':'
list of files names separated by ":" (nice for building a path)
read x y z <<< "$foo"
break a variable into words [2]
read x y z < <(echo a b c)
piping something into read [3]
cat /proc/loadavg
system load, without calling uptime
cut -c1-80
truncate lines at 80 chars
fold
wrap lines
set -o ignoreeof
do not terminate shell on EOF
shell function for pasting to a bin
pastebin() { curl -F 'sprunge=<-' http://sprunge.us/; }
[[ $0 = /* ]] && echo "$0" || echo "$PWD/${0#./}"
determin a script's own location (hopefully). see http://mywiki.wooledge.org/BashFAQ/028 for a full discussion.
pv
measure throughput through a pipe. handy to show progress on long running pipe jobs.
rsync -r A/ B/
copy the content of directory A into directory B (including all dot files, etc). Note that A/ will copy the contents of A, while just A will copy the directory (with its contents).
(thanks to hyphenated of #bash for this one)

SSH / Login

passwd -d <user>
disables password-based login for that user, both via ssh and locally. Unlike passwd -l, the acccount is not expired, and can be accessed via public key authentication, etc.
getent passwd <user>
look up the user not only in /etc/passwd but also NIS, LDAP, Samba, etc.
shopt | grep login_shell
is this a login shell?
echo $TERM | grep linux
is this a (linux) text terminal (vterm)?
export TMOUT=300
cause shell to automatically close after 5 minutes of inactivity.

shell scripts

set -e
script exists on any error.
set -u
treat unset variables as errors

mail

mutt -x -a somefile -s "some message" your@address < /dev/null
send mail with file attachment from the command line (no body)

system info

from [4]

cat /proc/cpuinfo
cpu make, speed, etc
dmidecode
BIOS info
ethtool -i eth0
network card info

Keyboard

loadkeys keymaps-file

xmodmap

keycode 66 = ISO_Level3_Shift
Makes CapsLock behave like AltGr - nice for programmign on a german keyboard

Configuration can be done in ~/.Xmodmap.

To execute automatically, put this into your ~/.xsessionrc:

 if [ -f ~/.Xmodmap ]; then
       xmodmap ~/.Xmodmap
 fi

To find out key codes, use xev

My .Xmodmap file:

keycode  66 = ISO_Level3_Shift
keycode  49 = asciicircum degree notsign notsign dead_circumflex dead_abovering
keycode  21 = apostrophe grave cedilla cedilla dead_acute dead_grave
keycode  34 = udiaeresis Udiaeresis diaeresis diaeresis dead_diaeresis dead_diaeresis
keycode  35 = plus asterisk asciitilde macron asciitilde dead_tilde

File System

lsof name
shows processes using a file or device

i18n files

  • Merge Multiple .po files into one: msgcat /tmp/*.po | msgfmt -o l10n/fr_CA/LC_MESSAGES/civicrm.mo -
  • Compile: msgcat *.po | msgfmt -o civicrm.mo -

Alt-SysRq

Alt-SysRq-something lets you talk directly to your system. See [5] for a list of commands. Here are the meanings of a few Alt-SysRq keys:

h
help
i
kill all processes, except ini
t
dump current tasks
b
dirty reboot (no disk sync!)
o
dirty shutdown

See /proc/sys/kernel/sysrq to find out if Alt-SysRq is enabled.

To trigger a SysRq programatically, use echo x > /proc/sysrq-trigger

Baby Mode

xtrlock
lock input but not the screen until pw is entered

KDE

  • save current session:
    dbus-send --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSession

xorg

xrandr
manipulate video mode & resolution at runtime
xbindkeys
bind hotkeys

vim

:set paste
enable past mode, disable auto-indent
:syntax on
enable syntax highlighting
:w !sudo tee %
save file with root permissions

DNS

host foo 192.168.0.1
lookup at a specific host
host -t srv foo
lookup of a specific record type
dig foo @192.168.0.1
detailed lookup with dig, at a specific host
  • When changes to DNS fail to take effect:
    • check /etc/hosts for hard coded entries
    • check /etc/resolv.conf for which DNS server will be used
    • restart dnsmasq if running
    • restart nscd if running
getent hosts <hostname>
resolves the host name, but also considers /etc/hosts (which host and dig don't do)

clock

  • in /etc/default/rcS set UTC=yes
  • use hwclock --set --utc --date="...."

iptables

log outgoing tcp connections to port $foo
iptables -A OUTPUT -p tcp -m state --state NEW --dport $foo -j LOG --log-ip-options --log-tcp-options --log-prefix "DIRTY FOO: " --log-uid --log-level warn
see output with dmsg or via syslogd.

harddisk

hdparm
tweak disk parameters
hdparm -t <device>
raw read benchmark
fio
advanced i/o benchmarks
blkid
show labels and uuids for block devices

gnome

pulseaudio -k 
kill
start-pulseaudio-x11 
(re)start
gnome-session-save --kill
terminate session (with gui prompt)
/etc/init.d/gdm restart
kill all sessions (no prompt)
unity --replace --display :0 & disown
(re)start unity without losing the current session. also works from a ctrl-alt-f4 virtual console.
compiz --replace --display :0 & disown
(re)start compiz (and unity) without losing the current session. also works from a ctrl-alt-f4 virtual console.
gsettings set org.gnome.desktop.interface text-scaling-factor 0.9
set font scaling catory to 0.9

See Also

Links