温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux-fuser命令

发布时间:2020-08-09 08:08:12 来源:ITPUB博客 阅读:234 作者:不一样的天空w 栏目:建站服务器
[root@localhost test ~]$ fuser
No process specification given
Usage: fuser [ -a | -s | -c ] [ -n SPACE ] [ -SIGNAL ] [ -kimuv ] NAME...
             [ - ] [ -n SPACE ] [ -SIGNAL ] [ -kimuv ] NAME...
       fuser -l
       fuser -V
Show which processes use the named files, sockets, or filesystems.

    -a        display unused files too
    -c        mounted FS
    -f        silently ignored (for POSIX compatibility)
    -i        ask before killing (ignored without -k)
    -k        kill processes accessing the named file
    -l        list available signal names
    -m        show all processes using the named filesystems
    -n SPACE  search in this name space (file, udp, or tcp)
    -s        silent operation
    -SIGNAL   send this signal instead of SIGKILL
    -u        display user IDs
    -v        verbose output
    -V        display version information
    -4        search IPv4 sockets only
    -6        search IPv6 sockets only
    -         reset options

  udp/tcp names: [local_port][,[rmt_host][,[rmt_port]]]

[oracle@acctdb01 ~]$  

fuser命令用于报告进程使用的文件和网络套接字。fuser命令列出了本地进程的进程号,那些本地进程使用file,参数指定的本地或远程文件。对于阻塞特别设备,此命令列出了使用该设备上任何文件的进程。
fuser:identify processes using files or sockets :可以显示出当前哪个程序在使用磁盘上的某个文件、挂载点、甚至网络端口,并给出程序进程的详细信息

常用参数及说明:更多详细信息可以参考man fuser页面

    -u:除了进程的PID之外,同时列出该进程的owner
    -v:列出更多的信息
    -m:后面接的文件名会主动上提到该文件系统的最顶层,对umount不成功很有帮助
    
例子:

1. 查询当前目录下的进程

[root@localhost test]# fuser -uv .  
 
                     USER        PID ACCESS COMMAND  
.:                   root       3784 ..c.. (root)bash  
                     root       3815 ..c.. (root)bash  
                     root       9998 ..c.. (root)bash  
                     root      28673 ..c.. (root)vi  

ACCES列说明:

    c:此程序在当前目录下
    e:当运行的时候可执行
    f:打开文件,默认状态下被忽略
    F:打开文件等待被写入,同f一样,默认状态下被忽略
    r:root directory,根目录
    m:可能为共享库    
    
2. 查询正在使用文件的进程信息

[root@rhel6164 ~]# fuser -uv 文件名  
[clef@rhel6164 ~]$ fuser -uv /home/clef/log.txt #用log.txt查看不到正在使用的进程  
[clef@rhel6164 ~]$ fuser -uv /home/clef/.log.txt.swp #用.log.txt.swp可以查看到正在使用的进程  
                     USER        PID ACCESS COMMAND  
/home/clef/.log.txt.swp:  
                     clef      32744 F.... (clef)vim  
                         
--要列出使用/etc/passwd文件的本地进程的进程号,请输入:
fuser /etc/passwd

--要列出使用/etc/filesystems文件的进程的进程号和用户登录名,请输入:
fuser -u /etc/filesystems   

--要终止使用给定文件系统的所有进程,请输入:
fuser -k -x -u -c /dev/hd1  或者  fuser -kxuc /home

--要列出正在使用已从给定文件系统删除的文件的全部进程,请输入:
fuser -d /usr文件                     
                         
3. 当umount不成功的时候,用fuser查询所占用文件系统的进程

[root@localhost test]# df -h /mnt/linux_share/ #/mnt/linux_share为mount的文件系统  
Filesystem            Size  Used Avail Use% Mounted on  
xx.xx.xx.xx:linux_share  
                      299G  156G  144G  52% /mnt/linux_share  
[root@localhost test]# umount /mnt/linux_share/ #当umount的时候提示设备正忙  
umount: /mnt/linux_share: device is busy  
umount: /mnt/linux_share: device is busy  
[root@localhost test]# fuser -muv /mnt/linux_share/ #通过fuser查看正在占用该文件系统的进程  
 
                     USER        PID ACCESS COMMAND  
/mnt/linux_share/:   root       3815 ..c.. (root)bash   
    
--运行下面命令杀掉占用此设备的进程
fuser -m -v -k/mnt/linux_share/
或者fuser -m -v -k -i  /mnt/linux_share/(每杀掉一下进程会让你确认)   

4. 查看那些程序使用tcp的80端口:
$fuser -n tcp 80
或$fuser -v -n tcp 80
或$fuser -v 80/tcp
这里,后两种方式显示的信息比较全,因为有了-v选项

# fuser -v 111/udp 111/tcp
                     用户     进程号 权限   命令
111/udp:             rpc         968 F.... rpcbind
111/tcp:             rpc         968 F.... rpcbind

如上例子中第4列的权限也成访问类型,如下:
c 代表当前目录                  F 打开的文件,用于写操作。默认不显示。
e 可执行的被运行                 m 映射文件或者共享库。
f 打开的文件。默认不显示。         r 根目录。                          
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI