Linux Usage

linux 查看端口占用

   lsof -i:端口号

Linux 启动程序的时候报 Text file busy

lsof | grep exec

查看文件被占用的进程号,然后 kill 便可。

macOS 查看路由

netstat -nr -f inet

Add sudo permission to normal account

Create new file under /etc/sudoers.d/ directory, then add following text.

user ALL=(ALL) NOPASSWD: ALL

Note that user is the account name of the user.

kill process with pattern in bulk

kill $(ps aux | grep './process_pattern' | awk '{print $2}');

Install Mosh on Alma/CentOS/RHEL

yum install epel-release -y

yum install mosh -y
# or
dnf install mosh

See which package provides us the semanage command on CentOS/RHEL

yum whatprovides semanage
# or
dnf whatprovides semanage

Linux netstat see the top connections of process

netstat -anp  | awk '{print $7}' |sort | uniq -c | sort -k1 -nr

Linux netstat see the top connections of peers

netstat -nat|awk '{print$5}'|awk -F : '{print$1}'|sort|uniq -c|sort -rn

use openssl generate random bytes

# base64
openssl rand -base64 32
# or hex
openssl rand -hex 32

Fix terminal after displaying a binary file

# https://unix.stackexchange.com/questions/79684/fix-terminal-after-displaying-a-binary-file
alias reset='/usr/bin/reset; stty sane; tput rs1; clear; echo -e "\033c"'

查看服务日志

journalctl -u service-name -o cat -e # 查看日志
journalctl -u service-name -o cat -f # 实时日志

referer to: https://unix.stackexchange.com/questions/79684/fix-terminal-after-displaying-a-binary-file/79686#79686

k3s

k3s kubectl get ns

k3s kubectl -n <NAMESPACE> get pods

# k3s kubectl -n <NAMESPACE> exec  -ti <POD> bash
k3s kubectl exec -it <POD> -n <NAMESPACE> -- bash


k3s kubectl cp <NAMESPACE>/<POD>:/tmp/foo /tmp/bar

kubernetes

kubectl get ns

kubectl -n <NAMESPACE> get pods

kubectl exec -it <POD> -n <NAMESPACE> -- bash

kubectl exec -it <POD> -n <NAMESPACE> -- sh


# 注意:复制文件的时候,需要带完整路径,包括目的地址
kubectl cp -n  <NAMESPACE> <POD>:tmp/foo /tmp/bar

docker

docker exec -it ix-gitea-gitea-1 bash
docker exec -it ix-gitea-postgres-1 bash
docker volume ls
docker volume inspect <volume_name>
docker volume rm <volume_name>
docker volume prune # remove unused volume, be careful

Router OS docker 镜像构建与导入

  • Go 编译
env GOOS=linux GOARCH=arm64 go build
  • 镜像构建
docker buildx build --no-cache --platform linux/arm64 --output=type=docker -t tencent-dns-arm64:20250219 -f Dockerfile .
  • 导出,注意文件扩展名为 tar
docker save tencent-dns-arm64:20250219 > tencent-dns-arm64-20250219.tar

然后在 Router OS 里面导入对应镜像,创建 Container 即可。

ipv6 subnet 计算器

https://www.site24x7.com/tools/ipv6-subnetcalculator.html

openresty 构建

./configure    --prefix=/usr/local/openresty     --sbin-path=/usr/sbin/nginx     --modules-path=/usr/lib/nginx/modules   --conf-path=/etc/nginx/nginx.conf       --error-log-path=/var/log/nginx/error.log     --http-log-path=/var/log/nginx/access.log       --pid-path=/var/run/nginx.pid   --lock-path=/var/run/nginx.lock         --http-client-body-temp-path=/var/cache/nginx/client_temp     --http-proxy-temp-path=/var/cache/nginx/proxy_temp      --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp  --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp      --http-scgi-temp-path=/var/cache/nginx/scgi_temp      --user=nginx    --group=nginx   --with-compat   --with-threads  --with-http_addition_module     --with-http_auth_request_module         --with-http_dav_module --add-module=/home/jiya/workspace/nginx-dav-ext-module     --with-http_flv_module  --with-http_gunzip_module       --with-http_gzip_static_module  --with-http_mp4_module  --with-http_random_index_module         --with-http_realip_module       --with-http_secure_link_module        --with-http_slice_module        --with-http_ssl_module  --with-http_stub_status_module  --with-http_sub_module  --with-http_v2_module   --with-mail     --with-mail_ssl_module        --with-stream   --with-stream_realip_module     --with-stream_ssl_module        --with-stream_ssl_preread_module        --add-dynamic-module=/home/jiya/workspace/ngx_http_geoip2_module

端口映射

端口 备注      
28080 HTTP 端口,用于 cloudflare 代理转发