{"id":3450,"date":"2017-07-29T14:02:41","date_gmt":"2017-07-29T06:02:41","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3450"},"modified":"2017-07-29T14:02:41","modified_gmt":"2017-07-29T06:02:41","slug":"linux%e6%95%85%e9%9a%9c%e6%8e%92%e9%99%a4%e6%89%8b%e5%86%8c%ef%bc%9astracehtoplsoftcpdumpiftopsysdig","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3450.html","title":{"rendered":"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5b66\u4e60\u4e00\u4e0b\u5404\u79cd\u5e38\u7528Linux\u7cfb\u7edf\u6545\u969c\u6392\u9664\u5de5\u5177\u7684\u4f7f\u7528\u3002<\/p>\n<h4 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>This Sysdig cheatsheet is a great guide of command-lines linux admins can use to get insights into their servers. Whether you\u2019ve been an admin for one month or 20 years you\u2019ve definitely used one if not all of these tools to troubleshoot an issue. Because we love Sysdig (naturally!) we also included a translation for each of these common operations into the sysdig command line or csysdig.<\/p>\n<p>Rather than attempt covering all options from manpages (which would have made for boring coverage of many esoteric, rarely-used switches), we\u2019ve started from examples referenced at the most popular web pages you\u2019d find when you search for terms like \u201cstrace examples\u201d, \u201chtop examples\u201d, and so forth.<\/p>\n<p>Do you have favorites that aren\u2019t listed here? Let us know and we\u2019ll include them in future articles.<\/p>\n<h2 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-strace\">strace<\/h2>\n<p>There\u2019s one subtle difference between strace and sysdig that will be apparent in many of these side-by-side comparisons: Many of the simplest strace examples include command-lines that are executed and traced as a \u201cone-shot\u201d operation. On the other hand, Sysdig has a somewhat different philosophy, in that it either watches live events from afar as they happen, or analyzes capture data previously saved to a file. Thankfully, Sysdig\u2019s rich filtering options provide the knobs to watch for specific one-shot executions, as you\u2019ll soon see.<\/p>\n<div class=\"table-responsive\">\n<div class=\"table-wrap\">\n<table class=\"wrapped confluenceTable tablesorter tablesorter-default stickyTableHeaders\" role=\"grid\">\n<colgroup>\n<col \/>\n<col \/>\n<col \/>\n<col \/><\/colgroup>\n<thead class=\"tableFloatingHeaderOriginal\">\n<tr class=\"tablesorter-headerRow\" role=\"row\">\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"0\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Operation: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Operation<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"1\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"strace: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">strace<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"2\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"sysdig: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">sysdig<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"3\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Note: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Note<\/div>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody aria-live=\"polite\" aria-relevant=\"all\">\n<tr role=\"row\">\n<td class=\"confluenceTd\">Trace the execution of a command<\/td>\n<td class=\"confluenceTd\"><code>strace who<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig proc.name=who<\/code><\/td>\n<td class=\"confluenceTd\">Whereas strace runs the who command shown here as a one-shot, Sysdig is watching for the execution of who. Use Sysdig\u2019s filtering to further isolate a specific run, e.g.:<\/p>\n<p><code>sysdig proc.name=who and proc.ppid=534<\/code><\/p>\n<p>This watches for a who that\u2019s about to be run in a shell that you\u2019ve determined to have PID of 534.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Trace only when certain\/specific system calls are made<\/td>\n<td class=\"confluenceTd\"><code>strace -e open who<\/code><\/p>\n<p><code>strace -e trace=open,read who<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig evt.type=open and proc.name=who<\/code><\/p>\n<p><code>sysdig \"evt.type in (open,read) and proc.name=who\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Save a trace to a file<\/td>\n<td class=\"confluenceTd\"><code>strace -o output.txt who<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -w output.scap proc.name=who<\/code><\/td>\n<td class=\"confluenceTd\">With strace, the file produced contains the same text you\u2019d have viewed on the screen if run interactively. With Sysdig, you get a raw, re-usable capture file, such that you can view the text output with:<\/p>\n<p><code>sysdig -r output.scap<\/code><\/p>\n<p>You could also use this as the basis to apply filters or any other Sysdig functionality you want to apply as you revisit the original events.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Watch a running process with PID=1363<\/td>\n<td class=\"confluenceTd\"><code>strace -p 1363<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig proc.pid=1363<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Print a timestamp for each output line of the trace<\/td>\n<td class=\"confluenceTd\"><code>strace -t who<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig proc.name=who<\/code><\/td>\n<td class=\"confluenceTd\">Sysdig prints timestamps by default.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Print relative time for system calls<\/td>\n<td class=\"confluenceTd\"><code>strace -r who<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -tD proc.name=who<\/code><\/td>\n<td class=\"confluenceTd\">Sysdig offers several more ways to represent timestamps via the -t option.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Generate batch statistics reports of system calls<\/td>\n<td class=\"confluenceTd\"><code>strace -c who<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -w output.scap proc.name=who<br \/>\n# Now run the \u201cwho\u201d separately<\/code>For one-shot batch text reports:<br \/>\n<code>sysdig -r output.scap -c topscalls -c topscalls_time<\/code><\/p>\n<p>Or for an interactive report that allows for further drill-down:<br \/>\n<code>csysdig -r output.scap -v syscalls<\/code><\/td>\n<td class=\"confluenceTd\">Sysdig\u2019s default behavior is more optimized for the case of presenting event data as it happens rather than \u201cbatch\u201d reporting. This is why the Sysdig equivalent is done in two steps here.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Generate live, per-second statistics reports of system calls for running process with PID=1363<\/td>\n<td class=\"confluenceTd\">N\/A<\/td>\n<td class=\"confluenceTd\"><code>csysdig -v syscalls proc.pid=1363<\/code><\/td>\n<td class=\"confluenceTd\">While strace can show individual events as they happen live, or provide a single batch report for the execution of a command, csysdig\u2019s views provide a unique ability to show live, periodic reports<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-htop\">htop<\/h3>\n<p>Since htop is a live, interactive, curses-style tool, we\u2019ll compare it to the live, interactive, curses-style csysdig.<\/p>\n<p>For starters, both tools use the same approach of navigating the live table via Up\/Down\/Left\/Right arrows and also PgUp\/PgDn. For operations that affect a single process (killing, renicing, etc.) it is assumed you\u2019ve used these controls to first highlight a particular process.<\/p>\n<div class=\"table-responsive\">\n<div class=\"table-wrap\">\n<table class=\"wrapped confluenceTable tablesorter tablesorter-default stickyTableHeaders\" role=\"grid\">\n<colgroup>\n<col \/>\n<col \/>\n<col \/>\n<col \/><\/colgroup>\n<thead class=\"tableFloatingHeaderOriginal\">\n<tr class=\"tablesorter-headerRow\" role=\"row\">\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"0\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Operation: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Operation<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"1\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"htop: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">htop<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"2\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"csysdig: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">csysdig<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"3\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Note: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Note<\/div>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody aria-live=\"polite\" aria-relevant=\"all\">\n<tr role=\"row\">\n<td class=\"confluenceTd\">Change sort order based on a column of the table<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>F6<\/code>,\u00a0<code>&lt;<\/code>, or\u00a0<code>&gt;<\/code>\u00a0and then select a column by name, or<br \/>\nPress\u00a0<code>M<\/code>,\u00a0<code>P<\/code>, or\u00a0<code>T<\/code>to sort by Memory, Processor Usage, or Time<br \/>\nPress\u00a0<code>I<\/code>\u00a0to invert the sort order<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>F9<\/code>\u00a0or\u00a0<code>&gt;<\/code>\u00a0and then select a column by name, or<\/p>\n<p>Press\u00a0<code>shift&lt;1-9&gt;<\/code>\u00a0to sort by any column\u00a0<code>n<\/code>, and press repeatedly to invert sort order, or<\/p>\n<p>Mouse-click on a column header<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Kill a process<\/td>\n<td class=\"confluenceTd\">Press<code>F9<\/code>\u00a0or\u00a0<code>k<\/code><\/td>\n<td class=\"confluenceTd\">Press<code>k<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Renice a process<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>F7<\/code>\u00a0or<code>\u00a0]<\/code>\u00a0to reduce the nice value by 1<br \/>\nPress\u00a0<code>F8<\/code>\u00a0or\u00a0<code>[<\/code>\u00a0to increase the nice value by 1<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>]<\/code>\u00a0to reduce the nice value by 1<br \/>\nPress\u00a0<code>[<\/code>\u00a0to increase the nice value by 1<\/td>\n<td class=\"confluenceTd\">This illustrates how easy it is to customize Sysdig. I noticed when first writing this article that csysdig was missing a couple minor features like this, so I used the opportunity to learn how easy it is to write\/modify Chisels, then put up my improvements as a\u00a0<a class=\"external-link\" href=\"https:\/\/github.com\/draios\/sysdig\/pull\/560\" rel=\"nofollow\">Pull Request<\/a>. You can do the same!<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Display only processes started by a user named &#8220;phil&#8221;<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>u<\/code>, then<br \/>\nSelect the user name\u00a0<code>phil<\/code>\u00a0from the list<\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>csysdig user.name=phil<\/code>Or mouse-click\u00a0<code>Filter:<\/code>\u00a0from within\u00a0<code>csysdig<\/code>\u00a0at the top of the default Processes view, then append\u00a0<code>and user.name=phil<\/code>\u00a0to the current filter text<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Change the output refresh interval to once every 5 seconds<\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>htop -d 50<\/code><\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>csysdig -d 5000<\/code><\/td>\n<td class=\"confluenceTd\">As you can see, htop works in units of tenths-of-a-second, while csysdig works in milliseconds.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Start a system call trace on a process<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>s<\/code>\u00a0to start an\u00a0<code>strace<\/code><\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>F6<\/code>\u00a0to start a\u00a0<code>sysdig<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List open files for a process<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>l<\/code>\u00a0to run a one-time\u00a0<code>lsof<\/code><\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>f<\/code>\u00a0to run a one-time lsof Or to see real-time, updating reports of files\/directories used by a process, drill down to a specific process by pressing\u00a0<code>Enter<\/code>, then press\u00a0<code>F2<\/code>\u00a0and select a View such as\u00a0<code>Files<\/code>,\u00a0<code>File Opens List<\/code>, or\u00a0<code>Directories<\/code>.<\/td>\n<td class=\"confluenceTd\">See the Note above for \u201cRenice a process\u201d about how the one-time\u00a0<code>lsof<\/code>\u00a0was recently added as an enhancement.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Follow a process, such that it remains highlighted even as its order in the list changes<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>F<\/code><\/td>\n<td class=\"confluenceTd\">Default behavior is to always follow the highlighted process<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-lsof\">lsof<\/h3>\n<div class=\"table-responsive\">\n<div class=\"table-wrap\">\n<table class=\"wrapped confluenceTable tablesorter tablesorter-default stickyTableHeaders\" role=\"grid\">\n<colgroup>\n<col \/>\n<col \/>\n<col \/>\n<col \/><\/colgroup>\n<thead class=\"tableFloatingHeaderOriginal\">\n<tr class=\"tablesorter-headerRow\" role=\"row\">\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"0\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Operation: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Operation<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"1\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"lsof: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">lsof<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"2\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"csysdig: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">csysdig<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"3\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Note: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Note<\/div>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody aria-live=\"polite\" aria-relevant=\"all\">\n<tr role=\"row\">\n<td class=\"confluenceTd\">List all open files belonging to all active processes<\/td>\n<td class=\"confluenceTd\"><code>lsof<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List processes that have opened the specific file \/var\/log\/syslog<\/td>\n<td class=\"confluenceTd\"><code>lsof \/var\/log\/syslog<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"fd.name=\/var\/log\/syslog\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List processes that have opened files under the directory \/var\/log<\/td>\n<td class=\"confluenceTd\"><code>lsof +d \/var\/log<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"fd.directory=\/var\/log\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List files opened by processes named \u201csshd\u201d<\/td>\n<td class=\"confluenceTd\"><code>lsof -c sshd<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"proc.name=sshd\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List files opened by a specific user named \u201cphil\u201d<\/td>\n<td class=\"confluenceTd\"><code>lsof -u phil<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"user.name=phil\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List files opened by everyone except for the user named \u201cphil\u201d<\/td>\n<td class=\"confluenceTd\"><code>lsof -u ^phil<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"user.name!=phil\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List all open files for a specific process with PID=1081<\/td>\n<td class=\"confluenceTd\"><code>lsof -p 1081<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"proc.pid=1081\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List all files opened by user &#8220;phil&#8221; or a process named &#8220;sshd&#8221; (OR logic)<\/td>\n<td class=\"confluenceTd\"><code>lsof -u phil -c sshd<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"'user.name=phil or proc.name=sshd'\"<\/code><\/td>\n<td class=\"confluenceTd\">Note the use of two layers of quotes with the Sysdig filter.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List all files opened by an &#8220;sshd&#8221; process for user &#8220;phil&#8221; (AND logic)<\/td>\n<td class=\"confluenceTd\"><code>lsof -u phil -c sshd -a<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"'user.name=phil and proc.name=sshd'\"<\/code><\/td>\n<td class=\"confluenceTd\">Note the use of two layers of quotes with the Sysdig filter.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Observe repeating reports of open files based on live activity<\/td>\n<td class=\"confluenceTd\">Enable repeat mode with one of:<br \/>\n<code>lsof -r<\/code><br \/>\n<code>lsof +r<\/code><\/td>\n<td class=\"confluenceTd\">Similar live data can be obtained with a live\/interactive csysdig view, launched like so:<br \/>\n<code>csysdig -v files<\/code><br \/>\n<code>csysdig -v file_opens<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List all network connections<\/td>\n<td class=\"confluenceTd\"><code>lsof -i<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"fd.type=ipv4\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List network connections in use by a specific process with PID=1014<\/td>\n<td class=\"confluenceTd\"><code>lsof -i -a -p 1014<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"'fd.type=ipv4 and proc.pid=1014'\"<\/code><\/td>\n<td class=\"confluenceTd\">Note the use of two layers of quotes with the Sysdig filter.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List processes that are listening on port 22<\/td>\n<td class=\"confluenceTd\"><code>lsof -i :22<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"'fd.port=22 and fd.is_server=true'\"<\/code><\/td>\n<td class=\"confluenceTd\">Note the use of two layers of quotes with the Sysdig filter.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">List all TCP or UDP connections<\/td>\n<td class=\"confluenceTd\"><code>lsof -i tcp<\/code><\/p>\n<p><code>lsof -i udp<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -c lsof \"fd.l4proto=tcp\"<\/code><\/p>\n<p><code>sysdig -c lsof \"fd.l4proto=udp\"<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-tcpdump\">tcpdump<\/h3>\n<p>tcpdump is focused entirely on network traffic, while network traffic is only a subset of what Sysdig covers. Many tcpdump use cases involve filtering, and tcpdump uses network-specific\u00a0<a class=\"external-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/Berkeley_Packet_Filter\" rel=\"nofollow\">BPF filters<\/a>, whereas Sysdig uses its own broader\u00a0<a class=\"external-link\" href=\"http:\/\/www.sysdig.org\/wiki\/sysdig-user-guide\/#output-formatting\" rel=\"nofollow\">Sysdig filtering<\/a>. The two approaches look similar in many ways, but you\u2019ll want to look at the docs for each side-by-side as you progress to more advanced filtering needs. Also, since in Linux<a class=\"external-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/Everything_is_a_file\" rel=\"nofollow\">everything is a file<\/a>, you\u2019ll notice the Sysdig filtering examples below all leverage a \u201cnetwork-connections-via-file-descriptors\u201d approach.<\/p>\n<div class=\"table-responsive\">\n<div class=\"table-wrap\">\n<table class=\"wrapped confluenceTable tablesorter tablesorter-default stickyTableHeaders\" role=\"grid\">\n<colgroup>\n<col \/>\n<col \/>\n<col \/>\n<col \/><\/colgroup>\n<thead class=\"tableFloatingHeaderOriginal\">\n<tr class=\"tablesorter-headerRow\" role=\"row\">\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"0\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Operation: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Operation<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"1\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"tcpdump: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">tcpdump<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"2\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"csysdig: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">csysdig<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"3\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Note: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Note<\/div>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody aria-live=\"polite\" aria-relevant=\"all\">\n<tr role=\"row\">\n<td class=\"confluenceTd\">Capture packets from a particular interface eth0 (192.168.10.119)<\/td>\n<td class=\"confluenceTd\"><code>tcpdump -i eth0<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig fd.ip=192.168.10.119<\/code><\/td>\n<td class=\"confluenceTd\">Sysdig does not currently have filtering based on named interfaces, but the equivalent via IP address is shown here.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Capture only 100 packets<\/td>\n<td class=\"confluenceTd\"><code>tcpdump -c 100<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -n 100 fd.type=ipv4<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Display captured packets in ASCII<\/td>\n<td class=\"confluenceTd\"><code>tcpdump -A<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -A fd.type=ipv4<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Display captured packets in HEX and ASCII<\/td>\n<td class=\"confluenceTd\"><code>tcpdump -XX<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -X fd.type=ipv4<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Capture packet data, writing it into into a file<\/td>\n<td class=\"confluenceTd\"><code>tcpdump -w saved.pcap<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -w saved.scap fd.type=ipv4<\/code><\/td>\n<td class=\"confluenceTd\">The Sysdig file format is capable of holding event data for much more than just network packets (e.g. system calls).<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Read back saved packet data from a file<\/td>\n<td class=\"confluenceTd\"><code>tcpdump -r saved.pcap<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig -r saved.scap<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Capture only packets longer\/smaller than 1024 bytes<\/td>\n<td class=\"confluenceTd\"><code>tcpdump greater 1024<\/code><\/p>\n<p><code>tcpdump less 1024<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig \"fd.type=ipv4 and evt.buflen &gt; 1024\"<\/code><\/p>\n<p><code>sysdig \"fd.type=ipv4 and evt.buflen &lt; 1024\"<\/code><\/td>\n<td class=\"confluenceTd\">The\u00a0<code>greater\/less<\/code>\u00a0options in tcpdump reference overall packet length whereas\u00a0<code>evt.buflen<\/code>\u00a0in Sysdig is relative to payload size.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Capture only UDP or TCP packets<\/td>\n<td class=\"confluenceTd\"><code>tcpdump udp<\/code><\/p>\n<p><code>tcpdump tcp<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig fd.l4proto=udp<\/code><\/p>\n<p><code>sysdig fd.l4proto=tcp<\/code><\/td>\n<td class=\"confluenceTd\">Note that we don\u2019t need to explicitly include\u00a0<code>fd.type=ipv4<\/code>since we\u2019re using other network-only filters here.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Capture only packets going to\/from a particular port<\/td>\n<td class=\"confluenceTd\"><code>tcpdump port 22<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig fd.port=22<\/code><\/td>\n<td class=\"confluenceTd\">Note that we don\u2019t need to explicitly include\u00a0<code>fd.type=ipv4<\/code>since we\u2019re using other network-only filters here.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Capture packets for a particular destination IP and port<\/td>\n<td class=\"confluenceTd\"><code>tcpdump dst 54.165.81.189 and port 6666<\/code><\/td>\n<td class=\"confluenceTd\"><code>sysdig fd.rip=54.165.81.189 and fd.port=6666<\/code><\/td>\n<td class=\"confluenceTd\">Note that we don\u2019t need to explicitly include\u00a0<code>fd.type=ipv4<\/code>since we\u2019re using other network-only filters here.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-iftop\">iftop<\/h3>\n<p>Since iftop is a live, interactive, curses-style tool, we\u2019ll compare it to the live, interactive, curses-style csysdig. Also, like tcpdump, iftop uses BPF filters. See the previous intro to the section on tcpdump for more detail about filtering differences.<\/p>\n<div class=\"table-responsive\">\n<div class=\"table-wrap\">\n<table class=\"wrapped confluenceTable tablesorter tablesorter-default stickyTableHeaders\" role=\"grid\">\n<colgroup>\n<col \/>\n<col \/>\n<col \/>\n<col \/><\/colgroup>\n<thead class=\"tableFloatingHeaderOriginal\">\n<tr class=\"tablesorter-headerRow\" role=\"row\">\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"0\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Operation: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Operation<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"1\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"iftop: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">iftop<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"2\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"csysdig: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">csysdig<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" role=\"columnheader\" scope=\"col\" data-column=\"3\" aria-disabled=\"false\" aria-sort=\"none\" aria-label=\"Note: No sort applied, activate to apply an ascending sort\">\n<div class=\"tablesorter-header-inner\">Note<\/div>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody aria-live=\"polite\" aria-relevant=\"all\">\n<tr role=\"row\">\n<td class=\"confluenceTd\">Display a table of current bandwidth usage between pairs of hosts<\/td>\n<td class=\"confluenceTd\"><code>iftop<\/code><\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>csysdig -v connections<\/code>Or press\u00a0<code>F2<\/code>\u00a0from within\u00a0<code>csysdig<\/code>\u00a0to change the View, then up-arrow to select\u00a0<code>Connections<\/code><\/td>\n<td class=\"confluenceTd\">By default iftop watches just the first interface it finds, whereas by default csysdig watches traffic across the entire host.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Turn on display of network ports<\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>iftop -P<\/code>Or press\u00a0<code>p<\/code>\u00a0from within\u00a0<code>iftop<\/code><\/td>\n<td class=\"confluenceTd\">Default behavior is to always display ports<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Observe traffic for just the eth0 interface (192.168.10.119)<\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>iftop -i eth0<\/code><\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>csysdig -v connections fd.ip=192.168.10.119<\/code>Or mouse-click on\u00a0<code>Filter:<\/code>\u00a0from within\u00a0<code>csysdig<\/code>, then append and\u00a0<code>fd.ip=192.168.10.119<\/code>\u00a0to the existing filter text<\/td>\n<td class=\"confluenceTd\">sysdig\/csysdig do not currently have filtering based on named interfaces, but the equivalent via IP address is shown here.<\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Resolve DNS names<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>n<\/code>\u00a0from within\u00a0<code>iftop<\/code>\u00a0to toggle resolution for all hosts shown<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>n<\/code>\u00a0from within\u00a0<code>csysdig<\/code>\u00a0to run\u00a0<code>nslookup<\/code>\u00a0on the currently-highlighted remote host<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Change sort order based on a column of the table<\/td>\n<td class=\"confluenceTd\">Press &lt; to sort by source Press &gt; to sort by destination<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>F9<\/code>\u00a0or\u00a0<code>&gt;<\/code>\u00a0and then select a column by name, or<\/p>\n<p>Press\u00a0<code>shift &lt;1-9&gt;<\/code>\u00a0to sort by any column\u00a0<code>n<\/code>, and press repeatedly to invert sort order, or<\/p>\n<p>Mouse-click on a column header<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Filter to show only traffic going to\/from IP address 54.84.222.1<\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>iftop -f \"host 54.84.222.1\"<\/code><\/td>\n<td class=\"confluenceTd\">Launch as:<br \/>\n<code>csysdig -v connections fd.ip=54.84.222.1<\/code>Or mouse-click on\u00a0<code>Filter:<\/code>\u00a0from within csysdig, then append\u00a0<code>and fd.ip=54.84.22.1<\/code>\u00a0to the existing filter text<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Pause the display<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>P<\/code><\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>p<\/code><\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr role=\"row\">\n<td class=\"confluenceTd\">Scroll the display<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>j<\/code>\u00a0to scroll up<br \/>\nPress\u00a0<code>k<\/code>\u00a0to scroll down<\/td>\n<td class=\"confluenceTd\">Press\u00a0<code>Up\/Down\/Left\/Right<\/code>\u00a0arrows or\u00a0<code>PgUp\/PgDn<\/code>\u00a0to scroll through the table<\/td>\n<td class=\"confluenceTd\">sysdig\/csysdig go well beyond scrolling through a single-table, since you can drill down into the Connections View to see data in other groupings such as per-container or per-thread.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>==<\/p>\n<h5 id=\"Linux\u6545\u969c\u6392\u9664\u624b\u518c\uff1astrace\/htop\/lsof\/tcpdump\/iftop\/sysdig-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a class=\"external-link\" href=\"https:\/\/sysdig.com\/blog\/linux-troubleshooting-cheatsheet\/\" rel=\"nofollow\">https:\/\/sysdig.com\/blog\/linux-troubleshooting-cheatsheet\/<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/www.brendangregg.com\/linuxperf.html\" rel=\"nofollow\">http:\/\/www.brendangregg.com\/linuxperf.html<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/www.brendangregg.com\/Perf\/linux_perf_tools_full.png\" rel=\"nofollow\">http:\/\/www.brendangregg.com\/Perf\/linux_perf_tools_full.png<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/www.brendangregg.com\/Perf\/linux_observability_tools.png\" rel=\"nofollow\">http:\/\/www.brendangregg.com\/Perf\/linux_observability_tools.png<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5b66\u4e60\u4e00\u4e0b\u5404\u79cd\u5e38\u7528Linux\u7cfb\u7edf\u6545\u969c\u6392\u9664\u5de5\u5177\u7684\u4f7f\u7528\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a This Sy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[868,870,332,867,871,869],"class_list":["post-3450","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-htop","tag-iftop","tag-lsof","tag-strace","tag-sysdig","tag-tcpdump"],"views":8282,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3450"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3450\/revisions"}],"predecessor-version":[{"id":3465,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3450\/revisions\/3465"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}