Linux shell commands 101

第 1 篇 介绍 Linux 系统配置命令,包括文件修改,用户添加、删除、修改,网络配置

1 文件

1.1 文件创建、显示内容、修改属主属性

dd if=/dev/zero bs=100k count=1 of=data.file
# touch modify files, create them if they not exist
touch file{1..100}.sh
# modify access & modification time
touch -a file.sh
touch -m file.sh
touch -d "jan 20 2017" file.sh
ln -s target_file symlink_name
find . -type l -print
echo test>>file.sh
file file.sh
file -b file.sh
cat file.sh
chmod 755 file.sh
chmod u=rwx g=rw o=r file.sh
chmod a+x file.sh
## Recursively change permissions to current directory
chmod 777 . -R
# chown user.group filename
chown zuoshi.kali file.sh
chown zuoshi.kali . -R
# make immutable file
chattr +i file.sh

1.2 Loopback filesystem vs. loopback file

dd if=/dev/zero of=loopbackfile.img bs=1G count=1
mkfs.ext4 loopbackfile.img
sudo mkdir /mnt/lookback
mount -o loop loopbackfile.img /mnt/loopback
# Or use follow steps to mount loopbackfile
losetup /dev/loop1 loopbackfile.img
mount /dev/loop1 /mnt/loopback
# 1st partition on disk img file, just like sda1 on sda
# offset is 32256 byte
losetup -o 32256 /dev/loop2 loopbackfile.img
umount /dev/loop1

1.3 Creating & Mounting ISO files as loopback

cat /dev/cdrom > linux.iso
# The preferred way
dd if=/dev/cdrom of=linux.iso
# Another way
mkisofs -V "Label" -o linux.iso source_dir/
# Hybrid ISO can be written to USB sd as a bootable USB device,
# So convernt bootable ISO file to Hybrid ISO
isohybrid linux.iso
# Write it to usb sd
dd if=linux.iso of=/dev/sdb1
# Or just use cat
cat linux.iso > /dev/sdb1
# Burning an iso file into CD ROM or DVD ROM
cdrecord -v dev=/dev/cdrom linux.iso
# mounting iso files
mkdir /mnt/iso
mount -o loop linux.iso /mnt/iso
# Flush into iso file
sync

1.4 Compression

zip

zip arch.zip file
zip -r arch.zip dire file
# Delete file from arch.zip
zip -d arch.zip file
# Update
zip arch.zip -u newfile
# List
unzip -l arch.zip
# Extract
unzip file.zip

gz, bz2, xz

gzip file
gunzip arch.gz
# List
gzip -l arch.gz
# Read from =stdio= and compress output file
cat file | gzip -c > arch.gz
# Dump extracted file without extracting gz
zcat arch.gz
# Compression ratio
gzip -9 file
# bzip & bzip2
bzip2 file
bunzip2 arch.bz2
bunzip2 arch.bz2 -k
cat file | bzip2 -c > arch.tar.bz2
# lzma
lzma file
unlzma file
cat file | lzma -c > file.lzma
# xz
xz -z file
xz -d arch.xz
xz -d arch.xz -k

squashfs

只读压缩文件,可以将 2-3G 的数据压缩到 700M 大小的文件,正如 Linux 的 Live CD 一样。配合 loopback 模式挂载压缩文件,因每次只解压少量文件,而能维持较高访问速度。

mksquashfs SOURCES comp.squashfs
mkdir -p /mnt/squash
mount -o loop comp.squashfs /mnt/squash
# Exclude
sudo mksquashfs /etc comp.squashfs -e /etc/passwd /etc/shadow
sudo mksquashfs /etc comp.squashfs -ef exclude.list

crypo tools

crypt PASSPHRASE input encrypted
gpg -c input
gpg file.gpg
base64 file > out_file
md5sum file
sha1sum file

sync & backup

rsync -az /home/zuoshi /home/backup
rsync -az source_path dest_path --exclude "*.txt"
rsync -az source_path dest_path --exclude-from exclude.list
# Delete non-exist files from destination path
rsync -az source_path dest_path --delete

1.5 Archiving

tar

# Create and compress after archiving
tar -cf output.tar file1 file2 folder1 ..
#-a --auto-compress with extension
tar -caf output.tar.xz file1 file2
tar -czf output.tar.gz file1 file2
tar -cjf output.tar.bz2 file1 file2
# --lzma xz
tar -cJf output.tar.xz file1 file2
# --total printing total bytes after archiving
# Exclude a set of file from archiving
tar -cf arch.tar * --exclude "*.txt"
tar -cf arch.tar * -X EXCLUED.LIST
# Exclude version control directory
tar --exclude-vcs -czvvf source_code.tar.gz DIRE
# Extract to
tar -xf archive.tar -C /path/to/extra_dir
tar -xJf output.tar.gz
# Extract and delete
tar -xzf output.tar.gz --remove-files
# Update
tar -rf output.tar new_file
# Updating only file1 has newer modification timestamp
tar -uf archive.tar file1
# Merge tarballs
tar -Af file1.tar file2.tar
# List = test
tar -tf output.tar
# Differ
tar -df archive.tar file1 file2
# Delete files in archive
tar -f archive.tar --delete file1 file2
tar --delete --file archive.tar file1 file2

cpio

文件夹和带有路径的文件, cpio 压缩解压时会遵守绝对路径, tar 则移除 / 转换成当前目录的相对路径。

# Archiving to output file
echo file1 file2 | cpio -o > arch.cpio
# List file in archive file
cpio -it < arch.cpio
# Extract from archive
cpio -id < arch.cpio

2 用户

2.1 添加、修改、删除

useradd -g users -s /bin/bash -m name
usermod -d /mnt/usb/home -m name
passwd name
userdel name

2.2 登录信息

who
w
users
uptime
last
lastb

3 系统

3.1 硬盘使用量

# Disk usage
du -sh
du -ch
du file
du -a directory
# Print size in bytes(default), kilobytes, megabytes, Block size(n bytes)
du -b file
du -k file
du -m file
du -B 4 file
# Exclude files
du --exclude "*.org" deft
du --exclude-from exclude.txt deft
du --max-depth 2 deft
# 10 largest size file
du -ak deft | sort -nrk 1 | head
# Disk free space
df -h

3.2 CPU 时间使用量

time COMMAND
time ls
time -o output.txt COMMAND
time -f "Time: %U" -a -o output.txt ls

3.3 系统资源使用量

ps
ps -fe | head
top
pgrep
lsof
kill -l
kill -s SIGNAL PID
kill -9 PID
killall PName
killall -9 emacs
killall -u USER PName
hostname
uname -n
uname -a
uname -r
uname -m
cat /proc/cpuinfo
cat /proc/meninfo
cat /proc/partitions
fdisk -l
lshw

3.4 计划任务

# Create a scheduling task
crontab -e
# 02 02 * * * /home/zuoshi/test.sh
crontab task.cron
crontab << EOF
http_proxy=http://192.168.0.3:3128
00 * * * * /home/zuoshi/download.sh
# Viewing the cron table
crontab -l -u zuoshi
# Removing the cron table
crontab -r

4 网络

4.1 ip

echo nameserver 8.8.8.8 >> /etc/resolv.conf
ip addr show
ip link status
ip addr add 192.168.0.10/24 broadcast 192.168.0.255 dev ens33
ip link set ens33 up
ip route add default via 192.168.0.1
ping -c2 github.com

4.2 ifconfig

ifconfig eth0 up
ifconfig eth0 192.168.0.1 netmask 255.255.255.0 broadcast 192.168.0.255
route add default gw 192.168.0.100

4.3 networkmanager

nmcli connection add con-name "static" ifname eth0 type ethernet autoconnect yes ip4 192.168.0.1 gw4 192.168.0.101
nmcli connection modify "static" ipv4.dns 8.8.4.4
nmcli connection modify "static" +ipv4.dns 8.8.8.8
nmcli connection modify "static" +ipv4.addresses 192.168.0.2/24
nmcli connection up static

4.4 write static ip

## /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# Static inet
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.100

4.5 network communication

lsof -i
netstat -antp