Linux

Articles

Configuring your system clock
Recover files from lost+found after a crash

apt update // update the package manager
etc: editable text configuration (/etc)

ls -1 // list the content of the current directory vertically

Quick bash / shell shortcuts:

Ctrl+w       // erase word
Ctrl+u       // erase to the beginning of line
Ctrl+a       // move to beginning of line
Ctrl+e       // move to the end of line

CTRL+S  // pause a scrolling screen
CTRL+Q // resume scrolling

tty
skill -KILL -v pts/0

How can we determine disk space usage?

sudo du -hs * | sort -h
df

How can we remove every files in current directory?

// It is the number 1
ls -1 | xargs rm
find -type f -delete

How can we display the program that is listening on a particular port?

lsof -i -P | grep '3306'

Where can we look for log files / messages?

/var/log/syslog
dmesg
/var/log
log files for the services that we are trying to debug

How can we count unique lines in a file?

cat fruits.txt | sort | uniq -c | sort -nk1

How can we sum up a column?

awk '{s+=$1} END {printf "%.0f", s}'
paste -s -d+ infile | bc

How can we display output as table?

Sometimes, when you see the output of a command, it can be overwhelming to identify the output due to overcrowded strings (for example, the output of the mount command). How about viewing it like a table? This is easy to do:

mount | column –t

In this example, the output is well-formatted because of the spaces. What if the separators were something else, like colons? (For example, in the output of cat /etc/passwd.) Just specify the separator with an –s parameter, like below:

cat /etc/passwd | column -t -s

How can we repeatedly run a command until it run successfully?

03-repeat-till-success.png

In this example, >/dev/null 2>&1 redirects the output of your program to /dev/null. Include both the Standard Error and Standard Out.

How can we sort process by memory usage?

ps aux | sort -rnk 4

How can we sort process by CPU usage?

ps aux | sort -nk 3

How can we watch multiple log files at the same time?

apt-get install multitail

How can we return to the previous directory?

cd -

How can we run a command at regular interval and see its output?

Use the watch command:

watch df –h

The the above statement, the watch command runs the df command at regular interval.

How can we run a program in such a way that it will not be terminated when we log out of the system?

Use the nohup command:

nohup wget site.com/file.zip

Or we can run our program with screen.

How can we automatically answer Yes or No to any command?

That can be done using yes command: yes | apt-get update.

Maybe you want to automate saying "no" instead. This can be done using: yes no | command.

How can we run our last command as root?

sudo !!

How can we determine the program that is listening on a particular port?

sudo lsof -i -P | grep -i "portNumber"
netstat -anp

The output of the netstat command above is a bit verbose. We might want to pipe it through grep and grep for the port number. The last column displays a combination of the process ID, and the name of the local program.

What are my favorite utilities?

  1. screen

How can we delete hard-to-delete files?

  1. Invoke emacs on the directory containing the file in question by CTRL-X CTRL-F
  2. Move the cursor to the file in question
  3. Enter d to mark the file for deletion, u to unmark, # to mark all auto-saved file and ~ to mark all backup files
  4. Enter x to delete all marked file

How to determine the version of the running OS?

uname -a
cat /etc/issue
cat /etc/fedora-release

Shell scripting:
Shell scripting (should rename this link to the appropriate shell language: bash, sh, ksh, etc)
sed
awk
linux-cp
cut
linux-ln
grep
wc
tr
expr
xargs
sort
uniq
source
bash

Working with files:
find
mkdir
touch
tar
cpio
compress
gzip
backup

Downloading or transferring files:
wget
lwp
curl
sftp
scp

System configuration:
system-config-security-level
chkconfig
runlevel

Security:
SELinux
system-config-security-level

Troubleshooting and logging
sar
mpstat
iostat
vmstat
pidstat
top - Prints a display of system processes that's continually updated until the user presses q.
htop
lsof
procinfo
ps
ls
syslog
rsyslog
dmesg - Prints the messages resulting from the most recent system boot.
dconf gather hardware and software snapshot for troubleshooting

User and group management:
passwd
useradd
usermod
userdel
chown
group
chgrp
chmod
su
ulimit

Network:
ethtool
netstat
iptraf
arp
iptraf
tcpdump
dig
sniffit
ifconfig
/etc/resolv.conf
/etc/host.conf
/etc/hosts
/etc/services
/etc/inetd.conf
/etc/networks
/etc/securetty
/etc/hosts.allow
/etc/hosts.deny
bonding
route

Administration:
shutdown
sync
fstab
vfstab
fsck
dump
restore
upstart
inittab

alias
inode
mt
paste
apropos - find possible manual page entries
suspend
od
ioscan (HPUX)
hwconfig (SCO)
lscfg (AIX)
lsdev (AIX)
hinv (IRIX)
scsiinfo (Linux)
hdparam (Linux)
fg
bkhistory
passwd root
xlock
lockscreen
lock
scolock
scologin
vlock -a
write username [tty]
write username@hostname
rwho
confer
joinconf
mesg n
wall
/etc/motd .hushlogin // disable message of the day
/etc/issue
clive
date
ipcs
pgrep / pkill
kill
locate
lspci list PCI devices
lsusb list USB devices
lsdev display information about installed hardware devices
insmod
lsmod
m4
man
nohup
rdesktop
rename
swap
cat
tac
unalias
makekey
X Forwarding

pmap
numactl

killall
skill
snice
stat
init
telinit
initctl
smbmount
fuser
lsdev
lspci
pnpdump
sum
w - See who is logged onto the system.
wall - Prints a message to each user except those who've disabled message reception. Type Ctrl-D to end the message.

visudo
asroot
cron
rdate
mount
umount
dd
split
rsync
LVM
grub
RRDtool
/usr/sbin/mouseconfig
hwconfig
Job Control
Bash History
dd and netcat
X Windows
SSH Tunnels
Password-less SSH
The Shell
free - Displays the amount of used and free.
ping IP - Sends an echo request via TCP/IP to the specified host. A response confirms that the host is connected to the network.
traceroute IP - Uses echo requests to determine and print a network path to the host.
Ethereal / Wireshark
strace
NFS
OpenSSL
SystemImager
RPM
sshd
conman
ssh
nslookup
logrotate
XAMPP / LAMPP
PVM
MPI
Beowulf
Private IP Addresses
Run Levels / Operating Modes
Installing Linux Without Touching MBR
KVM
Networking
misc
Filesystem recovery

How can we display a graph of network usage (on Windows, I think this is done by opening the Control Panel and double click on System):
iftop
ntop

/usr/local/sbin/pure-ftpd -e -i -R &
lokkit
svcadm
/etc/ssh/sshd_config
I've disabled the firewall on my desktop server, made a change to /etc/ssh/sshd_config
Can't open display: localhost:10.0
xclock
xeyes
export DISPLAY=192.168.1.100:0.0
http://forums.fedoraforum.org/showthread.php?t=24838

catman -w
/usr/lib/makewhatis (IRIX)

halt
reboot
mountall
fasthalt
fastreboot
dmsg
newfs
mkproto
whereis
which
elri
basename
dirname
psroff
pcfs
isalist
prtcom
fstyp
limits
blksize
devnam
devnna
getdev
sysdef
prvttoc
mkfs
checkall
df
du
dd
checklist
fsck -b
bcheckerc
brc
uadmin // equivalent of shutdown on SCO, Solaris, IRIX
quotachec -a
quotaon -a
/etc/savecore /var/adm/crash // copy the content of crash dump to a file
swapon -a // start paging
groups // display a list of available user group
groups harvey // list groups that user harvey belong to
newgrp graphics // On older SystemV, change active group
id // display userid and groupid
who -r // display current runlevel
crash // examine crash dump
pwck, grpck // perform syntax checking on /etc/passwd and /etc/group
uptime
last
lastcomm
acctcom
acctcms
kill -9 (KILL) -15 (TERM) -3 (QUIT) -2 (INT)
priocntl
reboot -n // prevent sync
MAKEDEV
mknod
mkfs
mknod name clb major minor
accton
ups_mond // HPUX monitor system physical status
deliver // mail daemon on SCO
lpd // printing daemon on BSD
lpsched // printing daemon on System V and Solaris. Solaris also use sac and listen.
qdaemon // printing daemon on AIX
netls // license server, rely on Network Computing System (NCS)
// local and global location broker daemon, llbd and glbd
expreserve // recover editor files from vi, ed, and other ex-based editor
elvis -vi-clone elvprsv // available on Linux

How can we configure our Linux servers to send / forward / relay email through a relay server?

Most Linux distribution use sendmail. Check your distribution's documentation. For sendmail, go to the sendmail page and search for "relay".

How can we find the version of the installed operating system?

cat /etc/redhat-release
uname -a

How can we use init to respawn a process that keep on dying unexpected?

ndo:345:respawn:/usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg

How can we find the current runlevel

who -r

How can we find the time of when the system was rebooted

who -b

How can we find the MAC address of a remote host given its IP address?

arping 192.168.1.100

How can we determine if two hosts on a network have the same IP address?

arping 192.168.1.100

How can we determine the cause of high IO wait:

  • ps -auxww and look under the "Start" column to see what actual command has been running the longest.
  • get the PID of that command and look for it in /proc
  • cd /proc/"PID"
  • watch 'ls -l fd' (Will actually show files being process in realtime)

I like to use watch instead of strace ( My eyes can't keep up with all the fast moving text. In a seperate xterm windows, run iostat -d 2 (disk) and iostat -c 2 (cpu). It will be cat and mouse for awhile but eventually you will find the culprit.

How can we make a Linux Live CD?

I NEED TO COMPLETE THIS.

What are the advantages of using a live CD (compare to having an image server and booting from network)?

I NEED TO COMPLETE THIS.

What is the graphical tool that we can use to reconfigure the network?

redhat-config-network

How can we reboot the machine from the command line?

shutdown -r now

What does ARP abbreviate for?

Address Resolution Protocol. When a host want to know the ethernet address corresponding to a certain IP, it send out a broadcast query for the IP address. Each receiving host compare it to its own IP address, and if it match, it returns an ARP reply to the inquiring host.

How can we monitor a process and automatically restart it via init if it fails?

Let says that we have a problematic process that need to be on life-support (for whatever reason, it keep crashing). Edit /etc/inittab:

sshd:2345:respawn:/usr/sbin/sshd -D

After adding this entry, run /sbin/telinit q to get it to reload.

What is the name of the kernel executable image?

  1. vmlinux
  2. vmlinuz: compressed

What is the purpose of the /etc/securesingle file?

On Linux systems, if the /etc/securesingle file exists, then password is required for single user mode.

How can we boot into single user mode?

At the boot prompt: specify linux single

What is the purpose of lilo?

I do not remember what lilo is. I think lilo is a boot loader. The /etc/lilo.conf:

other=/dev/hda4
label=dos
table=/dev/hda
lilo -c /etc/lilo.conf

How can we password protect single user mode for Linux using lilo?

1. Edit /etc/lilo.conf:

boot=/dev/hda
map=/boot/map
install=/boot/boot.b
prompt
timeout=50
linear
default=linux

image=/boot/working
label=working
read-only
root=/dev/hda8
restricted
password=...

2. lilo (I NEED TO COMPLETE THIS)

How can we update the superblock of all filesystems?

  • bdflush // on most system
  • syncd // on AIX
  • fsflush // on Solaris
  • update, bdflush // on Linux

How can we monitor unsuccessful login attempts?

The shadow packages on some Linux system will log failures to the binary /var/adm/faillog file if enabled with FAILLOG_ENAB setting in /etc/login.defs. The command **faillog -p" can be used to view login failure.

How can we record our command line session?

If you want to record what you’ve typed on your shell screen, you can use script command to save all of your typings to a file named typescript : script.

Once you type exit, all of your commands will be written to that file so you can review it later.

See https://dzone.com/articles/most-useful-linux-command-line-tricks

How can we Replace Spaces With Tabs?

You can replace any character with any other using the tr command, which is very handy: cat geeks.txt | tr ':[space]:' '\t' > out.txt.

12-tr-command.png

How can we Convert a File to Upper or Lower Case?

You can do this using: cat myfile | tr a-z A-Z > output.txt.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License