{"id":7983,"date":"2021-02-21T10:52:12","date_gmt":"2021-02-21T07:52:12","guid":{"rendered":"https:\/\/kifarunix.com\/?p=7983"},"modified":"2024-03-19T19:30:33","modified_gmt":"2024-03-19T16:30:33","slug":"install-osquery-on-ubuntu","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-osquery-on-ubuntu\/","title":{"rendered":"Install Osquery on Ubuntu 20.04"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1329\" height=\"531\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/02\/osquery.png\" alt=\"Install Osquery on Ubuntu 20.04\" class=\"wp-image-7987\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/02\/osquery.png 1329w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/02\/osquery-768x307.png 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/02\/osquery-150x60.png 150w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/02\/osquery-300x120.png 300w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/02\/osquery-696x278.png 696w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/02\/osquery-1068x427.png 1068w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/02\/osquery-1051x420.png 1051w\" sizes=\"(max-width: 1329px) 100vw, 1329px\" \/><\/figure>\n\n\n\n<p>In this guide, we are going to learn how to install osquery on Ubuntu 20.04. <a aria-label=\"Osquery (opens in a new tab)\" href=\"https:\/\/osquery.io\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Osquery<\/a> is an opensource tool that queries an operating system as if it were a relational database. It leverage SQL-like queries to gather Operating System information for performance, security, compliance audit analysis. It runs on multiple platforms such as Linux, FreeBSD, MacOS, Windows systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Osquery on Ubuntu 20.04<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install Osquery APT Repository<\/h3>\n\n\n\n<p>The default Ubuntu repositories does not contain the osquery package. However, osquery publishes an&nbsp;apt repository for each stable release. To add osquery apt repository to Ubuntu 20.04, create the osquery source list;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"deb [arch=amd64] https:\/\/pkg.osquery.io\/deb deb main\" | sudo tee \/etc\/apt\/sources.list.d\/osquery.list<\/code><\/pre>\n\n\n\n<p>Import the repository signing keys<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B<\/code><\/pre>\n\n\n\n<p>Update your system packages<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt update<\/code><\/pre>\n\n\n\n<p>Once the update is done, install osquery;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt install osquery<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Components of osquery<\/h3>\n\n\n\n<p>Osquery package installs three basic components;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>osqueryctl<\/code><\/strong> &#8211; This is an osquery helper script for testing osquery configuration\/deployment as well as managing the osqueryd service.<\/li>\n\n\n\n<li><code><strong>osqueryd<\/strong><\/code> &#8211; is an osquery daemon for scheduling queries and recording the changes in the state of OS.<\/li>\n\n\n\n<li><strong><code>osqueryi<\/code><\/strong> &#8211; is an osquery interactive shell. From the shell, you can run various queries to explore that state of your OS.<\/li>\n<\/ul>\n\n\n\n<p>In order to learn the usage of the commands above, you can pass the -h\/&#8211;help option.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>osqueryctl -h<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Usage: \/usr\/bin\/osqueryctl {clean|config-check|start|stop|status|restart}<\/code><\/pre>\n\n\n\n<p>For example to start, stop and restart osqueryd using osqueryctl, run the commands;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>osqueryctl start osqueryd<\/code><\/pre>\n\n\n\n<pre id=\"block-1bd97d05-61ba-4e2f-9d1c-80d57fda865f\" class=\"wp-block-preformatted\">osqueryctl stop osqueryd<\/code><\/pre>\n\n\n\n<pre id=\"block-dcf24eef-4963-45c6-9b43-ac12d2fdbfa5\" class=\"wp-block-preformatted\">osqueryctl restart osqueryd<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Running Osquery<\/h3>\n\n\n\n<p>Osquery can be run in standalone mode using the osqueryi or it can be run as service using osqueryd. In this guide, we are going to focus on how to use the osquery interactive shell to query various system activities.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Running osquery in standalone mode<\/h4>\n\n\n\n<p>When <strong><code>osqueryi<\/code><\/strong> is run without any arguments, it takes you to the interactive shell prompt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><code>osqueryi<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Using a virtual database. Need help, type '.help'<br>osquery&gt;<\/code><\/pre>\n\n\n\n<p>You can obtain help by typing <strong><code>.help<\/code><\/strong> on the shell prompt.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>osquery&gt; .help<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nWelcome to the osquery shell. Please explore your OS!\nYou are connected to a transient 'in-memory' virtual database.\n\n.all [TABLE]     Select all from a table\n.bail ON|OFF     Stop after hitting an error\n.echo ON|OFF     Turn command echo on or off\n.exit            Exit this program\n.features        List osquery's features and their statuses\n.headers ON|OFF  Turn display of headers on or off\n.help            Show this message\n.mode MODE       Set output mode where MODE is one of:\n                   csv      Comma-separated values\n                   column   Left-aligned columns see .width\n                   line     One value per line\n                   list     Values delimited by .separator string\n                   pretty   Pretty printed SQL results (default)\n.nullvalue STR   Use STRING in place of NULL values\n.print STR...    Print literal STRING\n.quit            Exit this program\n.schema [TABLE]  Show the CREATE statements\n.separator STR   Change separator used by output mode\n.socket          Show the osquery extensions socket path\n.show            Show the current values for various settings\n.summary         Alias for the show meta command\n.tables [TABLE]  List names of tables\n.types [SQL]     Show result of getQueryColumns for the given query\n.width [NUM1]+   Set column widths for \"column\" mode\n.timer ON|OFF      Turn the CPU timer measurement on or off\nosquery&gt;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">System Information tables<\/h4>\n\n\n\n<p>Osquery converts various OS attributes into tabular like database concepts. Hence, to list tables from which various system information is stored, run the <code><strong>.tables<\/strong><\/code> command within the <strong>osqueryi<\/strong> prompt.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong><code>osqueryi<\/code><\/strong><\/code><\/pre>\n\n\n\n<p>osquery&gt; .tables<\/p>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n=&gt; acpi_tables\n=&gt; apt_sources\n=&gt; arp_cache\n=&gt; augeas\n=&gt; authorized_keys\n=&gt; block_devices\n=&gt; carbon_black_info\n=&gt; carves\n=&gt; chrome_extensions\n=&gt; cpu_time\n\u2026\n=&gt; time\n=&gt; uptime\n=&gt; usb_devices\n=&gt; user_events\n=&gt; user_groups\n=&gt; user_ssh_keys\n=&gt; users\n=&gt; yara\n=&gt; yara_events\n=&gt; yum_sources\nosquery&gt;\n<\/code><\/pre>\n\n\n\n<p>For example purposes, let us see what is contained on some of the tables;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>select * from os_version;<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n+--------+---------------------------+-------+-------+-------+-------+----------+---------------+----------+--------+\n| name   | version                   | major | minor | patch | build | platform | platform_like | codename | arch   |\n+--------+---------------------------+-------+-------+-------+-------+----------+---------------+----------+--------+\n| Ubuntu | 20.04.1 LTS (Focal Fossa) | 20    | 4     | 0     |       | ubuntu   | debian        | focal    | x86_64 |\n+--------+---------------------------+-------+-------+-------+-------+----------+---------------+----------+--------+\n<\/code><\/pre>\n\n\n\n<p>To query system users whose uid is greater than 1000,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>select * from users where uid &gt;=1000;<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n+-------+-------+------------+------------+-----------+-------------+-----------------+-------------------+------+\n| uid   | gid   | uid_signed | gid_signed | username  | description | directory       | shell             | uuid |\n+-------+-------+------------+------------+-----------+-------------+-----------------+-------------------+------+\n| 65534 | 65534 | 65534      | 65534      | nobody    | nobody      | \/nonexistent    | \/usr\/sbin\/nologin |      |\n| 1000  | 1000  | 1000       | 1000       | koromicha | koromicha   | \/home\/koromicha | \/bin\/bash         |      |\n| 65534 | 65534 | 65534      | 65534      | nobody    | nobody      | \/               | \/usr\/sbin\/nologin |      |\n+-------+-------+------------+------------+-----------+-------------+-----------------+-------------------+------+\n<\/code><\/pre>\n\n\n\n<p>To list all logged in users;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>select user,tty,host,time from logged_in_users where tty not like '~';<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>+-----------+-------+--------------+------------+\n| user      | tty   | host         | time       |\n+-----------+-------+--------------+------------+\n| koromicha | tty1  |              | 1613887707 |\n| koromicha | pts\/0 | 192.168.57.1 | 1613888358 |\n+-----------+-------+--------------+------------+<\/code><\/pre>\n\n\n\n<p>Check system uptime;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>select * from uptime;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>+------+-------+---------+---------+---------------+\n| days | hours | minutes | seconds | total_seconds |\n+------+-------+---------+---------+---------------+\n| 0    | 1     | 21      | 49      | 4909          |\n+------+-------+---------+---------+---------------+<\/code><\/pre>\n\n\n\n<p>To show network interfaces and IP addresses;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>select interface,address,mask from interface_addresses where interface NOT LIKE '%lo%';<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>+-----------+---------------------------------+-----------------------+\n| interface | address                         | mask                  |\n+-----------+---------------------------------+-----------------------+\n| enp0s3    | 10.0.2.15                       | 255.255.255.0         |\n| enp0s8    | 192.168.57.3                    | 255.255.255.0         |\n| enp0s3    | fe80::a00:27ff:fe5c:52a%enp0s3  | ffff:ffff:ffff:ffff:: |\n| enp0s8    | fe80::a00:27ff:fe7f:8415%enp0s8 | ffff:ffff:ffff:ffff:: |\n+-----------+---------------------------------+-----------------------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Osquery command output view modes<\/h4>\n\n\n\n<p>The osquery command output view mode can be changed by running the command, <code>.mode MODE<\/code> from within the <strong><code>osqueryi<\/code><\/strong> shell prompt, where MODE can be <strong><code>line<\/code><\/strong>, <code><strong>csv<\/strong><\/code>, <code><strong>pretty<\/strong><\/code> (default), <code><strong>column<\/strong><\/code>, <strong><code>list<\/code><\/strong>.<\/p>\n\n\n\n<p>For example to set the view to line mode;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>osquery&gt; <strong>.mode line<\/strong><\/code><\/pre>\n\n\n\n<p>The when you run the queries, output is produced line by line;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT * FROM system_info;<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n          hostname = ubuntu20\n              uuid = 269c209d-fc67-ec4f-bf56-c759a8296e14\n          cpu_type = x86_64\n       cpu_subtype = 142\n         cpu_brand = Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz\ncpu_physical_cores = 1\n cpu_logical_cores = 1\n     cpu_microcode = \n   physical_memory = 2084356096\n   hardware_vendor = innotek GmbH\n    hardware_model = VirtualBox\n  hardware_version = 1.2\n   hardware_serial = 0\n      board_vendor = Oracle Corporation\n       board_model = VirtualBox\n     board_version = 1.2\n      board_serial = 0\n     computer_name = ubuntu20\n    local_hostname = ubuntu20\n<\/code><\/pre>\n\n\n\n<p>List installed system packages;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>select * from deb_packages top limit 3;<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n      name = accountsservice\n   version = 0.6.55-0ubuntu12~20.04.4\n    source = \n      size = 452\n      arch = amd64\n  revision = 0ubuntu12~20.04.4\n    status = install ok installed\nmaintainer = Ubuntu Developers &lt;ubuntu-devel-discuss@lists.ubuntu.com&gt;\n   section = admin\n  priority = optional\n\n      name = adduser\n   version = 3.118ubuntu2\n    source = \n      size = 624\n      arch = all\n  revision = \n    status = install ok installed\nmaintainer = Ubuntu Core Developers &lt;ubuntu-devel-discuss@lists.ubuntu.com&gt;\n   section = admin\n  priority = important\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Exit Osquery Interactive shell<\/h4>\n\n\n\n<p>To exit osqueri interactive shell, osquery&gt;, use the command <code><strong>.exit<\/strong><\/code> or simply press <strong><code>Control+d<\/code><\/strong> keyboard combination keys.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>osquery&gt; <strong>.exit<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Running Osquery as a service<\/h4>\n\n\n\n<p><code><strong>osqueryd<\/strong><\/code> is an osquery daemon for scheduling queries and recording the changes in the state of OS. You can use this daemon to run Osquery a service.<\/p>\n\n\n\n<p>For this to work, you need to copy the sample Osquery configuration to <code><strong>\/etc\/osquery<\/strong><\/code> directory as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp \/usr\/share\/osquery\/osquery.example.conf \/etc\/osquery\/osquery.conf<\/code><\/pre>\n\n\n\n<p>Next, that the service;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl start osqueryd<\/code><\/pre>\n\n\n\n<p>Checking the status;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status osqueryd<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n\u25cf osqueryd.service - The osquery Daemon\n     Loaded: loaded (\/lib\/systemd\/system\/osqueryd.service; disabled; vendor preset: enabled)\n     Active: active (running) since Sun 2021-02-21 07:42:48 UTC; 18s ago\n    Process: 66618 ExecStartPre=\/bin\/sh -c if [ ! -f $FLAG_FILE ]; then touch $FLAG_FILE; fi (code=exited, status=0\/SUCCESS)\n    Process: 66633 ExecStartPre=\/bin\/sh -c if [ -f $LOCAL_PIDFILE ]; then mv $LOCAL_PIDFILE $PIDFILE; fi (code=exited, status=0\/SUCCESS)\n   Main PID: 66634 (osqueryd)\n      Tasks: 14 (limit: 2282)\n     Memory: 7.6M\n     CGroup: \/system.slice\/osqueryd.service\n             \u251c\u250066634 \/usr\/bin\/osqueryd --flagfile \/etc\/osquery\/osquery.flags --config_path \/etc\/osquery\/osquery.conf\n             \u2514\u250066637 \/usr\/bin\/osqueryd\n\nFeb 21 07:42:48 ubuntu20 systemd[1]: Starting The osquery Daemon...\nFeb 21 07:42:48 ubuntu20 systemd[1]: Started The osquery Daemon.\nFeb 21 07:42:48 ubuntu20 osqueryd[66634]: osqueryd started [version=4.6.0]\n<\/code><\/pre>\n\n\n\n<p>Well, that is it on how to install Osquery. You can continue to explore this awesome tool.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Further Reading<\/h3>\n\n\n\n<p><a aria-label=\"Osquery Documentation (opens in a new tab)\" href=\"https:\/\/osquery.readthedocs.io\/en\/4.6.0\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Osquery Documentation<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" class=\"rank-math-link\" href=\"https:\/\/kifarunix.com\/install-and-setup-kolide-fleet-on-ubuntu-18-04\/\" target=\"_blank\">Install and Setup Kolide Fleet on Ubuntu 18.04<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" class=\"rank-math-link\" href=\"https:\/\/kifarunix.com\/install-kolide-fleet-osquery-fleet-manager-on-debian-10\/\" target=\"_blank\">Install Kolide Fleet Osquery Fleet Manager on Debian 10<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-osquery-on-debian-10-buster\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Osquery on Debian 10 Buster<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to install osquery on Ubuntu 20.04. Osquery is an opensource tool that queries an operating system<\/p>\n","protected":false},"author":1,"featured_media":7987,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[72,121,1065,34],"tags":[3174,1066,3177,3176,1200,3175],"class_list":["post-7983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-monitoring","category-howtos","category-osquery","category-security","tag-install-osquery-ubuntu-20-04","tag-osquery","tag-osquery-install-ubuntu","tag-osquery-ubuntu-20-04","tag-ubuntu-20-04","tag-ubuntu-20-04-osquery","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7983"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=7983"}],"version-history":[{"count":4,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7983\/revisions"}],"predecessor-version":[{"id":21927,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7983\/revisions\/21927"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/7987"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=7983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=7983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=7983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}