How To Use the Linux Fuser

83 篇文章 1 订阅

How To Use the Linux Fuser Command

Tags: System Tools

What Is The fuser Command?

The fuser command is a very smart unix utility used to find which process is using a file, a directory or a socket. It also gives information about the user owning the process and the type of access. The fuser tool displays the process id(PID) of every process using the specified files or file systems.

How To Use The fuser Utility?

The man command can be used to see manual pages for any command, but the best way to learn something new, especially linux commands, is by going through real world examples and never stop typing commands in the terminal. Run the following command in your terminal to get information about the usage options of the fuser utility. We will be experimenting with the fuser utility on a Ubuntu 12.04 VPS. However, as long as you are running a linux distribution it should be okay.


fuser

root@exampleuser-X55CR:~# fuser 
No process specification given
Usage: fuser [-fMuv] [-a|-s] [-4|-6] [-c|-m|-n SPACE] [-k [-i] [-SIGNAL]] NAME...
       fuser -l
       fuser -V
Show which processes use the named files, sockets, or filesystems.

  -a,--all              display unused files too
  -i,--interactive      ask before killing (ignored without -k)
  -k,--kill             kill processes accessing the named file
  -l,--list-signals     list available signal names
  -m,--mount            show all processes using the named filesystems or block device
  -M,--ismountpoint     fulfill request only if NAME is a mount point
  -n,--namespace SPACE  search in this name space (file, udp, or tcp)
  -s,--silent           silent operation
  -SIGNAL               send this signal instead of SIGKILL
  -u,--user             display user IDs
  -v,--verbose          verbose output
  -w,--writeonly        kill only processes with write access
  -V,--version          display version information
  -4,--ipv4             search IPv4 sockets only
  -6,--ipv6             search IPv6 sockets only
  -                     reset options

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

How To View Processes Using A Directory

The fuser utility can be used with -v option, which runs the tool in verbose mode. The verbose option is used to produce verbose output on the computer screen so the user can see real-time status of what the utility is doing.

root@exampleuser-X55CR:~# fuser -v .
                     USER        PID ACCESS COMMAND
/root:               root       3378 ..c.. vim
                     root       3398 ..c.. bash
                     root       3449 ..c.. bash
                     root      19370 ..c.. bash
root@exampleuser-X55CR:~#

The above output shows that, when ran in verbose mode, the fuse utility gives information about the USER, PID, ACCESS and COMMAND. The ‘c’ character under ACCESS shows the type of access, it means ‘current directory’. There are many access types such as e(executable being run), r(root directory), f(open file. f is omitted in default display mode), F(open file for writing, F is omitted in default display mode) and m (mmap’ed file or shared library).

What process is using your tcp or udp socket?

There are times when you need to look up processes using TCP and UDP sockets. In order to look up these processes, the usage of -n option is needed. The -n option is used to select the corresponding name space.The following command creates a tcp listener on port 80.

root@exampleuser-X55CR:~# nc -l -p 80
Since a tcp server is listening on port 80, the fuser utility can be used to find the process which is using the server’s socket. The -v option is used to put the fuser utility in verbose mode and the -n option is used to select the tcp protocol as a name space.
root@exampleuser-X55CR:~# fuser -v -n tcp 80
root@exampleuser-X55CR:~# fuser -v -n tcp 80
                     USER        PID ACCESS COMMAND
80/tcp:              root       3846 F.... nc
root@exampleuser-X55CR:~#

By default, the fuser tool will look in both IPv6 and IPv4 sockets, but the default option can be changed with the -4 and -6 options. The -4 option stands for IPv4, the -6 one stands for IPv6. Note that fuser outputs only the PIDs to stdout, everything else is sent to stderr.

The results of the ‘fuser -v -n tcp 80’ command show that process id of the process using netcat is 3846 and the command which was used to launch it is ‘nc’. The process id (PID) can be used in many ways, one of them is process killing. When used with a PID, the kill command kills a process based on that process id. The fuser utility can also be used to kill processes accessing a specific file. In the following command, the -k option is used to kill the process which is using the tcp listener running on port 123. To make sure that the user does not kill a wrong process, the -i option is used which asks the user for confirmation before killing a process.

root@exampleuser-X55CR:~# fuser -k  123/tcp
123/tcp:             11543

Use the ‘fuser -k’ command with the -i option to ask the user for confirmation before killing a process. The user can answer with y for yes or N for not confirming the killing.

root@exampleuser-X55CR:~# fuser -i -k 123/tcp
123/tcp:             12216
Kill process 12216 ? (y/N)
Use The -6 Option To Look For IPv6 Sockets.

The following command uses fuser tool in verbose mode and tries to find IPv6 sockets running on port 123.

root@exampleuser-X55CR:~# fuser -v -n tcp -6 123
Since there is no IPv6 socket running on port 123, the command does not produce any output. The -6 option can be replaced with the -4 option in order to search for IPv4 sockets running on a specific port.

Find The Process Accessing A File System

The -m option can be used with the fuser command to find processes accessing files on the filesystem of a file. This option requires a filename as input argument. The -m option is very useful, especially when used to discover processes accessing a file system with the intetion of identifying which process to kill.

The following command displays all processes accessing filesystem on which ‘example.txt’ resides. See carefully how the -m option is used with the fuser utility.

root@exampleuser-X55CR:~# fuser -v -m example.txt 
                     USER        PID ACCESS COMMAND
/root/example.txt:   root     kernel mount /
                     root          1 Frce. init
                     root          2 .rc.. kthreadd
                     root          3 .rc.. ksoftirqd/0
                     root          6 .rc.. migration/0
                     root          7 .rc.. watchdog/0
                    [...]
                    exampleuser   23347 .r.e. gcalctool
                    exampleuser   24527 f..e. chrome
                    exampleuser   25388 f..e. chrome
                    exampleuser   25628 .r.e. evince
                    exampleuser   25634 .rce. evinced
                    exampleuser   25706 .rce. gm-notify
                    exampleuser   25769 .rce. at-spi-bus-laun
                    exampleuser   28191 .rce. mate-settings-d
                    exampleuser   28193 .rce. mate-screensave
                    exampleuser   29942 f..e. chrome
                    exampleuser   30044 .r.e. evince
                    exampleuser   32721 f..e. chrome

The fuser utility can also be used to send specific signals to a process. When used with the -k option, the fuser command sends the KILL signal to a process. There are many signals which can be sent to a specific running process; the -l option helps to find the list of signals that can be used with the fuser tool.

root@exampleuser-X55CR:~# fuser -l
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED

The above output shows all the possible signals that can be used with the fuser tool.

Conclusion

Of course this article is not enough to cover all options and practical examples of the fuser tool, but every example served in this article will help you in your way to being a Linux ninja.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
“unable to obtain lock on fuser 无进程” 是指在打印机的使用过程中出现的一个错误消息,通常意味着在使用打印机时发生了一些问题。 首先,这个错误消息中的 "fuser"(熨斗) 指的是打印机的一个部件,负责在打印过程中加热和熨平纸张,以确保打印结果的质量。 "unable to obtain lock"(无法获取锁定) 表示打印机无法锁定或控制该部件,而无进程(no process) 进一步说明没有正在运行的任务或程序来操作打印机。 出现这个错误消息可能有几个原因: 1. 可能是由于其他程序或任务正在使用或占用打印机资源,导致打印机无法获取熨斗上的锁。 2. 可能是由于打印机本身的故障或损坏,导致无法正常锁定熨斗。 针对这个问题,可以尝试以下解决方法: 1. 确保没有其他程序或任务正在使用打印机。检查并关闭其他正在运行的打印任务或程序,然后重新尝试打印。 2. 重启打印机。有时,重新启动打印机可以清除一些临时问题,并重新初始化打印机的硬件。 3. 检查打印机的连接。确保打印机正确连接到电脑或网络,并且连接线没有松动或损坏。 4. 如果以上方法都无效,可能需要联系打印机制造商的技术支持部门,寻求进一步的帮助和指导。 总之,"unable to obtain lock on fuser 无进程" 错误消息是打印机在使用过程中的一种错误提示,可能是由其他程序占用资源或打印机硬件故障导致的。通过检查和关闭其他程序、重启打印机或检查打印机连接,可以尝试解决这个问题。如无法解决,建议联系打印机制造商的技术支持寻求进一步的帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值