删除含有特殊字符文件
find -delete
ls -i
# note the inode number 12345
find . -xdev -inum 12345 -delete
If your find doesn't have a -delete option, call rm:
find . -xdev -inum 12345 -exec rm -- {} \;
rm
rm -i -- *
and answer “no” except for this particular file.
后台运行任务
When you run
$ nohup some-command &
there is a job running in the background until some-command exits. nohup only disconnects the command from the terminal; disown removes it from the jobs list. It seems to me your intended syntax is
$ nohup python test.py > a.out & disown
Note that the syntax used in the question has > a.out after the & that ends the command. This means that the output of test.py is not being sent to a.out — instead, it is being appended to nohup.out as the message indicates.
crontab -e
可以设置环境变量,例如:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 * * * * service shadowsocks-libev restart
Get back rc.local in systemd based Linux
# Create /etc/rc.local
vi /etc/rc.local
chmod +x /etc/rc.local
systemctl daemon-reload
systemctl start rc-local
Set system timezone 设置系统时区
timedatectl set-timezone UTC
或使用 "Etc/UTC"。
其它时区:
timedatectl set-timezone Asia/Tokyo # +09:00
timedatectl set-timezone Asia/Shanghai # +08:00
Other ways
echo "GMT+0000" > /etc/TZ
hwclock -s
Prevent a process from OOM kill
When a Linux system runs very low on memory, the kernel will begin killing processes to free ram. The mechanism responsible for this is called the OOM (Out Of Memory) Killer.
# dmesg | grep -i kill
[116393.964000] <invoker_app> invoked oom-killer: gfp_mask=0x2d2, order=0, oom_score_adj=0
[116394.032000] [<801b7084>] oom_kill_process+0xc8/0x3c8
[116394.524000] Out of memory: Kill process 2730 (miniportal) score 166 or sacrifice child
[116394.532000] Killed process 2730 (<killed_app>) total-vm:555612kB, anon-rss:9868kB, file-rss:20kB
OOM killer exclusion is done on a process by process basis by changing the corresponding kernel parameter associated with a particular process.
echo -1000 > /proc/$PID/oom_score_adj
# Or
pgrep -f process_name | while read PID; do echo -1000 > /proc/$PID/oom_score_adj; done
Make a file readonly
# make a file read-only so that nobody (even root) could modify or delete it.
chattr +i /path/to/file
# Undo
chattr -i /path/to/file
应用场景:锁定 DNS。
Linux 里设置 DNS 只需在 /etc/resolv.conf 里加一行 "nameserver 8.8.8.8"。但是很多发行版带的 NetworkManager 这个傻逼总是会自动重写 resolv.conf,强迫你去改那个傻逼 NM 的配置文件。这种情况只需要在手工修改完 /etc/resolv.conf 后将其设为只读即可完美解决: chattr +i /etc/resolv.conf (某些版本 Linux /etc/resolv.conf 默认是符号链接,这时用 chattr +i "$(realpath /etc/resolv.conf)" )
获取本机网卡IP / 网关IP / 公网IP等
# 获取某个网卡(iface) IP
ip addr | grep eth0 | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1
# 获取默认网关 IP
ip route show 0/0 | head -n1 | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
公网IP无法直接获得。只能通过第三方网站检测
curl ipinfo.io/ip
Upgrade Kernel
debian:
apt-cache search linux-image
apt-get install linux-image-4.X.X-X-amd64
"xxx" file is busy
查看哪些进程打开了目录或文件描述符
fuser -vmu /root
fuser path
- -m : name specifies a file on a mounted file system or a block device that is mounted. All processes accessing files on that file system are listed.
- -u : append username.
- -v : verbose.
lsof | grep '/path'
lsof +D dir_path
Monitor network traffic
apt-get install bmon
# 实时显示当前各网卡传输速率
bmon
apt-get install vnstat
vnstat -u -i eth0
# 自动统计每日/每月指定网卡的流量信息
vnstat
apt install nethogs
# 实时显示各进程网络流量速率
nethogs
Disk / Mount
/etc/fstab
UUID=dddd-dd-dd-dd / ext4 defaults 1 1
查看所有硬盘分区uuid:
blkid
Access to original contents of mount point
mkdir /mnt/root
mount --bind / /mnt/root
ls /mnt/root/home/foo/.ssh
As long as you use --bind (as opposed to --rbind), you get a clone of the mount without the stuff mounted on top of it.
df not showing correct free space after file removal
Deleting the filename doesn't actually delete the file. Some other process is holding the file open, causing it to not be deleted; restart or kill that process to release the file. Use the below command to find out which process is using a deleted (unlinked) file.
lsof +L1
多线程下载工具 axel
apt install axel
axel -a -n 10 http://example.com/a.iso
- -a: 显示动态 process bar
- -n: 线程数