Ubuntu Docker镜像缺少ip命令。

95

当我在Ubuntu的Docker容器中尝试执行IP命令时,我收到以下错误信息:

bash: ip: 命令未找到。

Ubuntu版本:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"

我应该安装哪个软件包来运行那个命令?

谢谢!

4个回答

160

你可以使用 apt-file search 命令来确定包含所需命令的软件包。在我的ubuntu16.04上,它告诉我需要安装 iproute2,我认为在1804版本中也是相似的。

1. 获取需要安装的软件包:

$ apt-file search --regexp 'bin/ip$'
iproute2: /bin/ip
iproute2: /sbin/ip

2. 安装该软件包:

$ apt install -y iproute2
...

3. 确认软件包已安装:

$ dpkg -l iproute2
ii  iproute2    4.3.0-1ubuntu3      amd64      networking and traffic control tools

15
要使用apt-file,首先需要安装并更新其缓存: $ apt-get install apt-file 然后: $ apt-file update - Alexandro de Oliveira
1
apt install -y iproute2中出现Unable to locate package iproute2。需要先运行apt-get update。请参阅此处:https://www.devmanuals.net/install/ubuntu/ubuntu-20-04-focal-fossa/installing-iproute2-on-ubuntu20-04.html。 - Chris
$ apt-get install apt-file 对我来说是缺失的关键部分。 - Peter Gloor

63

不要忘记安装iproute2

apt install iproute2

然后您可以使用 ip 命令。


1
你的意思是从容器内部吗?如果没有网络下载任何东西,我该怎么做呢? - Pavel Niedoba
1
这个过程假设您有互联网连接,如果没有连接我不知道该怎么做。 - prosti
在Ubuntu 18的主机和客户端上,我的主机有工作连接,iptables中已设置masquerade,已自动设置docker0桥接。但是,在容器中使用apt update不起作用,ip、ping、route和ifconfig命令均丢失,/etc/network/interfaces文件也不存在。 - Pavel Niedoba
1
我们可以执行 chroot rootfs_dir,然后安装 iproute2。@Pavel Niedoba - Mahler
4
这仅是一年前发布的现有答案的子集,没有提供任何新信息。 - Étienne

-3

希望它能有所帮助:

root@458517c8d93f:/# ip a
bash: ip: command not found
root@458517c8d93f:/# ifconfig
bash: ifconfig: command not found

root@458517c8d93f:/etc/systemd/network# apt-get install net-tools
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  net-tools
0 upgraded, 1 newly installed, 0 to remove and 7 not upgraded.
Need to get 194 kB of archives.
After this operation, 803 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 net-tools amd64 1.60+git20161116.90da8a0-1ubuntu1 [194 kB]
Fetched 194 kB in 3s (69.0 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package net-tools.
(Reading database ... 9962 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20161116.90da8a0-1ubuntu1_amd64.deb ...
Unpacking net-tools (1.60+git20161116.90da8a0-1ubuntu1) ...
Setting up net-tools (1.60+git20161116.90da8a0-1ubuntu1) ...
root@458517c8d93f:/etc/systemd/network# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.4  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe12:4  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:12:00:04  txqueuelen 0  (Ethernet)
        RX packets 21578  bytes 54216887 (54.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19156  bytes 1057383 (1.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536    
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 16  bytes 2100 (2.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 2100 (2.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

这仍然没有回答问题,下面的答案已经是正确的。你正在演示你可以安装 ifconfig 但不能安装 ip - Luke Briner

-7

一旦您使用最新版本的Ubuntu或任何版本的映像创建了Docker并创建了一个Ubuntu容器,您必须运行Ubuntu容器并安装以下基本网络命令:

apt-get update

apt-get upgrade

apt-get install -y net-tools

apt-get update

2
这不正确。ip 不是 net-tools 的一部分。 - Luke Briner
@LukeBriner 我希望下一条评论能够帮到你。 - Brijesh Sondarva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接