跳至主要內容

不常用的命令

程序员李某某大约 11 分钟

不常用的命令

查看架构的方法

#### Linux
## 结果为 x86_64、i686是AMD
## 结果为 aarch64、arm64、armv7l是ARM
lscpu | grep Architecture
## 国产系统可能不叫 Architecture,而叫架构,可以直接用 lscpu查看
lscpu

#### Windows
echo %PROCESSOR_ARCHITECTURE%

防火墙开放端口

  • 老系统 -- iptables(未测试)
## 查看
sudu iptables -L
## 开发端口
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
## 保存
sudo iptables-save > /etc/iptables/rules.v4
  • 新系统 -- firewall-cmd(测试通过)
## 查看
sudo firewall-cmd --list-all
## 开放端口
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
## 加载
sudo firewall-cmd --reload
  • --zone=public表示开放到哪个网卡,
  • --add-port=80/tcp表示开放的端口,
  • --permanent表示永久生效, 重启后依赖有效

rpm 命令

  • -i--install 安装
  • -U--upgrade 更新包
  • -F--freshen 只升级已安装的
  • -e--erase 卸载
  • -q--query 查询一个包的信息
    • -a--all 查询所有安装的包
    • -l--list 查询一个包安装的所有文件
    • -p--package 查询未安装的包的信息
  • -V--verify 验证一个包的完整性
  • -h--hash 显示进度条
  • --nodeps 不检查依赖

解压

tar.xz

tar.xz使用

tar -xJvf filename.tar.xz -C temp_dir

这里:

  • -x:表示解压。
  • -J:表示使用 xz 解压缩。
  • -v:表示详细模式,会输出每个正在处理的文件名。
  • -f:表示指定要操作的文件。
  • -C:解压到某文件夹

tar.gz和tar.xz区别

.tar.gz.tar.xz 都是常见的归档和压缩文件格式,但它们使用不同的压缩算法。

.tar.gz

  • 归档.tar 文件格式用于将多个文件和目录打包成一个文件,不进行压缩。
  • 压缩.gzgzip 压缩格式,压缩效率较低但速度较快。
  • 解压命令tar -xzf filename.tar.gz

.tar.xz

  • 归档:同样是 .tar 文件格式,用于打包文件。
  • 压缩.xzxz 压缩格式,提供更高的压缩率,但解压速度较慢。
  • 解压命令tar -xJf filename.tar.xz

总结

  • .tar.gz 压缩速度较快,适合需要快速解压的场景。
  • .tar.xz 提供更高的压缩比,但解压可能更耗时。

zip

## 单文件压缩
zip archive.zip filename
## 多文件压缩
zip archive.zip file1 file2 file3
## 文件夹压缩
zip -r archive.zip foldername
## 压缩排除文件
zip -r archive.zip foldername -x "*.txt"
## 设置压缩级别,9最高
zip -r -9 archive.zip foldername
## 添加密码
zip -er archive.zip foldername
## 分卷压缩
zip -s 1G archive.zip foldername
## 覆盖
unzip -o archive.zip 

## 查看压缩内容
zipinfo archive.zip
unzip -l archive.zip

## 解压
unzip archive.zip
## 解压到指定文件夹
unzip -d foldername archive.zip

服务器直接发文件

scp

## 上传
scp -r /path/to/local/file username@remote_host:/path/to/remote/destination
## 下载
scp username@remote_host:/path/to/remote/file /path/to/local/destination
## 在服务器1,将服务器2的文件拷贝到服务器3
scp -r user1@remote1:/path/to/file user2@remote2:/path/to/destination
  • -r 表示递归上传文件夹
  • 也可以从另一个服务器拷到本地
  • 甚至可以在服务器1,将服务器2的文件拷贝到服务器3

sftp

sftp username@remote_host
## 进入后进行上传
sftp> put /path/to/local/file /path/to/remote/destination
## 从远程下载
sftp> get /path/to/remote/file /path/to/local/destination
## 退出
sftp> exit

ftp

服务器可能没有自带ftp服务,需要安装

## 安装ftp
apt install ftp
## 连接
ftp remote_host
ftp> login
ftp> put /path/to/local/file /path/to/remote/destination
ftp> get /path/to/remote/file /path/to/local/destination

rsync

支持增量传输和带宽限制,非常适合大文件和目录的传输。 服务器可能没有自带ftp服务,需要安装

## 安装rsync
apt install rsync
## 连接
rsync -avz /path/to/local/file remote_host:/path/to/remote/destination
rsync -avz remote_host:/path/to/remote/file /path/to/local/destination
rsync -avz --bwlimit=1000 /path/to/local/file remote_host:/path/to/remote/destination
rsync -avz -e ssh user1@remote1:/path/to/file user2@remote2:/path/to/destination

rsync参数的具体解释如下:

  • -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
  • -v, --verbose 详细模式输出
  • -z, --compress 对备份的文件在传输时进行压缩处理
  • --delete 删除那些DST中SRC没有的文件
  • --force 强制删除目录,即使不为空
  • -e 'ssh -p 30022' (这里用来指定ssh端口,默认22)
  • --exclude=workspace(目录下除workspace外的所有内容)
  • -v, --verbose 详细模式输出
  • -q, --quiet 精简输出模式
  • -c, --checksum 打开校验开关,强制对文件传输进行校验
  • -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
  • -r, --recursive 对子目录以递归模式处理
  • -R, --relative 使用相对路径信息
  • -b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。- 可以使用--suffix选项来指定不同的备份文件前缀。
  • --backup-dir 将备份文件(如~filename)存放在在目录下。
  • -suffix=SUFFIX 定义备份文件前缀
  • -u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更- 新的文件)
  • -l, --links 保留软链结
  • -L, --copy-links 想对待常规文件一样处理软链结
  • --copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
  • --safe-links 忽略指向SRC路径目录树以外的链结
  • -H, --hard-links 保留硬链结
  • -p, --perms 保持文件权限
  • -o, --owner 保持文件属主信息
  • -g, --group 保持文件属组信息
  • -D, --devices 保持设备文件信息
  • -t, --times 保持文件时间信息
  • -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
  • -n, --dry-run现实哪些文件将被传输
  • -W, --whole-file 拷贝文件,不进行增量检测
  • -x, --one-file-system 不要跨越文件系统边界
  • -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
  • -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
  • --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
  • -C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
  • --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
  • --delete-excluded 同样删除接收端那些被该选项指定排除的文件
  • --delete-after 传输结束以后再删除
  • --ignore-errors 及时出现IO错误也进行删除
  • --max-delete=NUM 最多删除NUM个文件
  • --partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
  • --force 强制删除目录,即使不为空
  • --timeout=TIME IP超时时间,单位为秒
  • -I, --ignore-times 不跳过那些有同样的时间和长度的文件
  • --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
  • --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
  • --compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
  • -P 等同于 --partial
  • --progress 显示备份过程
  • --exclude=PATTERN 指定排除不需要传输的文件模式
  • --include=PATTERN 指定不排除而需要传输的文件模式
  • --exclude-from=FILE 排除FILE中指定模式的文件
  • --include-from=FILE 不排除FILE指定模式匹配的文件
  • --config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
  • --port=PORT 指定其他的rsync服务端口
  • --stats 给出某些文件的传输状态
  • --password-file=FILE 从FILE中得到密码
  • --bwlimit=KBPS 限制I/O带宽,KBytes per second

磁盘分区

查看可用磁盘

## 查看磁盘
lsblk
  • sda一般就是装系统时的磁盘
  • 添加新磁盘后会多出sdb,最后一个字母是按顺序生成的
  • sr0是安装系统时的光盘
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0  100G  0 disk 
|-sda1   8:1    0   99G  0 part /
|-sda2   8:2    0    1K  0 part 
`-sda5   8:5    0  975M  0 part [SWAP]
sdb      8:16   0  100G  0 disk 
sr0     11:0    1 1024M  0 rom  

磁盘分区

新添加的磁盘都在/dev/sdb其中sdb根据实际情况定

## 开始分区
fdisk /dev/sdb
############ 进入分区工具 ############
Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)    主要 (0 个主要、0 个扩展、4 个免费)
   e   extended (container for logical partitions)   extended(逻辑分区的容器)
Select (default p):        直接回车
Partition number (1-4, default 1):    直接回车
First sector (2048-209715199, default 2048):  直接回车
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199): 直接回车

Created a new partition 1 of type 'Linux' and of size 100 GiB.

Command (m for help): w       写入
The partition table has been altered.   分区表已更改。
Calling ioctl() to re-read partition table.  调用 ioctl() 重新读取分区表。
Syncing disks.         同步磁盘

挂载

## 空盘需要格式化分区
mkfs -t xfs /dev/sdb1   ## xfs 可以换为其他,如 ext4
## 创建挂载文件夹
mkdir /mnt/llm
## 挂载
mount /dev/sdb1 /mnt/llm

执行mkfs -t xfs /dev/sdb1报错mkfs: failed to execute mkfs.xfs: No such file or directory

## 查看有没有这个命令
ls -l /sbin/mkfs*

##############################
-rwxr-xr-x 1 root root  14648 Mar 28 17:52 /sbin/mkfs
-rwxr-xr-x 1 root root  35136 Mar 28 17:52 /sbin/mkfs.bfs
-rwxr-xr-x 1 root root  43256 Mar 28 17:52 /sbin/mkfs.cramfs
-rwxr-xr-x 1 root root  51520 Nov  5  2023 /sbin/mkfs.exfat
lrwxrwxrwx 1 root root      6 Mar  5  2023 /sbin/mkfs.ext2 -> mke2fs
lrwxrwxrwx 1 root root      6 Mar  5  2023 /sbin/mkfs.ext3 -> mke2fs
lrwxrwxrwx 1 root root      6 Mar  5  2023 /sbin/mkfs.ext4 -> mke2fs
-rwxr-xr-x 1 root root  64272 Feb  8  2021 /sbin/mkfs.fat
-rwxr-xr-x 1 root root 112968 Mar 28 17:52 /sbin/mkfs.minix
lrwxrwxrwx 1 root root      8 Feb  8  2021 /sbin/mkfs.msdos -> mkfs.fat
lrwxrwxrwx 1 root root      6 Mar 23  2023 /sbin/mkfs.ntfs -> mkntfs
lrwxrwxrwx 1 root root      8 Feb  8  2021 /sbin/mkfs.vfat -> mkfs.fat
################################
## 没有安装一个就可以了
apt install xfsprogs  ## Debian、Ubuntu
yum install xfsprogs ## Centos

删除分区

## 开始分区
fdisk /dev/sdb
############ 进入分区工具 ############
Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x71b27e80

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 209715199 209713152  100G 83 Linux

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

################# 下面就可以重新创建分区了 #################

详细说明

Welcome to fdisk (util-linux 2.38.1).   # 欢迎使用fdisk
# 您决定写入之前的更改将仅保留在内存中,
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.  # 小心使用写入命令

Command (m for help): m       # m 命令获取帮助

Help:

  DOS (MBR)
   a   toggle a bootable flag    # 切换可引导标志
   b   edit nested BSD disklabel   # 辑嵌套的 BSD disklabel
   c   toggle the dos compatibility flag # 切换 DoS 兼容性标志

  Generic
   d   delete a partition     # 删除分区
   F   list free unpartitioned space  # 列出可用的未分区空间
   l   list known partition types   # 列出已知的分区类型
   n   add a new partition     # 添加新分区
   p   print the partition table   # 打印分区表
   t   change a partition type    # 改变分区类型
   v   verify the partition table   # 验证分区表
   i   print information about a partition # 打印有关分区的信息

  Misc
   m   print this menu      # 打印菜单
   u   change display/entry units   # 更改显示/输入单位
   x   extra functionality (experts only) # 额外功能(仅限专家)

  Script
   I   load disk layout from sfdisk script file  # 从 sfdisk 脚本文件加载磁盘布局
   O   dump disk layout to sfdisk script file  # 将磁盘布局转储到 sfdisk 脚本文件

  Save & Exit
   w   write table to disk and exit   # 写入磁盘并退出
   q   quit without saving changes   # 直接退出不保存

  Create a new label
   g   create a new empty GPT partition table   # 创建一个新的空 GPT 分区表
   G   create a new empty SGI (IRIX) partition table # 创建一个新的空 SGI (IRIX) 分区表
   o   create a new empty MBR (DOS) partition table  # 创建一个新的空 MBR (DOS) 分区表
   s   create a new empty Sun partition table   # 创建一个新的空 Sun 分区表

硬盘识别

开机状态下,新插入的硬盘不被马上识别,需要重启才能识别,若不想对服务器进行重启或者因为某些原因不能够重启服务器。下面介绍的就是无需重启服务器,使其识别新磁盘的方法。

## 查看主机总线号
ls /sys/class/scsi_host/  # host0 host1 host2

## 重新扫描SCSI总线添加设备
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host2/scan

注意

有几个总线,就扫描几个

再次查看磁盘

发现新添加的磁盘出现,即为成功

Linux与Win换行符不同的问题

install.sh:行2: $'\r':未找到命令
install.sh:行7: $'\r':未找到命令
install.sh:行14: 未预期的符号“$'do\r'”附近有语法错误
'nstall.sh:行14: `    do

这个错误通常是因为脚本文件中包含了Windows格式的换行符(\r\n)。你可以使用以下命令将文件转换为Unix格式:

sed -i 's/\r$//' install.sh

ssh

SSH密钥认证

## 在本地生成密钥
ssh-keygen
## 将公钥复制到远程服务器
ssh-copy-id user@remote_host

在远程服务器上执行命令

# ssh user@remote_host "command"
ssh user@remote_host "ls -l"
上次编辑于:
贡献者: 李元昊