nsenter命令简介

介绍

nsenter 命令是一个可以在指定进程的命令空间下运行指定程序的命令。它位于 util-linux 包中。

nsenter 命令的语法:

 1nsenter [options] [program [arguments]]
 2
 3options:
 4-t, --target pid:指定被进入命名空间的目标进程的pid
 5-m, --mount[=file]:进入mount命令空间。如果指定了file,则进入file的命令空间
 6-u, --uts[=file]:进入uts命令空间。如果指定了file,则进入file的命令空间
 7-i, --ipc[=file]:进入ipc命令空间。如果指定了file,则进入file的命令空间
 8-n, --net[=file]:进入net命令空间。如果指定了file,则进入file的命令空间
 9-p, --pid[=file]:进入pid命令空间。如果指定了file,则进入file的命令空间
10-U, --user[=file]:进入user命令空间。如果指定了file,则进入file的命令空间
11-G, --setgid gid:设置运行程序的gid
12-S, --setuid uid:设置运行程序的uid
13-r, --root[=directory]:设置根目录
14-w, --wd[=directory]:设置工作目录
15
16如果没有给出program,则默认执行$SHELL

示例:

运行一个 nginx 容器,查看该容器的 pid:

1docker inspect -f {{.State.Pid}} nginx
254325
3# 进入net命令空间
4nsenter -n -t54325
5ip addr
6# 进入容器nsenter -m -u -i -n -p -t
7nsenter --target 54325 --mount --uts --ipc --net --pid
8root@164f44ff58d1:/bin#

在 Kubernetes 中,在得到容器 pid 之前还需获取容器的 ID,可以使用如下命令获取:

1kubectl get pod test -oyaml|grep containerID
2  - containerID: docker://cf0873782d587dbca6aa32f49605229da3748600a9926e85b36916141597ec85

或者更为精确地获取 containerID:

1kubectl get pod test -o template --template='{{range .status.containerStatuses}}{{.containerID}}{{end}}'
2docker://cf0873782d587dbca6aa32f49605229da3748600a9926e85b36916141597ec85

原理

namespace 是 Linux 中一些进程的属性的作用域,使用命名空间,可以隔离不同的进程。

Linux 在不断的添加命名空间,目前有:

  • mount:挂载命名空间,使进程有一个独立的挂载文件系统,始于 Linux 2.4.19
  • ipc:ipc 命名空间,使进程有一个独立的 ipc,包括消息队列,共享内存和信号量,始于 Linux 2.6.19
  • uts:uts 命名空间,使进程有一个独立的 hostname 和 domainname,始于 Linux 2.6.19
  • net:network 命令空间,使进程有一个独立的网络栈,始于 Linux 2.6.24
  • pid:pid 命名空间,使进程有一个独立的 pid 空间,始于 Linux 2.6.24
  • user:user 命名空间,是进程有一个独立的 user 空间,始于 Linux 2.6.23,结束于 Linux 3.8
  • cgroup:cgroup 命名空间,使进程有一个独立的 cgroup 控制组,始于 Linux 4.6

Linux 的每个进程都具有命名空间,可以在/proc/PID/ns 目录中看到命名空间的文件描述符。

 1pwd
 2/proc/1/ns
 3ll
 4total 0
 5lrwxrwxrwx 1 root root 0 Sep 23 19:53 ipc -> ipc:[4026531839]
 6lrwxrwxrwx 1 root root 0 Sep 23 19:53 mnt -> mnt:[4026531840]
 7lrwxrwxrwx 1 root root 0 Sep 23 19:53 net -> net:[4026531956]
 8lrwxrwxrwx 1 root root 0 Sep 23 19:53 pid -> pid:[4026531836]
 9lrwxrwxrwx 1 root root 0 Sep 23 19:53 user -> user:[4026531837]
10lrwxrwxrwx 1 root root 0 Sep 23 19:53 uts -> uts:[4026531838]

发布日期:2023-04-18 00:22 字数:222 用时 2分钟
tags:nsenter