一些常用的命令

DNS

刷新 DNS 缓存

Windows

bash
1ipconfig /flushdns

macOS

在macOS操作系统中,刷新DNS缓存通常可以通过执行特定的命令行指令实现。该操作有助于解决域名解析问题,清除旧的或错误的DNS信息。需要注意的是,根据macOS的不同版本,刷新DNS缓存的指令可能有所不同。以下是一些常见版本及其对应的刷新方法:

  1. macOS Catalina, Mojave, High Sierra, Sierra, and macOS El Capitan:

    打开终端(Terminal)应用程序,输入以下命令并按回车:

    bash
    1sudo killall -HUP mDNSResponder

    输入管理员密码后,系统将刷新DNS缓存。

  2. macOS Yosemite:

    打开终端,输入以下命令并按回车:

    bash
    1sudo discoveryutil mdnsflushcache
  3. macOS Mavericks, Mountain Lion, and Lion:

    打开终端,输入以下命令并按回车:

    bash
    1sudo killall -HUP mDNSResponder
  4. macOS Snow Leopard:

    打开终端,输入以下命令并按回车:

    bash
    1sudo dscacheutil -flushcache

执行上述命令后,建议关闭所有的网络应用程序并重新启动,以确保更改生效。

需要强调的是,刷新DNS缓存可能会导致网络短暂中断,因此最好在执行这一操作前保存所有在线活动。

Docker

清理命令

  • 清空无用的 images docker image prune -a

docker build & docker run

Docker是一种流行的容器化技术,它允许开发人员打包应用程序及其所有依赖项,以便在任何地方轻松部署。在Docker中,docker builddocker run 是两个最常用的命令,下面是它们的介绍:

  1. docker build:这是用于构建Docker镜像的命令。通过Dockerfile文件中的指令来定义应用程序的环境,并将其打包为镜像。构建镜像的过程可以包括安装依赖项、拷贝文件、运行脚本等。例如,以下命令将在当前目录中查找名为Dockerfile的文件,并在本地构建一个名为“myapp”的镜像:

    docker build -t myapp .
  2. docker run:这是用于启动Docker容器的命令。容器是在Docker镜像的基础上创建的运行实例,可以在其中运行应用程序。通过该命令可以指定应用程序运行时的一些参数,如端口映射、环境变量、数据卷等。例如,以下命令将从名为“myapp”的镜像中启动一个新的容器,并将本地端口5000映射到容器内部的端口80:

    arduino
    1docker run -p 5000:80 myapp

通过这两个命令,开发人员可以快速构建和部署应用程序,从而简化了应用程序的开发和维护。

ENTRYPOINTCMD 的区别

ENTRYPOINTCMD 都是 Dockerfile 指令,用于指定 Docker 容器的默认执行行为。它们之间的主要区别在于它们的使用方式和目的。

  1. ENTRYPOINT:此指令用于指定容器的默认可执行程序。当容器启动时,ENTRYPOINT 指定的程序将被执行,后面可以跟随一些参数。它有两种形式:

    • exec 形式:ENTRYPOINT ["executable", "param1", "param2"]
    • shell 形式:ENTRYPOINT command param1 param2

    当使用 docker run 命令启动容器时,可以通过提供附加参数覆盖 ENTRYPOINT 指令中的参数。这些附加参数将追加到 ENTRYPOINT 指定的可执行程序后面。

  2. CMD:此指令用于提供默认的参数,这些参数将传递给 ENTRYPOINT 指令指定的可执行程序。CMD 也有两种形式:

    • exec 形式:CMD ["param1", "param2"]
    • shell 形式:CMD command param1 param2

    当使用 docker run 命令启动容器时,如果提供了附加参数,CMD 中的参数将被覆盖。

ENTRYPOINTCMD 的主要区别在于它们在容器启动时的行为。ENTRYPOINT 是容器的主要可执行程序,而 CMD 提供了默认参数。在 Dockerfile 中,您可以同时使用这两个指令。这样,如果您在运行容器时未提供任何参数,那么 ENTRYPOINTCMD 中的参数将一起执行。如果提供了参数,那么 CMD 中的参数将被覆盖。

例如,在 Dockerfile 中:

less
1FROM node:14
2WORKDIR /app
3COPY . .
4ENTRYPOINT ["node"]
5CMD ["index.js"]

当使用 docker run my-image 运行容器时,将执行 node index.js。但是,如果运行 docker run my-image app.js,则将执行 node app.js,覆盖了 CMD 中的参数。

网络

netstat

下面的命令仅测试过 linux
netstat -tunlp

  • -t (tcp) 仅显示tcp相关选项
  • -u (udp) 仅显示udp相关选项
  • -n 拒绝显示别名,能显示数字的全部转化为数字
  • -l 仅列出在Listen(监听)的服务状态
  • -p 显示建立相关链接的程序名

traceroute 简单用法

也是只在 linux 使用
traceroute [-n] -T -p [$Port] [$Host]

  • -n:直接使用IP地址而非主机名称(禁用DNS反查)。
  • -T:通过TCP探测。
  • -p:设置探测的端口号。
  • [$Port]:需要探测的端口号,比如80。
  • [$Host]:需要探测的目标服务器地址,比如 10.10.1.1

相关 Windows 下使用 tracecert

route 命令

  • Windows, route print
  • Linux, route -n

文件查看搜索

查看日志

  • 可以使用head(查看前几行)、tail(查看末尾几行)两个命令。
    查看/etc/profile的前10行内容,应该是: head -n 10 /etc/profile
    查看/etc/profile的最后5行内容,应该是: tail -n 5 /etc/profile
    注意:

    • tail -n 1000 显示最后1000行
    • tail -n +1000 从1000行开始显示,显示1000行以后的
    • head -n 1000 显示前面1000行
  • sed 命令
    sed -n '5,10p' filename 这样你就可以只查看文件的第5行到第10行。

Disk

显示磁盘空间信息 (df)

  • 使用 df -k 命令以千字节为单位显示磁盘空间信息。
    • -k 表示 kb
    • -m 表示 mb
    • -g 表示 gb
bash
1df -k
2Filesystem            kbytes    used   avail capacity  Mounted on
3/dev/dsk/c0t3d0s0     192807   40231  133296    24%    /
  • 其中每列的含义
    字段名说明
    kbytes文件系统中可用空间的总大小
    used已用空间
    avail可用空间
    capacity已用空间百分比
    mounted on挂载点

Understanding Linux File Permissions

example: drwxrwxrwx
第一位代表文件类型,有两个数值:“d”和“-”,“d”代表目录,“-”代表非目录。
后面9位可以拆分为3组来看,分别对应不同用户,2-4位代表所有者 user 的权限说明,5-7位代表组群 group 的权限说明,8-10位代表其他人 other 的权限说明。
r 代表可读权限,w 代表可写权限,x 代表可执行权限。
drwxrwxrwx 表示所有用户都对这个目录有可读可写可执行权限。

Permission Groups

  • u 代表所有者 (user)-The Owner permissions apply only the owner of the file or directory, they will not impact the actions of other users.
  • g 代表所有者所在的组和群 (group)-The Group permissions apply only to the group that has been assigned to the file or directory, they will not effect the actions of other users.
  • o 代表其他人但不是 u 和 g(other)
  • a 代表全部人 - The All Users permissions apply to all other users on the system, this is the permission group that you want to watch the most.

Permission Types

  • read – The Read permission refers to a user’s capability to read the contents of the file.
  • write – The Write permissions refer to a user’s capability to write or modify a file or directory.
  • execute – The Execute permission affects a user’s capability to execute a file or view the contents of a directory.

Advanced Permissions

  • _ – no special permissions
  • d – directory
  • l – The file or directory is a symbolic link
  • s – This indicated the setuid/setgid permissions. This is not set displayed in the special permission part of the permissions display, but is represented as a s in the read portion of the owner or group permissions.
  • t – This indicates the sticky bit permissions. This is not set displayed in the special permission part of the permissions display, but is represented as a t in the executable portion of the all users permissions

r,w,x 可以用数字表示 r=0x100 w=0x10 x=0x1

  • rw------- (600) 只有所有者才有读和写的权限
  • rw-r–r-- (644) 只有所有者才有读和写的权限,组群和其他人只有读的权限
  • rwx------ (700) 只有所有者才有读,写,执行的权限
  • rwxr-xr-x (755) 只有所有者才有读,写,执行的权限,组群和其他人只有读和执行的权限
  • rwx–x--x (711) 只有所有者才有读,写,执行的权限,组群和其他人只有执行的权限
  • rw-rw-rw- (666) 每个人都有读写的权限

Change Permissions

The potential Assignment Operators are + (plus) and – (minus); these are used to tell the system whether to add or remove the specific permissions.
use command chmod to modify permissions;

  • To make this modification you would invoke the command: chmod a-rw file1
  • To add the permissions above you would invoke the command: chmod a+rw file1
  • You use the chown command to change owner and group assignments, the syntax is simple chown owner:group filename, so to change the owner of file1 to user1 and the group to family you would enter chown user1:family file1.

Chown Recursively

The easiest way to use the chown recursive command is to execute “chown” with the “-R” option for recursive and specify the new owner and the folders that you want to change.

chown -R <owner> <folder_1> <folder_2> ... <folder_n>

For example, if you want to change the owner of directories and files contained in the home directory of a specific user, you would write

chown -R user /home/user

Linux中修改用户UID和组GID的方法

修改用户uid和组gid的命令分别是usermod和groupmod,思路很简单。先使用usermod修改用户的uid,然后使用groupmod修改组的gid,最后使用chown和chgrp命令修改原来用户文件和目录的属主属组。

1、修改foo用户的uid

usermod -u 2005 foo

2、修改foo组的gid

groupmod -g 3000 foo

3、foo用户的家目录下面的文件属主和属组会在1、2命令执行后自动修改成新的uid、gid对应的属主属组,但是其他文件目录需要手动修改。手动修改的命令也比较简单。

find / -user 2005 -exec chown -h foo {} \; find / -group 3000 -exec chgrp -h foo {} \;