운영체제/LINUX

[Linux] Linux Commands Cheat Sheet (Linux 명령어 정리)

benjykim 2021. 5. 17. 09:25
반응형

LINUX (RHEL/CENTOS) 명령어 정리

목차


시스템 명령어

시스템 명령어

명령어 설명 예시
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 hostname.
hostname NEWHOSTNAME ← 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

프로세스 관리

명령어 설명 예시
ps report a snapshot of the current processes ps aux
ps aux grep XXX awk '{print $2}'
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.
fg Resume jobspec in the foreground
bg Resume each suspended job jobspec in the background jobs -l ← List job
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 with "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

스케줄

명령어 설명 예시
crontab maintain crontab files crontab -l ← -l = list user's crontab
crontab -u USER -l
crontab -e ← -e = edit user's crontab
crontab -u USER -e
at queue jobs for later execution echo "/sbin/shutdown -h now"
at -t 200902302100
atq lists the user's pending jobs atq
atrm delete jobs for later execution atrm JOBID
watch execute a program periodically, showing output fullscreen watch ntpq -p ← By default, the program is run every 2 seconds
watch -n 1 ntpq -p ← 1 seconds interval
-d = highlight the differences between successive updates

시스템 ETC

명령어 설명 예시
ntpupdate set the date and time via NTP ntpupdate -b -u IP
-b = Force the time(step mode)
-u = If you are running ntpd, "-u" must be added
history GNU History Library history
history 5 ← lists only the last 5 lines
HISTSIZE=1000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
which locate a command which ls
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 ltrace -o test.txt wget http://example.com/
ltrace -p PID
ltrace -p 3365
script make typescript of terminal session script -afq $LOG

파일 및 디렉토리 명령어

파일 명령어

명령어 설명 예시
ls list directory contents ls -ltrh ← -r = reverse order while sorting
-h = with -l, print sizes in human readable format
ls -ltrh less
cp copy files and directories cp -p SRC DES
cp -pr SRC/ DES/ ← -r, -R = copy directories recursively
cp -f SRC DES ← -f, --force
mv move (rename) files mv file1 file2
mv dir1 dir2
mv file1 file2 DIR
rename rename multiple files rename 's/(.변경하고자 하는 text)/(.변경할 text)' (찾을 파일)
rename 's/.txt/.c/' *.txt ← *.txt 파일을 모두 *.c파일로 변경하기
rename -n 's/test/ttt/' *.txt ← -n = 변경될 파일 미리 출력 가능(검증용)
rename -v 's/test/ttt/' *.txt ← -v = 변경 및 변경된 파일 출력
rm remove files or directories rm -rf TARGET ← -r, -R = remove directories
touch change file timestamps touch file1
touch -d "2021/10/20 13:00:00" file1
ln make links between files ln -s SRC DES
unlink unlink DES
wc print newline, word, and byte counts for each file wc -l ← -l, --lines = print the line counts
tree list contents of directories in a tree-like format tree -Dpuga /etc
col filter reverse line feeds from input man ifconfig

디렉터리 명령어

명령어 설명 예시
pwd print name of current/working directory pwd
cd Change the current directory cd ~
cd ..
pushd Adds a directory to the top of the directory stack pushd /var/log
pushd `pwd`
popd Removes entries from the directory stack popd
dirs displays the list of currently remembered directories dirs -v
mkdir make directories mkdir -p /tmp/test1/test2/ ← make parent directories as needed
mkdir -m 700 /home/user01/.ssh
rmdir remove empty directories
If you want to delete directory, you must use “rm -r DIR” rmdir DIR

파일 내용 접근 명령어

명령어 설명 예시
more file perusal for crt viewing
less opposite of more crontab -l
view Start in read-only mode
cat concatenate files and print on the standard output cat /dev/null > access.log
tail output the last part of files tail -n 50 a.txt ← output the last N lines
tail -f /var/log/messages
tailf follow the growth of a log file head -n 100 a.txt ← -n, --lines
head output the first part of files
diff compare files line by line diff --suppress-common-lines --side-by-side File1 File2
diff -r dir1 dir2 ← When comparing directories, recursively compare
sdiff side-by-side merge of file differences sdiff -s File1 File2 ← -s = Do not print common lines
sdiff -s -w 200 File1 File2
colordiff
vimdiff vimdiff File1 File2
vim -d File1 File2

검색

명령어 설명 예시
grep print lines matching a pattern grep WORD FILE
grep -Ev "^# ^$" xxx.tx
grep . ifcfg-eth* ← check filename and contents
grep -r PATTERN --include="*.txt" DIRECTORY ← -r = recursive
egrep egrep is the same as grep -E egrep "aaa
find, xargs search for files in a directory hierarchy find . -name "txt"

유저 관리(Administration)

유저

명령어 설명 예시
useradd create a new user or update default new user information useradd -G wheel USER1
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
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 shadow-group file vipw ← edit /etc/passwd
vipw -s ← edit /etc/shadow
passwd change user password passwd -S USER01 ← check about the status of the password
passwd -l USER01 ← locc the user
passwd -u USER01 ← unlock
chpasswd update passwords in batch mode echo USER01:password
chage change user password expiry information chage -l USER ← check
chage -M 90 USER ← the password expires day set 90 days
usermod modify a user account usermod -g GROUP USER
usermod -l NEW_USERNAME OLD_USERNAME ← change username
usermod -u UID USER ← change UID
chsh change login shell chsh -s /bin/bash ← changing shell
getent get entries from Name Service Switch libraries getent passwd ← you can check LDAP users
pam_tally2 The login counter (tallying) module pam_tally2 -u USER ← check
pam_tally2 -u USER --reset ← reset

그룹

명령어 설명 예시
groups print the groups a user is in groups
groups USERNAME
groupadd create a new group groupadd -g GIP GROUP
groupadd -g 1100 dev
addgroup add group to the system addgroup [--gid ID] group
groupdel delete a group groupdel GROUP
groupmod change USER's GID groupmod -g GID GROUP
groupmod -g 1501 testgroup1
chgrp change the Group of the file chgrp -R GROUP FILE
vigr edit the password, group, shadow-password or shadow-group file

파일 권한

명령어 설명 예시
chmod change file mode bits chmod 777 TARGET
chmod u+s PROGRAM ← add SSUID(Set User ID)
chown change file owner and group chown USER FILE
chown USER:GROUP FILE

유저 ETC

명령어 설명 예시
finger user information lookup program finger USER01
su change user ID or become superuser su - ← change root user
sudo su - USER -s /bin/bash
sudo execute a command as another user sudo -u USER COMMAND
id print real and effective user and group IDs id USERNAME
last show listing of last logged in users last -5 ← last 5 logged in users
last USER
lastlog reports the most recent login of all users or of a given user lastlog
umask set file mode creation mask umask 022 ← default 666-022=644 (rw-r--r--)

네트워크

네트워크

명령어 설명 예시
ip show / manipulate routing, devices, policy routing and tunnels ip a(=addr) ← print ip address
ip r(=route) ← show IP routing
ss another utility to investigate sockets ss -lt ← list all listening TCP connections
ss -ua ← List all UDP connections
ss -ltp ← process name with listening TCP
ifconfig configure a network interface ifconfig ← check ip
ifconfig -a ← -a = display all interfaces
ifconfig eth0 up or d
ifdown take a network interface down ifdown eth0
ifup bring a network interface up ifup eth0
route show / manipulate the IP routing table route ← show the IP routing table
rout add-net 192.168.10.0 netmast 255.255.255.0 gw 10.51.0.1
ethtool Display or change ethernet card settings ethtool eth0
ethtool -s eth0 speed 100 duplex full autoneg off
mii-tool view, manipulate media-independent interface status mii-tool eth0
arp manipulate the system ARP cache arp -n
arp -an ← a = show the entries of the specified hosts
arp -d 192.168.xx.xx ← delete arp
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 -r /tmp/20210121.pcap ← -r = read packets from file

네트워크 연결 확인

명령어 설명 예시
ping send ICMP ECHO_REQUEST to network hosts
traceroute print the route packets trace to network host ping -c 5 -s 1500 192.168.0.1
tracepath traces path to a network host discovering MTU along this path traceroute -n 192.168.0.10
traceroute -T -p 80 192.168.0.10 ← -T = TCP
nmap Network exploration tool and security / port scanner nmap google.com ← check TCP
nmap -Pn -sT -p 22 xx.xx.xx.xx ← check firewall
nmap -p 443 www.google.com
nc
netcat Concatenate and redirect sockets nc 192.168.0.10 80 22 ← check TCP
nc -vz 192.168.0.10 1-1023 ← port scan
httping measure the latency and throughput of a webserver

DNS

명령어 설명 예시
dig DNS lookup utility dig [@global-server] [domain] [q-type]
dig @8.8.8.8 google.com any
nslookup query Internet name servers interactively nslookup -type=any google.com 8.8.8.8
host DNS lookup utility host [-t type] [server]
host -t mx gmail.com 8.8.8.8
whois client for the whois service whois google.com
nscd name service cache daemon nscd -i hosts ← clear cache

연결

명령어 설명 예시
telnet user interface to the TELNET protocol telnet IP PORT
ssh OpenSSH SSH client (remote login program) ssh USER@IP
ssh xx.xx.xx.xx sudo /sbin/reboot
scp secure copy (remote file copy program) scp -rp /tmp/test1/ user1@192.168.0.10:/tmp/test2/
rsync a fast, versatile, remote (and local) file-copying tool rsync -avz --delete /home/user1/ /tmp/user1.bk/ ← rsync “/” is very important.
rsync -e ssh -avz --delete /home/user1/ user2@192.168.0.2:/home/backup/server1/home/user1/
ssh-keygen authentication key generation, management and conversion ssh-keygen -t rsa ← generate RSA key pair
ssh-keygen -R HOST
← removes all keys belonging to hostname from a known_hosts file
ssh-copy-id use locally available keys to authorise logins on a remote machine ssh-copy-id USER@xx.xx.xx.xx

HTTP

명령어 설명 예시
curl transfer a URL curl -I http://www.example.com/ ← Only Header
curl -i http://www.example.com/ ← Header and Body
wget The non-interactive network downloader wget http://google.com/
wget -e http_proxy=xx.xx.xx.xx:8080 http://example.com/

하드웨어

하드웨어

명령어 설명 예시
dmesg print or control the kernel ring buffer dmesg
lsusb List USB devices lsusb
lspci list all PCI devices lspci
nproc print the number of processing units available nproc
inxi Display info about all hardware inxi -Fxz
hwinfo Display info about all hardware hwinfo --short
lshw Display info about all hardware lshw --short
lshw -C cpu ← display all CPU info
lshw -short -C memory
lshw -short -C disk
lshw -C network
lscpu Display all CPU info lscpu

모듈

명령어 설명 예시
lsmod show the status of modules in the Linux Kernel lsmod
modinfo show information about a Linux Kernel module modinfo MODULENAME
modinfo bnx2
insmod insert a module into the Linux Kernel
rmmod remove a module from the Linux Kernel
modprobe add and remove modules from the Linux Kernel

디스크 유틸(Utilities)

HDD

명령어 설명 예시
du estimate file space usage du -sh *
du -sh dir/
du -h --max-depth=1
fuser identify processes using files or sockets fuser -mv /mnt/test ← check
fuser -mvk /mnt/test ← -k = kill processes
chroot run command or interactive shell with special root directory
hdparm get/set hard disk parameters
dumpe2fs dump ext2/ext3/ext4 filesystem information
badblocks search a device for bad blocks

파티션

명령어 설명 예시
df report file system disk space usage df -h ← -h = print sizes in human readable format
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
gdisk Interactive GUID partition table (GPT) manipulator
parted a partition manipulation program parted -l ← check partitions
parted /dev/mapper/mapth0
lsblk list block devices lsblk
e2label Change the label on an ext2/ext3/ext4 filesystem

Swap

명령어 설명 예시
mkswap set up a Linux swap area
swapon enable devices and files for paging and swapping swapon -s ← check
swapon -a
swapon /dev/xvda3
swapoff disable devices and files for paging and swapping swappoff -a

파일 시스템

명령어 설명 예시
mkfs build a Linux filesystem
#you must umount the device before mkfs) mkfs -t xfs /dev/sdb1
mkfs -t ext4 /dev/sdb2
mkfs.xfs
mkfs.ext4
mkfs.ext3 #you must umount the device before mkfs. mkfs.ext4 /dev/sdb1
mkfs2fs create an ext2/ext3/ext4 filesystem
#you must umount the device before mkfs mke2fs /dev/sdb1 ← ext2
tune2fs adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems tune2fs -l /dev/mapper/mpath0 ← -l = list the contents of the filesystem superblock
tune2fs -i 0 -c 0 /dev/mapper/mpath0 ← -i = interval, -c = mount count
fsck check and repair a Linux filesystem you must umount the device before fsck. for example single usermode and umount. ('shutdown -r -F now' is force fsck after reboot.) fsck -p /dev/sda1 ← -p = automatically repair the file system
fsck.ext4 check and repair a Linux filesystem
e2fsck check a Linux ext2/ext3/ext4 file system
resize2fs ext2/ext3/ext4 file system resizer resize2fs /dev/testvg/lvol0

데이터

명령어 설명 예시
dd convert and copy a file dd if=/dev/zero of=test_10M bs=1M count=10
sync flush file system buffers
shred overwrite a file to hide its contents, and optionally delete it

마운트

명령어 설명 예시
mount mount a filesystem mount /mnt/test /dev/sda1
mount -o remount /dev/sda1
umount unmount file systems umount /mnt/test
umount -f /mnt/test ← -f = force unmount(in case of an unreachable NFS system)

성능

성능

명령어 설명 예시
top display Linux processes top -b -n 4 -d 5 ← interval 5 sec, 4 times
sar Collect, report, or save system activity information sar -f /var/log/sa/sa16
sar (cpu, io)
sar -r (memory)
vmstat Report virtual memory statistics vmstat 1 ← interval 1sec (cpu, io, memory, swap)
vmstast 1 5 ← interval 1 sec, 5 times
iostat Report Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions. iostat -xtk 1 (cpu, io) ← interval 1 sec
mpstat Report processors related statistics. mpstat -P ALL
w Show who is logged on and what they are doing
free Display amount of free and used memory in the system free -m ← show output in MB
netstat Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships netstat -anp ← a = show both listening and non-listening sockets
netstat -rn ← -r = display the kernel routing tables
iotop simple top-like I/O monitor iotop -b -n 4 -d 15 ← interval 15 sec, 4 times
dstat versatile tool for generating system resource statistics dstat -taf

반응형

'운영체제 > LINUX' 카테고리의 다른 글

[LINUX] run level(런레벨) 정리  (1) 2022.03.27
[Linux] Linux Basic (리눅스 기초)  (0) 2021.05.24
[Linux] Linux OS Booting Process 정리  (0) 2021.05.11
[Linux] Capabilities  (0) 2019.12.17
[Linux] 사용자(User) 네임스페이스  (0) 2019.12.16