Linux Cheat Sheet
Linux Cheat Sheet
ro
System Commands
shutdown bring the system down shutdown -h now ← -h = Halt or poweroff after shutdown
shutdown -r now ← -r : Reboot after shutdown
shutdown -r -F now ← -F : Force fsck after reboot.
halt stop the system. halt
reboot reboot the system. reboot
init init 1 #change to single usermode
uptime Tell how long the system has been running. uptime
runlevel find the previous and current system runlevel. runlevel
printenv print all or part of environment printenv
env run a program in a modified environment env
hostname show or set the system's host name hostname ← show the system's host name
I recommend uname -n for check hostanme.
hostname NEWHOSTNASME ← set the system's host name
uname print system information uname -a ← print all information (=uname --all)
uname -n ← show the system's host name (=uname --nodename)
locale Get locale-specific information. locale
locale -a |grep -i ja <- -a : --all-locales
1
cursuri-linux.ro
Process Management
ps report a snapshot of the current processes. ps aux |grep httpd ← Check httpd rocess
ps aux | grep XXX | awk '{print $2}' | xargs kill -9
ps auxwf
ps auxwf |grep XXX
pgrep look up processes based on name and other attributes pgrep -f 'bash'
pgrep -lf 'bash' ← output with process name
pgrep -f 'bash' | xargs kill
pstree display a tree of processes pstree -a
pidof find the process ID of a running program pidof httpd
/bin/kill $(/sbin/pidof qmail-popup)
kill send a signal to a process kill -9 PID ← (-9 or -KILL = force-quit)
pkill signal processes based on name and other attributes pkill -f 'bash'
pkill -u user1
pkill java
pkill -f jar
killall kill processes by name killall vi
killall -i vi ← -i = Interactively
killall -HUP kterm
killproc
lsof list open files lsof -i
lsof -i -P ← no port names
lsof -i :80,443 ← Which process is using Port 80,443
Ctrl + C Stop running process
Ctrl + Z Suspend running process Move Running Process to Background
1. ctrl + z
2. jobs
3. bg
4. disown %JOBID
jobs The first form lists the active jobs. jobs -l ← List job
2
cursuri-linux.ro
fg Resume jobspec in the foreground
bg Resume each suspended job jobspec in the background
nohup run a command immune to hangups, with output to a non-tty nohup command.sh &
disown disown %jobid
nice run a program with modified scheduling priority nice -n 19 test.sh
nice -n 19 ionice -c 3 CMD
nice -n 19 ionice -c 2 -n 7 COMMAND
renice alter priority of running processes renice 19 -p PID
you can check the nice whith “top” or “ps alx”.
ionice sets or gets process io scheduling class and priority ionice -p PID ← check
ionice -c 3 -p PID
nice -n 19 ionice -c 2 -n 7 COMMAND
3
cursuri-linux.ro
Schedule
4
cursuri-linux.ro
etc
alias Alias with no arguments or with the -p option prints alias ← check all alias
the list of aliases alias ls='ls -la --color=auto'
When arguments are supplied, an alias is defined for alias grep='grep --color'
each name whose value is given.
unalias Remove each name from the list of defined aliases. unalias COMMAND
If -a is supplied, all alias definitions are removed.
ntpdate set the date and time via NTP ntpdate -b -u IP
-b : Force the time (step mode)
-u : If you are running ntpd, “-u” must be added.
chronyc command-line interface for chronyd chronyc sources
chronyc sources -v
chronyc sourcestats ← check offset
chrony makestep ← Correct the time
ntpq standard NTP query program ntpq -p
-p : Print a list of the peers known to the server
watch -n 1 ntpq -p
hwclock query and set the hardware clock (RTC) hwclock (-r) ← Read the Hardware Clock and print the
time on standard output.
hwclock -w ← Set the Hardware Clock to the current
System Time.
hwclock -s ← Set the System Time from the Hardware
Clock.
man an interface to the on-line reference manuals man COMMAND
whatis display manual page descriptions whatis KEYWORD
whatis cat
whatis vi
history GNU History Library history |less
history 5 ← lists only the last 5 lines.
HISTSIZE=1000
HISTTIMEFORMAT=“%Y/%m/%d %H:%M:%S ”
5
cursuri-linux.ro
which locate a command which ls
which -a bash ← -a : print all matching pathnames of
each argument
time time a simple command or give resource usage time sleep 5
strace trace system calls and signals strace -t php test.php
strace -t -o test.txt php test.php
-t : each line of the trace with the time of day.
ltrace A library call tracer lltrace -o test.txt wget http://example.com/
ltrace -p PID
ltrace -p 3365
script make typescript of terminal session script -afq $LOG
6
cursuri-linux.ro
File Commands
7
cursuri-linux.ro
Directory Commands
8
cursuri-linux.ro
9
cursuri-linux.ro
Searching
10
cursuri-linux.ro
File Compression
tar The GNU version of the tar archiving utility tar zcvf test.tar.gz Dir ← Create, Verbose, File
tar warn the order of target and destination. tar ztvf test.tar.gz ← Test, Verbose, File
tar zxvf test.tar.gz ← eXtract, Verbose, File
tar jcvf test.tar.bz2 DIR ← Create, Verbose, File
tar jxvf test.tar.bz2
tar zcvf /tmp/user01.tar.gz user01
tar zxvf user01.tar.gz -C /home
gzip compress or expand files gzip file1
gunzip gunzip file1.gz
gzip access_log.2011-[0][5-8]*.txt ←wild card
compress package and compress (archive) files compress file1
uncompress uncompress file1.Z
bzip2 a block-sorting file compressor bzip2 file1
bunzip2 bunzip2 file1.gz2
bz2cat bz2cat file1.gz2
zip package and compress (archive) files zip file.zip file1 file2
unzip zip -l file.zip ← -l = list for check
unzip file.zip
unzip -t file.zip ← -t = test
lha lha a file.lzh file1 file2
lha t file.lzh
lha x file.lzh
gzcat gzcat file.gz
gzcat file.Z
zcat zcat file.Z
zless file perusal filter for crt viewing of compressed text zless file.gz
zgrep search possibly compressed files for a regular expression zgrep PATTERN file.gz
zegrep search possibly compressed files for a regular expression zegrep “new” test.txt.gz
11
cursuri-linux.ro
zdiff compare compressed files zdiff file1.gz file2.gz
Character
12
cursuri-linux.ro
User
useradd create a new user or update default new user Adminuser on RHEL
information useradd -G wheel USER1
Adminiuser on Ubuntu
useradd -m -s /bin/bash -G sudo USER2
useradd -u UID -g GROUP -G GROUP1,GROUP2 -s /bin/bash -d
HOME_DIR LOGIN
useradd -D ← check Default Parameter
adduser add a user to the system
whoami print effective userid whoami
w Show who is logged on and what they are doing. w
who show who is logged on who
who --all
userdel delete a user account and related files userdel -r USER
← (-r, --remove : Files in the user's home directory will
be removed)
vipw edit the password, group, shadow-password or vipw ← edit /etc/passwd
shadow-group file vipw -s ← edit /etc/shadow
passwd change user password passwd
passwd user1
echo "password01" | passwd --stdin user01
passwd -S user1 ← check about the status of the password
passwd -l user01 ← Lock the user
passwd -u user01 ← Unlock
passwd -d user01 ← delete password
chpasswd update passwords in batch mode echo user01:password | chpasswd
echo 'USER:PASS' > tmp.txt ; chpasswd < tmp.txt ; rm -f
tmp.txt
mkpasswd
13
cursuri-linux.ro
chage change user password expiry information chage -l USER ← check
chage -M 90 USER ← the password expires day set 90days
change -d 0 USER ← force a user to change their password
at next login
usermod modify a user account usermod -g GROUP USER
usermod -g GROUP -G SUBGROUP USER
usermod -G SUBGROUP USER
usermod -aG SUBGROUP USER ← add Group
usermod -G SUBGROUP1,SUBGROUP2 USER
usermod -G "" USER
usermod -l USER_NAME_NEW USERNAME_OLD ← change username
usermod -d HOME_DIR_NEW USER_NAME ← change home directory
usermod -u UID USER ← change UID
gpasswd gpasswd -a USER sudo ← add USER to GROUP
gpasswd -r USER sudo ← remove USER from GROUP
chsh change login shell chsh -l ← = cat /etc/shells
chsh -s /bin/bash ← changing shell
chsh -s /bin/bash user01
getent get entries from Name Service Switch libraries getent passwd ← you can check LDAP Users
getent group
getent shadow
pam_tally2 The login counter (tallying) module pam_tally2 -u USER ← check
pam_tally2 -u USER --reset ← reset
14
cursuri-linux.ro
Group
15
cursuri-linux.ro
File Permissions
16
cursuri-linux.ro
Etc
17
cursuri-linux.ro
Network
18
cursuri-linux.ro
hosts.)
arp -d 192.168.xx.xx ← delete arp
nmcli line tool for controlling NetworkManager nmcli d #d=device
nmcli d show ← defail
nmcli c #c=con=connection
nmcli c down eno1
nmcli c up eno1
nmtui
tcpdump dump traffic on a network tcpdump -n port 80 -i any
tcpdump -n not arp and not port 123 and not port 22
tcpdump host 192.168.0.10 -n -w /tmp/20110615.pcap
tcpdump -r /tmp/20110615.pcap ← -r : Read packets from
file
tcpdump -D # NIC LIST
19
cursuri-linux.ro
20
cursuri-linux.ro
fping fping -g 192.168.0.0/24
21
cursuri-linux.ro
DNS
22
cursuri-linux.ro
Connection
23
cursuri-linux.ro
HTTP
24
cursuri-linux.ro
FTP
25
cursuri-linux.ro
SNMP
snmpwalk retrieve a subtree of management values using SNMP snmpwalk -v 2c -c public localhost sysname
GETNEXT requests snmpwalk -v 2c -c public localhost .1.3.6.1.2.1
snmpget communicates with a network entity using SNMP GET snmpget -v 2c 192.168.0.10 -c public
requests .1.3.6.1.4.1.2021.11.50.0
snmptranslate translate MIB OID names between numeric and textual forms snmptranslate -Tp | less
snmpnetstat display networking status and configuration information snmpnetstat -v 2c -c public -Can localhost
from a network entity via SNMP snmpnetstat -v 2c -c public -Ci localhost
snmpnetstat -v 2c -c public -Cs localhost
26
cursuri-linux.ro
Shell
date print or set the system date and time date --date '10day ago' +"%Y%m%d" → 20061030
date +"%Y%m%d" → 20061030
date +"%H:%M" → 12:47
cp -p FILE FILE.`date +%Y%m%d`
cp -p FILE FILE.`date -d '1day ago' +%Y%m%d`
tr translate or delete characters
cut remove sections from each line of files echo abcdef |cut -c 3-
echo abcdef |cut -c 2-4
sort sort lines of text files sort -t: +1 -n sample.txt
uniq report or omit repeated lines cat access_log.1 | awk {'print $4'} |awk -F: {'print $1$2$3'} |
sort |uniq -c
logger a shell command interface to the syslog(3) logger "test test"
system log module
tee read from standard input and write to xxxx.sh | tee xxxx.log
standard output and files xxxx.sh | tee -a xxxx.log
xxx.sh 2>&1 | tee xxxx.log ← Save standard output and standard
error output to file
basename strip directory and suffix from filenames SHELLNAME=`/usr/bin/basename $0`
SHELLNAME=`/usr/bin/basename $0 .sh`
dirname strip last component from file name DIR=`dirname ${0}`
paste merge lines of files paste -d, test1.txt test2.txt
awk echo "1 2 3 4 5" | awk '{ print $1 "," $3 }'
echo "1 2 : 3 4 : 5" | awk -F: '{ print $2 }'
sed sed -e 's/xxx/XXX/g' input.txt > output.txt
sed -i "s/IPADDR=192.168.0.10/IPADDR=192.168.0.11/g" ifcfg-eth0
27
cursuri-linux.ro
mail send and receive Internet mail
mailx echo test | mail -s "test" -S "smtp=smtp://xx.xx.xx.xx:25" [email protected]
cat test.txt | mail -s "test" -S "smtp=smtp://xx.xx.xx.xx:25" [email protected]
echo "`hostname` `date`" | mail -s "attach test" -a tmp.txt -S smtp=smtp://x.x.xx:25 -r [email protected]
[email protected]
while while : ; do uptime ; sleep 1 ; done
while : ; do uptime >> /tmp/tmp.txt ; sleep 1 ; done
while : ; do ps aux |grep httpd |wc -l ; sleep 1 ; done
for for i in 127.0.0.1 192.168.10.1; do ping -c 2 $i; done
sleep sleep 1
usleep sleep some number of microseconds usleep 1000000 ← 1,000,000 = 1sec
usleep 100000 ← 100,000 = 0.1sec
usleep 10000 ← 10,000 = 0.01sec
28
cursuri-linux.ro
Hardware
29
cursuri-linux.ro
Module
30
cursuri-linux.ro
HDD
31
cursuri-linux.ro
Partition
df report file system disk space usage df -h ← (-h : print sizes in human readable format)
df -BG ← Bigabyte Unite
df -BM ← Megabyte Unite
sfdisk partition table manipulator for Linux sfdisk -l ←(-l : List the partitions of a device.)
fdisk manipulate disk partition table fdisk -l ← ( -l : List the partition tables)
fdisk -l /dev/sdb
fdisk -l -o +UUID
gdisk Interactive GUID partition table (GPT) manipulator
parted a partition manipulation program parted -l ← check partitions
parted /dev/mapper/mpath0
lsblk list block devices lsblk
e2label Change the label on an ext2/ext3/ext4 filesystem
32
cursuri-linux.ro
Swap
33
cursuri-linux.ro
File Systems
34
cursuri-linux.ro
Data
35
cursuri-linux.ro
mount
36
cursuri-linux.ro
Performance
37
cursuri-linux.ro
Software
38
cursuri-linux.ro
X Window System
39