Centos8系统执行二进制文件时,报“bash: ./xxx: no such file or directory”解决

在执行某个二进制文件时报错误:bash: ./xxx: no such file or directory,但在该目录下确实存在该文件,且具有执行权限。有些linux系统(本次场景:CentOS 8.3Fedora 27)的发行版里没有安装redhat-lsb这个库,所以导致执行一些二进制命令时报没找到文件或目录。

本文以/sbin/ip命令为例

  1. file命令查看可执行文件
[root@localhost sbin]# file ip
ip: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically 	linked (uses shared libs), for GNU/Linux 2.6.32,BuildID[sha1]=e44a34a2e79a8f0f1a5a3dcf21b62699248db754, stripped

看出是64位LSB二进制可执行文件,没问题。

  1. ldd命令显示共享库的依赖情况
[root@localhost sbin]# ldd ip
	linux-vdso.so.1 =>  (0x00007ffc05f70000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f69b8a9c000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f69b86ce000)
	/lib64/ld-linux-x86-64.so.2 (0x0000557edc118000)

显示出依赖的so。

  1. readelf命令查看必须的依赖

用这个命令查看readelf -l ip | grep interpreter

[root@localhost sbin]# readelf -l ip

Elf file type is EXEC (Executable file)
Entry point 0x406f0d
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x00000000000559ac 0x00000000000559ac  R E    200000
  LOAD           0x0000000000056648 0x0000000000656648 0x0000000000656648
                 0x0000000000004ff8 0x000000000000ae90  RW     200000
  DYNAMIC        0x0000000000056df0 0x0000000000656df0 0x0000000000656df0
                 0x00000000000001e0 0x00000000000001e0  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x000000000004f368 0x000000000044f368 0x000000000044f368
                 0x0000000000000d7c 0x0000000000000d7c  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     10
  GNU_RELRO      0x0000000000056648 0x0000000000656648 0x0000000000656648
                 0x00000000000009b8 0x00000000000009b8  R      1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .init_array .fini_array .jcr .data.rel.ro .dynamic .got .got.plt .data .bss 
   04     .dynamic 
   05     .note.ABI-tag .note.gnu.build-id 
   06     .eh_frame_hdr 
   07     
   08     .init_array .fini_array .jcr .data.rel.ro .dynamic .got 
[root@localhost sbin]# readelf -l ip | grep interpreter
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
[root@localhost sbin]#

可以看ld-linux-x86-64.so.2这个依赖包是必须的。我find命令看看库里是否有该库文件:

[root@localhost sbin]# find / -name ld-linux-x86-64.so.2
/usr/lib64/ld-linux-x86-64.so.2
[root@localhost sbin]#

我这里只是测试例子,在本次真实场景里是不存在该依赖文件,所以要去安装相关库。

  1. redhat-lsb安装
[root@localhost sbin]# dnf install redhat-lsb

或者

[root@localhost sbin]# yum install redhat-lsb
原创 Doflamingo https://www.cnblogs.com/doflamingo
原文地址:https://www.cnblogs.com/doflamingo/p/14197969.html