Method of using Fuser command in Linux system

  • 2021-08-12 04:22:04
  • OfStack

What is the Fuser command?

The fuser command is a very clever unix utility for finding processes that are using a file, directory, or socket. It also provides information about the user who owns the process and the type of access. . The fuser tool displays the process ID (PID) for each process that uses the specified file or file system.

Installation

If your compact version runs fuser, you will be prompted with the following information:

-bash: fuser: command not found

Execute the following command to install:

[winbert@winbert-server ~]$ sudo yum -y install psmisc

How do I use the fuser command?

The man command can be used to view a help manual for any command, but the best way to learn new things (especially the linux command) is by reading real examples and constantly typing commands into the terminal. Run the following command on the terminal to get information about the use options for the fuser utility.


[winbert@winbert-server ~]$ fuser
No process specification given
Usage: fuser [-fMuvw] [-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 order can be associated with -v Option 1, which runs the tool in verbose mode. The verbose option is used to generate verbose output on the computer screen, so the user can see what the utility is doing in real time.


[winbert@winbert-server ~]$ fuser -v .
      USER  PID ACCESS COMMAND
/home/winbert:  winbert 1435 ..c.. bash

The above output shows that when running in verbose mode, fuser provides information about USER , PID , ACCESS And COMMAND Information of. ACCESS Under c Character indicates the access type and "current directory". There are many types of access, such as e (running executable file), r (root directory), f (open file. Omit f in default display mode), F (open file for writing, omit F in default display mode), and m (mmap file or shared library).

View processes using your tcp or udp socket?

Sometimes you need to use TCP and UDP sockets to find processes. To find these processes, you need to use the -n Option. -n Option is used to select the appropriate namespace.


[root@huidukongjian-h4 docker]# fuser -v -n tcp 80
      USER  PID ACCESS COMMAND
80/tcp:    root  27411 F.... docker-proxy

By default, fuser will look in both IPv6 and IPv4 sockets, but you can use the -4 And USER0 Option to change the default option. -4 Option represents IPv4 , USER0 Option represents IPv6 . Please note that, fuser Only PID is output to stdout, and everything else is sent to stderr.

fuser -v -n tcp 80 Command displays the result of using the docker The process ID of is 27411, and the command used to start the process is docker-proxy . The process ID (PID) can be used in a number of ways, one of which is process termination. When used with PID1, the kill command terminates the process according to the ID of the process. fuser It can also be used to terminate a process that accesses a specific file. In the following command, -k Option is used to terminate the port that is being used 123 Running on the tcp The process of the listener. To ensure that the user does not kill the wrong process, use the -i Option asks the user whether to confirm before terminating the process.

fuser -k 123/tcp

Use with the -i Option fuser -k Command requires confirmation from the user before terminating the process. Users can use y Answer "yes", or use N Answer does not kill the process.


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 in verbose mode and attempts to find an IPv6 socket running on port 123.

fuser -v -n tcp -6 123

Find a process that occupies a file system

-m Options are available with the fuser Command 1 to find processes accessing files on the file system. This option requires a file name as an input parameter. -m Option is useful, especially when used to discover the process accessing the file system and identify the process to kill.

The following command displays all processes accessing the file system where "example. txt" resides. Take a closer look -m Option is associated with fuser 1 use.


[root@huidukongjian-h4 docker]# fuser -v -m data/v2/config.json 
      USER  PID ACCESS COMMAND
/root/docker-v2/data/v2/config.json:
      root  kernel mount /
      root   1 .rce. systemd
      root   2 .rc.. kthreadd
      root   3 .rc.. rcu_gp
      root   4 .rc.. rcu_par_gp
      root   6 .rc.. kworker/0:0H-kbl
      root   8 .rc.. mm_percpu_wq
      root   9 .rc.. ksoftirqd/0
      root   10 .rc.. rcu_sched
      root   11 .rc.. migration/0
      root   12 .rc.. watchdog/0
      root   13 .rc.. cpuhp/0
      root   16 .rc.. netns
      root   17 .rc.. kauditd
      root   18 .rc.. khungtaskd
      root   19 .rc.. oom_reaper
      root   20 .rc.. writeback
      root   21 .rc.. kcompactd0
      root   22 .rc.. ksmd
      root   23 .rc.. khugepaged
      root   24 .rc.. crypto
      root   25 .rc.. kintegrityd
      root   26 .rc.. kblockd
      root   27 .rc.. tpm_dev_wq
      root   28 .rc.. md
      root   29 .rc.. edac-poller
      root   30 .rc.. watchdogd
      root   42 .rc.. kswapd0
      root   93 .rc.. kthrotld
      root   94 .rc.. acpi_thermal_pm
      root   95 .rc.. kmpath_rdacd
      root   96 .rc.. kaluad
      root   97 .rc.. ipv6_addrconf
      root   98 .rc.. kstrp
      root  326 .rc.. scsi_eh_0
      root  327 .rc.. scsi_tmf_0
      root  329 .rc.. kworker/0:1H-kbl
      root  361 .rc.. ata_sff
      root  363 .rc.. scsi_eh_1
      root  365 .rc.. scsi_tmf_1
      root  366 .rc.. scsi_eh_2
      root  367 .rc.. scsi_tmf_2
      root  387 .rc.. xfsalloc
      root  390 .rc.. xfs_mru_cache
      root  391 .rc.. xfs-buf/vda1
      root  394 .rc.. xfs-data/vda1
      root  395 .rc.. xfs-conv/vda1
      root  396 .rc.. xfs-cil/vda1
      root  397 .rc.. xfs-reclaim/vda
      root  398 .rc.. xfs-log/vda1
      root  399 .rc.. xfs-eofblocks/v
      root  400 .rc.. xfsaild/vda1
      root  486 .rce. systemd-journal
      rpc   541 .rce. rpcbind
      root  543 Frce. auditd
      root  545 .rce. sedispatch
      root  558 .rc.. rpciod
      root  559 .rc.. kworker/u3:0
      root  561 .rc.. xprtiod
      root  582 Frce. sssd
      polkitd  585 .rce. polkitd
      root  589 .rce. rngd
      dbus  593 frce. dbus-daemon
      chrony  612 .rce. chronyd
      root  652 Frce. sssd_be
      root  668 Frce. sssd_nss
      root  671 .rc.. ttm_swap
      root  672 .rc.. nfit
      root  675 frce. systemd-logind
      root  683 Frce. gssproxy
      root  740 frce. NetworkManager
      root  743 Frce. tuned
      root  814 frce. systemd-udevd
      root  889 frce. sshd
      root  890 Frce. rsyslogd
      root  895 frce. agetty
      root  898 frce. crond
      root  899 frce. agetty
      root  21821 .rc.. kworker/u2:0-flu
      root  25475 frce. sshd
      root  25480 .rce. systemd
      root  25485 frce. (sd-pam
      root  25491 frce. sshd
      root  25492 frce. bash
      root  25705 Frce. containerd
      root  25706 Frce. dockerd
      root  26375 .rc.. kworker/u2:1-eve
      root  27251 Fr.e. containerd-shim
      root  27267 F...m v2
      root  27273 Fr.e. containerd-shim
      root  27295 ....m sh
      root  27400 .rce. docker-proxy
      root  27411 .rce. docker-proxy
      root  27416 Fr.e. containerd-shim
      root  27432 ....m sh
      root  27478 ....m sh
      root  27479 F...m nginx
      root  27480 ....m sleep
      (unknown) 27481 F...m nginx
      root  27561 ....m sleep
      root  27705 .rc.. kworker/0:0-xfs-
      root  27765 .rc.. kworker/0:1-xfs-
      root  27836 .rc.. kworker/0:2-even
      root  27860 frce. sshd
      root  27883 frce. sshd
      sshd  27884 frce. sshd

fuser It can also be used to send specific instructions to a process. When and -k When option 1 is used, fuser Command will KILL Instruction is sent to the process. There are many instructions that can be sent to a running process, -l Option helps you find a list of instructions that can be used with fuser1.


[root@huidukongjian-h4 docker]# fuser -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT
CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS

Summarize


Related articles: