<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ömer Gülen</title>
    <description>The latest articles on Forem by Ömer Gülen (@omergulen).</description>
    <link>https://forem.com/omergulen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F229624%2F981bfd43-591d-41b4-b782-82f688ad0506.jpg</url>
      <title>Forem: Ömer Gülen</title>
      <link>https://forem.com/omergulen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/omergulen"/>
    <language>en</language>
    <item>
      <title>URL shortener to voice for Clubhouse 👋</title>
      <dc:creator>Ömer Gülen</dc:creator>
      <pubDate>Wed, 10 Feb 2021 08:21:06 +0000</pubDate>
      <link>https://forem.com/omergulen/url-shortener-to-voice-for-clubhouse-4h2g</link>
      <guid>https://forem.com/omergulen/url-shortener-to-voice-for-clubhouse-4h2g</guid>
      <description>&lt;p&gt;&lt;a href="https://www.producthunt.com/posts/shorten-club?utm_source=badge-featured&amp;amp;utm_medium=badge&amp;amp;utm_souce=badge-shorten-club"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wYJjf_TX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://api.producthunt.com/widgets/embed-image/v1/featured.svg%3Fpost_id%3D284067%26theme%3Ddark" alt="👋 shorten.club - URL shortener to voice for Clubhouse 👋 | Product Hunt" width="250" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  URL shortener to voice for Clubhouse - &lt;a href="https://shorten.club"&gt;shorten.club&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Yes, yes, yes, and yes, I know it doesn't convert links into the voice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://shorten.club"&gt;shorten.club&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt; converts links or note buckets into a readable 6-digit number, but why?&lt;/p&gt;

&lt;h2&gt;
  
  
  Background - What is Clubhouse?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In short: Clubhouse is an audio-based social media app. The company describes itself as "a new type of social product based on a voice that allows people everywhere to talk, tell stories, develop ideas, deepen friendships, and meet interesting new people around the world."&lt;br&gt;
Basically, you can jump in and out of different chats, on different subjects, in something akin to a live, free-flowing podcast. You can simply listen or choose to throw in your thoughts. Imagine a cocktail party or, clubhouse.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Description is taken from &lt;a href="https://mashable.com/article/what-is-clubhouse-app/"&gt;Mashable&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What can't we do in Clubhouse?
&lt;/h2&gt;

&lt;p&gt;Now, you all know what you can do with Clubhouse, it's time to focus on what can't you do in Clubhouse. Clubhouse is a great platform to discuss ideas, contact your friends, organize brainstorming sessions or ask questions to known-people, yet it is very limited on other things such as resource sharing such as links, notes, buckets of ideas, movies, songs, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why can't I use other URL shorteners?
&lt;/h2&gt;

&lt;p&gt;Whether you are on desktop or mobile, URL shorteners works pretty well, but when it comes to sharing the shortened URL by voice/by spelling it, it can easily become a nightmare. Since Clubhouse is a voice-driven platform, you would want your shortened URL to be pronounced easily which is not possible with a mix of uppercase/lowercase letters and digits. On top of it, Clubhouse is an international platform, there are people from everywhere which means different languages and accents, etc. Even if people speak the same language, it is hard to spell a shortened URL one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  How did you solve it?
&lt;/h2&gt;

&lt;p&gt;As you may know every bank on the planet sends 6-8 digit numbers as SMS when it needs your authentication or 2FA works the same way, or quiz platform Kahoot.it gives 8-digit numbers to join a session. These all inspired us to make a URL shortener that converts buckets/lists into 6-digit numbers. In that way, you can easily gather the PIN and access the resources with it.&lt;/p&gt;

&lt;p&gt;Currently, you can create buckets of links or notes. You can add, update, and delete those resources even after you share them with your audience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A bucket of movies to watch: &lt;a href="https://shorten.club/665292"&gt;https://shorten.club/665292&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;A bucket of resources for Saas Startups: &lt;a href="https://shorten.club/191626"&gt;https://shorten.club/191626&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We would love to hear your feedback. Let us know what you think in the comments below!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lgax4Fwh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l94n0a9tqt7oi91h80os.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lgax4Fwh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l94n0a9tqt7oi91h80os.gif" alt="Alt Text" width="512" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>clubhouse</category>
      <category>javascript</category>
      <category>gatsby</category>
      <category>go</category>
    </item>
    <item>
      <title>How to reset Jenkins Admin user credentials</title>
      <dc:creator>Ömer Gülen</dc:creator>
      <pubDate>Sat, 11 Jul 2020 13:00:34 +0000</pubDate>
      <link>https://forem.com/omergulen/how-to-reset-jenkins-admin-user-credentials-3n6f</link>
      <guid>https://forem.com/omergulen/how-to-reset-jenkins-admin-user-credentials-3n6f</guid>
      <description>&lt;p&gt;Several months ago I've created a development environment for my side project &lt;a href="https://iytemenu.me"&gt;iytemenu&lt;/a&gt;. Yet, after a little development interval, I became too busy to develop the project and took a break. Then, this week my finals are done and I kinda have a free time to spend for my side projects, but when I came back I faced that I've lost my Jenkins credentials. Resetting the whole Jenkins logs/DB was too costly so I've researched and tried to find a better way. After some digging, I've found a way and I wanted to share with the community while I have the time to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's get started
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;NOTE: I think some versions have the Jenkins name as first letter capital. So use &lt;code&gt;Jenkins&lt;/code&gt; instead of &lt;code&gt;jenkins&lt;/code&gt; if you can't find the lower case one. Apply this in the following steps of the tutorial.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first step is making an SSH connection into your host.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh username@host
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Then, we need to edit the Jenkins config file. It is placed under this path:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/var/lib/jenkins/config.xml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To edit this config file I prefer using &lt;code&gt;vi&lt;/code&gt; because other editors may not be installed into the host.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vi /var/lib/jenkins/config.xml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Once you've successfully opened the file in your favorite editor, we need to find &lt;code&gt;&amp;lt;useSecurity&amp;gt;&lt;/code&gt; tag and set it's value to &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4-VPoNnk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1pc372zdxf4y3awqe5mx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4-VPoNnk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1pc372zdxf4y3awqe5mx.png" alt="useSecurity-1" width="875" height="739"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After setting the value, you can use save and exit from the editor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DTrEHAPs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f80wmyl4d2nxcz5yqez1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DTrEHAPs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f80wmyl4d2nxcz5yqez1.png" alt="useSecurity-2" width="748" height="652"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, we need to restart the Jenkins service.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl restart jenkins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PS: If you are not &lt;code&gt;root&lt;/code&gt; you may need &lt;code&gt;sudo&lt;/code&gt; for this operation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After the restart, you can check the status of the service by:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl status jenkins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now, you can go to the Jenkins dashboard. You will not need any credentials, and the &lt;code&gt;logged in user&lt;/code&gt; section will be blank since you are now an Anonymous user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h1Alv0mg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7gj00ahmy0s7d05dlv8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h1Alv0mg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7gj00ahmy0s7d05dlv8y.png" alt="Alt Text" width="704" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Then, navigate to &lt;code&gt;People&lt;/code&gt; on the left panel and remove old users by following the steps in the screenshots.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H1Yui7o2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4t8a9yd8633uvpmazj4a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H1Yui7o2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4t8a9yd8633uvpmazj4a.png" alt="Alt Text" width="880" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EF4Y3Vjs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1fv2nnuo15nm44krixwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EF4Y3Vjs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1fv2nnuo15nm44krixwg.png" alt="Alt Text" width="591" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gD2E-yuy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7pq25evz0bl178k040cc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gD2E-yuy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7pq25evz0bl178k040cc.png" alt="Alt Text" width="714" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PS: Some versions don't remove the old users totally, instead it shows an anonymous user (you) with other users. This should be enough to continue to the next steps if it doesn't start from the beginning and remove all users.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After removing the old users, navigate to &lt;code&gt;Manage Jenkins&lt;/code&gt; page from the left panel, and go to the &lt;code&gt;Configure Global Security&lt;/code&gt; section by clicking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ggxPgyBq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/anq8xv2t5yzqn76ntuxc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ggxPgyBq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/anq8xv2t5yzqn76ntuxc.png" alt="Alt Text" width="359" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_ZJJwPIE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l4udwybnjnztsdygzejg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_ZJJwPIE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l4udwybnjnztsdygzejg.png" alt="Alt Text" width="880" height="611"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Tick &lt;code&gt;enable security&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;Security Realm&lt;/code&gt; section, you will;&lt;br&gt;
-- Select &lt;code&gt;Jenkins' own user database&lt;/code&gt;&lt;br&gt;
-- Untick the &lt;code&gt;Allow users to sign up&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;Authorization&lt;/code&gt; section, you will;&lt;br&gt;
-- Select &lt;code&gt;Logged-in users can do anything&lt;/code&gt;&lt;br&gt;
-- Untick the &lt;code&gt;Allow anonymous read access&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your settings should be looking like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3um_ZvVU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v2sovd22qm2n1bejqnde.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3um_ZvVU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v2sovd22qm2n1bejqnde.png" alt="Alt Text" width="580" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And do not forget to &lt;code&gt;save&lt;/code&gt; the changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C7zLfuj0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2uub60jfh5p9k1db1oq6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C7zLfuj0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2uub60jfh5p9k1db1oq6.png" alt="Alt Text" width="708" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On successful save, it will redirect you to the &lt;code&gt;Create First Admin User&lt;/code&gt; page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will set the credentials of your new &lt;code&gt;admin&lt;/code&gt; user here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once, you've completed the fields you can click to the &lt;code&gt;Create First Admin User&lt;/code&gt; button.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vf9zAwsz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8rfquvlyx2r62lgwdko5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vf9zAwsz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8rfquvlyx2r62lgwdko5.png" alt="Alt Text" width="483" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Jenkins will redirect you to the &lt;code&gt;Success&lt;/code&gt; page if everything goes fine. You can see your new admin user's username or name on the right top corner. And when you try to login with your credentials it won't be a problem anymore.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fj5njCye--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v5i2sdgdqc3v4pwyyl2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fj5njCye--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v5i2sdgdqc3v4pwyyl2t.png" alt="Alt Text" width="880" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  That's all...
&lt;/h2&gt;

&lt;p&gt;Thanks for reading,&lt;/p&gt;

&lt;p&gt;Feel free to reach out to me at &lt;a href="mailto:omrglen@gmail.com"&gt;omrglen@gmail.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I’m open to suggestions &amp;amp; requests for future articles, cya 😃&lt;/p&gt;

</description>
      <category>devops</category>
      <category>javascript</category>
      <category>ci</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Node, Express, SSL Certificate: Run HTTPS Server from scratch in 5 steps</title>
      <dc:creator>Ömer Gülen</dc:creator>
      <pubDate>Thu, 02 Jan 2020 21:17:06 +0000</pubDate>
      <link>https://forem.com/omergulen/step-by-step-node-express-ssl-certificate-run-https-server-from-scratch-in-5-steps-5b87</link>
      <guid>https://forem.com/omergulen/step-by-step-node-express-ssl-certificate-run-https-server-from-scratch-in-5-steps-5b87</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Node, Express, SSL Certificate: Run HTTPS Server from scratch in 5 steps&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I've decided to write about this tutorial after I struggled while I was coding one of my web apps for a customer. It was a simple chart for the web but it was collecting data on a Telegram Bot. Held the data in MongoDB and prepared a quick API for fetching the data but got many problems on the way and SSL Certificate was one of them.&lt;/p&gt;

&lt;p&gt;So in this tutorial, I will go through my mistakes &amp;amp; problems and my solutions to them, if you want to skip straight to the short version, you can continue from here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;In this article I will not mention MongoDB related code or problems.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Creating My Basic API Server with Express
&lt;/h2&gt;

&lt;p&gt;In my projects, I prefer creating an &lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;yarn&lt;/code&gt; environment after creating the project folder.&lt;/p&gt;

&lt;p&gt;So, I've done it with the following commands:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;mkdir &lt;/span&gt;my-project &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;my-project
yarn init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Just spammed &lt;code&gt;Enter&lt;/code&gt; after &lt;code&gt;yarn init&lt;/code&gt; and created the project environment with default settings.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(I prefer &lt;code&gt;yarn&lt;/code&gt; over &lt;code&gt;npm&lt;/code&gt; if there are no obstacles to use it.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Then, I installed &lt;code&gt;express&lt;/code&gt; to my project, locally with:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

yarn add express


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;You can also use:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm &lt;span class="nb"&gt;install &lt;/span&gt;express


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then, I created my single source file &lt;code&gt;index.js&lt;/code&gt; and inserted these lines below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// import express&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// create new express app and assign it to `app` constant&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// server port configuration&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// create a route for the app&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello dev.to!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// server starts listening the `PORT`&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server running at: http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;So far, I imported the &lt;code&gt;express&lt;/code&gt; package, created an instance of it and assigned it to the &lt;code&gt;app&lt;/code&gt;. Set my &lt;code&gt;PORT&lt;/code&gt; variable, and created a route for &lt;code&gt;endpoint&lt;/code&gt; handling in my API Server and called &lt;code&gt;app.list(PORT, callback())&lt;/code&gt; method to start my server listening on the specified port.&lt;/p&gt;

&lt;p&gt;Went back to my terminal and executed the command below in my project directory:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

node index.js


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;which starts my server and logs to the console as below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Server running at http://localhost:8080/


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then, I switched to my browser and browsed to &lt;code&gt;http://localhost:8080/&lt;/code&gt; and the following page appeared:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fy50os9hzwtzqdq1q86e8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fy50os9hzwtzqdq1q86e8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far so good. My app is correctly listening to my port.&lt;br&gt;
Afterwards, I've tested my initial trial works and wanted to test if I can handle more endpoints. So I've just added another &lt;code&gt;route&lt;/code&gt; to my code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/omergulen&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Omer! Welcome to dev.to!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;I expect this to work only when I entered &lt;code&gt;/omergulen&lt;/code&gt; endpoint in my browser.&lt;/p&gt;

&lt;p&gt;So, I've stopped my running server with &lt;code&gt;Control+C&lt;/code&gt; and re-started again, since hot-reloading is not inherent with how I run my app. Switched to my browser and visited the &lt;code&gt;http://localhost:8080/omergulen&lt;/code&gt; and it was working, to be sure I re-visited the &lt;code&gt;http://localhost:8080/&lt;/code&gt; and it was also working as expected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fou7jn5bg7xzvrzavgwjg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fou7jn5bg7xzvrzavgwjg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Why and how to use middleware with Express?
&lt;/h2&gt;

&lt;p&gt;After my first API server deploys, I switched to my web app project and sent a fetch request to my API endpoint.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MY_API_URL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Nothing was happening in my DOM, but the console message was frustrating.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Access to fetch at 'MY_API_URL' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

App.js:34 Cross-Origin Read Blocking (CORB) blocked cross-origin response MY_API_URL with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;After doing some quick research, I've realized I needed to configure my API Server according to the &lt;code&gt;CORS Policy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, I've added added &lt;code&gt;mode: 'cors'&lt;/code&gt; to my fetch request:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MY_API_URL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;It was alone no use to my problem. Then, I've added my &lt;code&gt;cors&lt;/code&gt; middleware to my API server with only two lines actually.&lt;/p&gt;

&lt;p&gt;After installing &lt;code&gt;cors&lt;/code&gt; package with:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

yarn add cors


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;I just added these lines to my code:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// import `cors` package&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// use middleware&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And after running with these configurations, my problem was solved, for now.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. How to serve Express API Server as HTTPS?
&lt;/h2&gt;

&lt;p&gt;To deploy, I moved my project to my VPS and redirected my &lt;code&gt;my_api_url&lt;/code&gt; domain to this VPS. In that way I've put a small layer of abstraction to my server IP, Also, I wouldn't need to type my IP everywhere instead I could use my own domain with fancy subdomains like &lt;code&gt;api.omergulen.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this step, I first tried to deploy it without certification on HTTP.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

[blocked] The page at 'https://my_web_app'  was loaded over HTTPS but ran insecure content from 'http://my_api_url': this content should also be loaded over HTTPS.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Yet, my webserver was being server on Firebase Hosting and it was served as https, sending a request from &lt;code&gt;HTTPS to HTTP&lt;/code&gt; is called &lt;a href="https://developers.google.com/web/fundamentals/security/prevent-mixed-content/what-is-mixed-content" rel="noopener noreferrer"&gt;Mixed Content&lt;/a&gt;. And it is not allowed to.&lt;/p&gt;

&lt;p&gt;So, I just put &lt;code&gt;s&lt;/code&gt; at the beginning of the URL :))&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://my_api_url&lt;/code&gt; as you can guess, it didn't work either.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

GET https://my_api_url net::ERR_SSL_PROTOCOL_ERROR


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then, after doing focused research I've realized that I needed to create a certificate with a Certificate Authority. Many Certificate Authorities were paid but not &lt;a href="https://letsencrypt.org/" rel="noopener noreferrer"&gt;Let's Encrypt&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let’s Encrypt is a free, automated, and open Certificate Authority.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you have shell access to your server, it suggests you use &lt;a href="https://certbot.eff.org/" rel="noopener noreferrer"&gt;certbot&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;certbot&lt;/code&gt; website, I chose:&lt;/p&gt;

&lt;p&gt;My HTTP website is running &lt;code&gt;None of the above&lt;/code&gt; on &lt;code&gt;Ubuntu 16.04 (xenial)&lt;/code&gt; which was fitting to my case.&lt;/p&gt;

&lt;p&gt;Before starting they want you to be sure to have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;comfort with the command line&lt;/li&gt;
&lt;li&gt;and an HTTP website (API Server in my case)&lt;/li&gt;
&lt;li&gt;which is &lt;code&gt;online&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;and serving on HTTP port (&lt;code&gt;80&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;which is hosted on a &lt;code&gt;server&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;which you can access via &lt;code&gt;SSH&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;with the ability to &lt;code&gt;sudo&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then just apply the steps on the below:&lt;/p&gt;
&lt;h3&gt;
  
  
  1. SSH into the server
&lt;/h3&gt;

&lt;p&gt;SSH into the server running your HTTP website as a user with sudo privileges.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Add Certbot PPA
&lt;/h3&gt;

&lt;p&gt;You'll need to add the Certbot PPA to your list of repositories. To do so, run the following commands on the command line on the machine:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;software-properties-common &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;add-apt-repository universe &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;add-apt-repository ppa:certbot/certbot &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get update


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  3. Install Certbot
&lt;/h3&gt;

&lt;p&gt;Run this command on the command line on the machine to install Certbot.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;certbot


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  4. Choose how you'd like to run Certbot
&lt;/h3&gt;

&lt;p&gt;Are you ok with temporarily stopping your website?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Yes, my web server is not currently running on this machine.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stop your web server, then run this command to get a certificate. Certbot will temporarily spin up a webserver on your machine.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;certbot certonly &lt;span class="nt"&gt;--standalone&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;No, I need to keep my webserver running.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have a web server that's already using port 80 and don't want to stop it while Certbot runs, run this command and follow the instructions in the terminal.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;certbot certonly &lt;span class="nt"&gt;--webroot&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;In this step, you need to insert your domain into the terminal such as &lt;code&gt;dev.to&lt;/code&gt;. After that it will check your web server and look for specific files which it will create and in case of success it should print out like that:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Performing the following challenges:
http-01 challenge for my_api_url
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/my_api_url/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/my_api_url/privkey.pem
   Your cert will expire on 2020-04-01. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Important Note:&lt;/strong&gt;&lt;br&gt;
To use the webroot plugin, your server must be configured to serve files from hidden directories. If &lt;code&gt;/.well-known&lt;/code&gt; is treated specially by your webserver configuration, you might need to modify the configuration to ensure that files inside &lt;code&gt;/.well-known/acme-challenge&lt;/code&gt; are served by the webserver.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Installing signed certificate to Express API Server
&lt;/h2&gt;

&lt;p&gt;You'll need to install your new certificate in the configuration file for your API Server.&lt;/p&gt;

&lt;p&gt;First, you need to install and import some modules:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

yarn add https


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// import packages&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// serve the API with signed certificate on 443 (SSL/HTTPS) port&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpsServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/etc/letsencrypt/live/my_api_url/privkey.pem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;cert&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/etc/letsencrypt/live/my_api_url/fullchain.pem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;httpsServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;443&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HTTPS Server running on port 443&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;If you also want to maintain &lt;code&gt;HTTP&lt;/code&gt; requests among the &lt;code&gt;HTTPS&lt;/code&gt; requests, you can add the following lines, too:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// serve the API on 80 (HTTP) port&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;httpServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HTTP Server running on port 80&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;In the end your final API Server code will be something like that:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// import required packages&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// create new express app and save it as "app"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// create a route for the app&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello dev.to!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// another route&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/omergulen&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Omer! Welcome to dev.to!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Listen both http &amp;amp; https ports&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpsServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/etc/letsencrypt/live/my_api_url/privkey.pem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;cert&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/etc/letsencrypt/live/my_api_url/fullchain.pem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;httpServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HTTP Server running on port 80&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;httpsServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;443&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HTTPS Server running on port 443&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  5. Automatic Renewal and Test of the Certificate
&lt;/h2&gt;

&lt;p&gt;The Certbot packages on your system come with a cron job or systemd timer that will renew your certificates automatically before they expire. You will not need to run Certbot again, unless you change your configuration. You can test automatic renewal for your certificates by running this command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;certbot renew &lt;span class="nt"&gt;--dry-run&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The command to renew certbot is installed in one of the following locations:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

/etc/crontab/
/etc/cron.&lt;span class="k"&gt;*&lt;/span&gt;/&lt;span class="k"&gt;*&lt;/span&gt;
systemctl list-timers


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;If you needed to stop your webserver to run Certbot, you'll want to edit the built-in command to add the &lt;code&gt;--pre-hook&lt;/code&gt; and &lt;code&gt;--post-hook&lt;/code&gt; flags to stop and start your web server automatically. For example, if your webserver is HAProxy, add the following to the &lt;code&gt;certbot renew&lt;/code&gt; command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nt"&gt;--pre-hook&lt;/span&gt; &lt;span class="s2"&gt;"service haproxy stop"&lt;/span&gt; &lt;span class="nt"&gt;--post-hook&lt;/span&gt; &lt;span class="s2"&gt;"service haproxy start"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;More information is available in the &lt;a href="https://certbot.eff.org/docs/using.html?highlight=hooks#renewing-certificates" rel="noopener noreferrer"&gt;Certbot documentation on renewing certificates&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Confirm that Certbot worked&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To confirm that your site is set up properly, visit &lt;code&gt;https://yourwebsite.com/&lt;/code&gt; in your browser and look for the lock icon in the URL bar. If you want to check that you have the top-of-the-line installation, you can head to &lt;a href="https://www.ssllabs.com/ssltest/" rel="noopener noreferrer"&gt;https://www.ssllabs.com/ssltest/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Well done! You have come to the end of this long tutorial.&lt;/p&gt;

&lt;p&gt;After applying these steps you can finally go to your API Server URL and you should be seeing &lt;code&gt;Hello dev.to!&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Thanks for reading&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;I hope this tutorial was helpful enough.&lt;/p&gt;

&lt;p&gt;You can check my last article here: &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/omergulen" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F229624%2F981bfd43-591d-41b4-b782-82f688ad0506.jpg" alt="omergulen"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/omergulen/how-to-add-system-call-syscall-to-the-kernel-compile-and-test-it-3e6p" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How to add system call (syscall) to the kernel, compile and test it?&lt;/h2&gt;
      &lt;h3&gt;Ömer Gülen ・ Dec 25 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#kernel&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#syscall&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#linux&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#unix&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;Feel free to reach out to me at &lt;a href="mailto:omrglen@gmail.com"&gt;omrglen@gmail.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I’m opened to suggestions &amp;amp; requests for future articles, cya 😃&lt;/p&gt;

&lt;p&gt;Happy New Year! 🥳🥳🥳&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>How to add system call (syscall) that prints elapsed time of a process with given PID to the kernel and test it?</title>
      <dc:creator>Ömer Gülen</dc:creator>
      <pubDate>Fri, 27 Dec 2019 18:18:52 +0000</pubDate>
      <link>https://forem.com/omergulen/how-to-add-system-call-syscall-that-prints-elapsed-time-of-a-process-with-given-pid-to-the-kernel-and-test-it-335c</link>
      <guid>https://forem.com/omergulen/how-to-add-system-call-syscall-that-prints-elapsed-time-of-a-process-with-given-pid-to-the-kernel-and-test-it-335c</guid>
      <description>&lt;p&gt;In my previous tutorial 👇&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/omergulen" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Viobpfhe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--Yevyuz4B--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/229624/981bfd43-591d-41b4-b782-82f688ad0506.jpg" alt="omergulen"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/omergulen/how-to-add-system-call-syscall-to-the-kernel-compile-and-test-it-3e6p" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How to add system call (syscall) to the kernel, compile and test it?&lt;/h2&gt;
      &lt;h3&gt;Ömer Gülen ・ Dec 25 '19 ・ 6 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#kernel&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#syscall&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#linux&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#unix&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;I described how to add syscall to the kernel, compile it and test it in detail. As promised, I now would like to describe how to create a syscall that prints out the elapsed time of a process with a given process id(PID).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This task was my Operating Systems Programming Assignment.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;In this tutorial, unlike the previous tutorial I will not go over into every detail, but point out the different parts of the process.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After changing my current directory into kernel source files, I create another folder for my new function. You don't need to do it, but I prefer it this way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;petime &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;petime
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;I will call my function &lt;code&gt;petime&lt;/code&gt; short for process elapsed time.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After I've created a C file which my code goes into.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vim petime.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my C code, I used &lt;code&gt;SYSCALL_DEFINE1&lt;/code&gt; Macro from &lt;code&gt;&amp;lt;linux/syscalls.h&amp;gt;&lt;/code&gt;. Because I needed to pass one integer argument to the function.&lt;/p&gt;

&lt;p&gt;Also, you can use it for different argument counts. While argument count is &lt;code&gt;X&lt;/code&gt;, the syntax will be like &lt;code&gt;SYSCALL_DEFINEX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// For 2 parameters&lt;/span&gt;
&lt;span class="n"&gt;SYSCALL_DEFINE2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second_int&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// code goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// For 1 parameter&lt;/span&gt;
&lt;span class="n"&gt;SYSCALL_DEFINE1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_int&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// code goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// For no-parameters&lt;/span&gt;
&lt;span class="n"&gt;SYSCALL_DEFINE0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function_name&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// code goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to this Macro, I used the &lt;code&gt;task_struct&lt;/code&gt; struct to get the process' start time and time elapsed since the boot with &lt;code&gt;ktime_get_ns()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;My C file which contains my syscall:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;linux/kernel.h&amp;gt;
#include &amp;lt;linux/sched.h&amp;gt;
#include &amp;lt;linux/syscalls.h&amp;gt;
&lt;/span&gt;
&lt;span class="c1"&gt;// Used this macro to be able to pass variables to the syscall&lt;/span&gt;
&lt;span class="c1"&gt;// It takes one integer, PID.&lt;/span&gt;
&lt;span class="n"&gt;SYSCALL_DEFINE1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys_petime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Using task_struct struct from sched.h, which has task's (process)&lt;/span&gt;
  &lt;span class="c1"&gt;// various data mostly about scheduling.&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;task_struct&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;u64&lt;/span&gt; &lt;span class="n"&gt;start_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;elapsed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Get process with PID.&lt;/span&gt;
  &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pid_task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;find_vpid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;PIDTYPE_PID&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// If process is not found.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Print the error to the kernel buffer&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"Cannot find a process with that PID: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Taken from "taskstats.c" source code (line 240-241).&lt;/span&gt;
    &lt;span class="c1"&gt;// Used the logic behind calculating the delta time in that source.&lt;/span&gt;
    &lt;span class="c1"&gt;// Get current nanoseconds since boot&lt;/span&gt;
    &lt;span class="n"&gt;start_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ktime_get_ns&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// Difference&lt;/span&gt;
    &lt;span class="n"&gt;elapsed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start_time&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;real_start_time&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Print result to the kernel buffer&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"PID: %d has ELAPSED %llu nanoseconds."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;elapsed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// If nothing goes wrong, return 0 as successful&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;code&gt;ktime_get_ns()&lt;/code&gt; is like timestamp but it doesn't count from 1 Jan 1970 instead it counts nanoseconds since the Operating System boots.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;code&gt;task-&amp;gt;real_start_time&lt;/code&gt; is the boot based start time of the process in nanoseconds.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So as a result, the difference of total elapsed time since boot and the boot based start time of a process gives the elapsed time of a process.&lt;/p&gt;

&lt;p&gt;Then, I've created &lt;code&gt;Makefile&lt;/code&gt; file of my syscall:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vim Makefile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and inserted the default-ish configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;obj-y := petime.o
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, I need to let my general &lt;code&gt;Makefile&lt;/code&gt; know about my new folder and function.&lt;/p&gt;

&lt;p&gt;Go back to the kernel source folder and edit &lt;code&gt;Makefile&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; .. &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; vim Makefile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Find the line with the &lt;code&gt;core-y&lt;/code&gt;, it should be similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;core-y      += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ hello/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;hello/&lt;/code&gt; is the folder from &lt;a href="https://dev.to/omergulen/how-to-add-system-call-syscall-to-the-kernel-compile-and-test-it-3e6p"&gt;our previous tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I've added my new folder &lt;code&gt;petime&lt;/code&gt; to the end of this line. Result should be looking like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;core-y      += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ hello/ petime/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After saving the &lt;code&gt;Makefile&lt;/code&gt;, we need to add our new syscall to the syscalls tables.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I will describe the tutorial according to 64-bit Operating System, if you want to do it on 32-bit OS, you can go back to my previous tutorial find the details of the following steps for 32-bit OS.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For 64-bit OS, I add my syscall to the end of my file(table):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;549 64  petime  __64_sys_sys_petime
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've put &lt;code&gt;sys&lt;/code&gt; 2 times, like &lt;code&gt;_sys_sys_&lt;/code&gt; because my function name was already &lt;code&gt;sys_petime&lt;/code&gt; but you need to prepend &lt;code&gt;__64_sys_&lt;/code&gt; of the function name in this table, so result becomes &lt;code&gt;__64_sys_sys_petime&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, we need to add our function signature to the syscalls header file &lt;code&gt;syscalls.h&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vim include/linux/syscalls.h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, add the following line to the end of the document before the #endif statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;asmlinkage&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;sys_petime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can compile it the same as our Hello World example.&lt;/p&gt;

&lt;p&gt;After the, compilation we can test it with the following C code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;linux/kernel.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;sys/syscall.h&amp;gt;
#include &amp;lt;unistd.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Get args from terminal and call petime (get elapsed time) with that PID.&lt;/span&gt;
  &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;helloCheck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;syscall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;549&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;atoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"System call sys_petime returned %ld&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;helloCheck&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the result is such that:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4rtwaRVQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/SKfmZGQ/screenshot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4rtwaRVQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/SKfmZGQ/screenshot.png" alt="Terminal output of syscall" width="880" height="928"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Please comment below your questions and feedback.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>kernel</category>
      <category>unix</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to add system call (syscall) to the kernel, compile and test it?</title>
      <dc:creator>Ömer Gülen</dc:creator>
      <pubDate>Wed, 25 Dec 2019 17:13:46 +0000</pubDate>
      <link>https://forem.com/omergulen/how-to-add-system-call-syscall-to-the-kernel-compile-and-test-it-3e6p</link>
      <guid>https://forem.com/omergulen/how-to-add-system-call-syscall-to-the-kernel-compile-and-test-it-3e6p</guid>
      <description>&lt;p&gt;&lt;em&gt;One of my example &lt;code&gt;syscall&lt;/code&gt; methods will print &lt;code&gt;Hello world&lt;/code&gt; to the kernel buffer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The second one gets PID of a process and prints the &lt;code&gt;elapsed time&lt;/code&gt; of it to the kernel buffer in my other tutorial:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/omergulen" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F229624%2F981bfd43-591d-41b4-b782-82f688ad0506.jpg" alt="omergulen"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/omergulen/how-to-add-system-call-syscall-that-prints-elapsed-time-of-a-process-with-given-pid-to-the-kernel-and-test-it-335c" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How to add system call (syscall) that prints elapsed time of a process with given PID to the kernel and test it?&lt;/h2&gt;
      &lt;h3&gt;Ömer Gülen ・ Dec 27 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#kernel&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#unix&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#linux&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  1. Setup environment
&lt;/h2&gt;

&lt;p&gt;I prefer to get &lt;code&gt;root&lt;/code&gt; and apply the steps as &lt;code&gt;root&lt;/code&gt; to prevent getting permission failures on the way. If you don't want to apply the steps as a root, you can just use &lt;code&gt;sudo&lt;/code&gt; for root required commands.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

su -


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;will switch to &lt;code&gt;root&lt;/code&gt; user after entering the password of the &lt;code&gt;root&lt;/code&gt;. Then, first you can check the active kernel version of your OS with:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;uname&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This prints out &lt;code&gt;4.19.0-6-amd64&lt;/code&gt; in my case. Then, we need to get the source of a kernel. I will use slightly newer version (4.20.1) from my version with following &lt;code&gt;wget&lt;/code&gt; command.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.20.1.tar.xz


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will download the source of &lt;code&gt;4.20.1&lt;/code&gt;. It may take a while, like 2-3 minutes depending on your internet speed.&lt;/p&gt;

&lt;p&gt;Extract the compressed kernel code with&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-xvf&lt;/span&gt; linux-4.20.1.tar.xz


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;It will create a folder named &lt;code&gt;linux-4.20.1.tar.xz&lt;/code&gt; and extract the compressed code into that folder.&lt;/p&gt;

&lt;p&gt;Now we will change our directory to new kernel code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd &lt;/span&gt;linux-4.20.1


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  2. Add "Hello world" syscall to kernel
&lt;/h2&gt;

&lt;p&gt;I prefer creating new folder for my own stuff while adding a new syscall.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;mkdir &lt;/span&gt;hello &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;hello


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;After that, I will create a C file for my syscall implementation. I prefer &lt;code&gt;vim hello.c&lt;/code&gt; to create and edit the file and insert following C code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;linux/kernel.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;asmlinkage&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;sys_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//printk prints to the kernel’s log file.&lt;/span&gt;
        &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We need to create a &lt;code&gt;Makefile&lt;/code&gt; in the &lt;code&gt;hello&lt;/code&gt; directory.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

vim Makefile


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;and then insert this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 := hello.o

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then, go to the parent directory (kernel source main directory):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd&lt;/span&gt; ..


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We need to add our new syscall directory to &lt;code&gt;Makefile&lt;/code&gt;, in this way it will compile our syscall, too. To achieve this, search for &lt;code&gt;core-y&lt;/code&gt; in the &lt;code&gt;Makefile&lt;/code&gt; then, find the&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In vim you can do search with &lt;code&gt;/core-y&lt;/code&gt; after pressing ESC.&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 += kernel/ mm/ fs/ ipc/ security/ crypto/ block/

``` line and add `hello/` to the end of this line.

As a result, the line should be looking like this:

```core-y

 += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ hello/

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Next step is adding the new system call to the system call table.&lt;br&gt;
If you are on a 32-bit system you’ll need to change &lt;code&gt;syscall_32.tbl&lt;/code&gt; file.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

vim &lt;span class="nb"&gt;arch&lt;/span&gt;/x86/entry/syscalls/syscall_32.tbl


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;For 64-bit, change &lt;code&gt;syscall_64.tbl&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

vim &lt;span class="nb"&gt;arch&lt;/span&gt;/x86/entry/syscalls/syscall_64.tbl


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;em&gt;I will continue with my 64-bit OS and my steps will be accordingly.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We need to keep table's structure while adding our syscall. So that we will add our line to the end of the line. My last syscall has number of &lt;code&gt;547&lt;/code&gt;, so that I will use &lt;code&gt;548&lt;/code&gt;, you also should use &lt;code&gt;N+1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My syscall data:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
       64        hello          sys_hello

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Example file:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

546     ...     ...     ...
547     ...     ...     ...
548     64      hello   sys_hello


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now, we need to add our syscall method signature to &lt;code&gt;syscalls&lt;/code&gt; header file which is &lt;code&gt;syscalls.h&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

vim include/linux/syscalls.h


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then, add the following line to the end of the document before the #endif statement:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;

&lt;span class="n"&gt;asmlinkage&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;sys_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  3. Compiling our kernel
&lt;/h2&gt;

&lt;p&gt;Before starting to compile you need to install a few packages. Type the following commands in your terminal to install required packages:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

apt-get &lt;span class="nb"&gt;install &lt;/span&gt;gcc &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
apt-get &lt;span class="nb"&gt;install &lt;/span&gt;libncurses5-dev &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
apt-get &lt;span class="nb"&gt;install &lt;/span&gt;bison &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
apt-get &lt;span class="nb"&gt;install &lt;/span&gt;flex &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
apt-get &lt;span class="nb"&gt;install &lt;/span&gt;libssl-dev &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
apt-get &lt;span class="nb"&gt;install &lt;/span&gt;libelf-dev &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
apt-get upgrade &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
apt-get make


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now, you can configure your kernel by using the config menu by executing:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

make menuconfig


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT NOTE&lt;/strong&gt;: While entering this command, you might want to maximize your terminal screen. Otherwise, you might get and error and a pop-up screen will not appear.&lt;/p&gt;

&lt;p&gt;Once the above command is used to configure the Linux kernel, you will get a pop up window with the list of &lt;br&gt;
menus and you can select the items for the new configuration. If your unfamiliar with the configuration just check for the file systems menu and check whether “ext4” is chosen or not, if not select it and save the configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OR:&lt;/strong&gt; You can simply execute the following command and use the default configuration.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

make defconfig


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now, finally we can compile the kernel with the following command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;make


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The compilation took 40 mins to 1 hour on my uni-core VM. As an alternative solution you can increase the core count of your VM in the VM settings and use them to compilation with the &lt;code&gt;-jn&lt;/code&gt; parameter. &lt;code&gt;n&lt;/code&gt; is the number of cores dedicated to your VM.&lt;/p&gt;

&lt;p&gt;In my case, I've used the following ocmmand for 8-core VM which reduced the compilation time to 20 seconds to 1 minute:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

make &lt;span class="nt"&gt;-j8&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  4. Installing our kernel
&lt;/h2&gt;

&lt;p&gt;After the successful compilation, to install/update kernel use the following command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

make modules_install &lt;span class="nb"&gt;install&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The command will create some files under &lt;code&gt;/boot/&lt;/code&gt; directory and it will &lt;code&gt;automatically&lt;/code&gt; make a entry in your grub.cfg. &lt;br&gt;
To check whether it made correct entry, check the files under &lt;code&gt;/boot/&lt;/code&gt; directory . If you have followed the steps without any error you will find the following files in it in addition to others.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Testing our syscall in new kernel
&lt;/h2&gt;

&lt;p&gt;Now all we need to do is restart the system:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

shutdown &lt;span class="nt"&gt;-r&lt;/span&gt; now


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;After computer restarts, in &lt;code&gt;grub&lt;/code&gt;'s advanced options, you can see there are 2 options (without counting the recovery mode options).&lt;/p&gt;

&lt;p&gt;Once your computer is up again, you can run the following command the check your kernel version:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;uname&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;which prints &lt;code&gt;4.20.1&lt;/code&gt; in my OS after installing the kernel.&lt;/p&gt;

&lt;p&gt;After checking the version of the kernel, we will test our &lt;code&gt;syscall&lt;/code&gt; with tiny C program.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

vim hello_test.c


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Insert the following C code into the &lt;code&gt;hello_test.c&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;linux/kernel.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;sys/syscall.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;helloCheck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;syscall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;548&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"System call sys_hello returned %ld&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;helloCheck&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then compile and run it:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

gcc hello_test.c &lt;span class="nt"&gt;-o&lt;/span&gt; hello.o &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; ./hello.o


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;it prints &lt;code&gt;System call sys_hello returned 0&lt;/code&gt; if there is no problem on the execution and now we can check the kernel log buffer to see if our &lt;code&gt;Hello world&lt;/code&gt; is there with the command below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

dmesg


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;It will print tons of line and in the end we should be seeing our &lt;code&gt;Hello world&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;[&lt;/span&gt;    ........] ...
&lt;span class="o"&gt;[&lt;/span&gt;    ........] ...
&lt;span class="o"&gt;[&lt;/span&gt;    ........] ...
&lt;span class="o"&gt;[&lt;/span&gt;    2.858022] IPv6: ADDRCONF&lt;span class="o"&gt;(&lt;/span&gt;NETDEV_UP&lt;span class="o"&gt;)&lt;/span&gt;: enp0s3: &lt;span class="nb"&gt;link &lt;/span&gt;is not ready
&lt;span class="o"&gt;[&lt;/span&gt;    2.858144] ip &lt;span class="o"&gt;(&lt;/span&gt;1443&lt;span class="o"&gt;)&lt;/span&gt; used greatest stack depth: 12424 bytes left
&lt;span class="o"&gt;[&lt;/span&gt;    2.860434] Adding 10483708k swap on /dev/sda5.  Priority:-2 extents:1 across:10483708k 
&lt;span class="o"&gt;[&lt;/span&gt;    3.391290] hrtimer: interrupt took 4499049 ns
&lt;span class="o"&gt;[&lt;/span&gt;    4.887969] e1000: enp0s3 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
&lt;span class="o"&gt;[&lt;/span&gt;    4.888216] IPv6: ADDRCONF&lt;span class="o"&gt;(&lt;/span&gt;NETDEV_CHANGE&lt;span class="o"&gt;)&lt;/span&gt;: enp0s3: &lt;span class="nb"&gt;link &lt;/span&gt;becomes ready
&lt;span class="o"&gt;[&lt;/span&gt;  306.497742] Hello world


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We can observe that our &lt;code&gt;Hello world&lt;/code&gt; is printed to kernel log buffer. So it worked!&lt;/p&gt;
&lt;h2&gt;
  
  
  Error - Fix
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Debian Certificate Problem
&lt;/h4&gt;

&lt;p&gt;On my first trial I have faced with an error such below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

make[1]: &lt;span class="k"&gt;***&lt;/span&gt; No rule to make target &lt;span class="s1"&gt;'debian/certs/debian-uefi-certs.pem'&lt;/span&gt;, needed by &lt;span class="s1"&gt;'certs/x509_certificate_list'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;  Stop.
make: &lt;span class="k"&gt;***&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;Makefile:1055: certs] Error 2


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  - SOLUTION -
&lt;/h4&gt;

&lt;p&gt;I've found the solution of this problem in the &lt;a href="https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=897163" rel="noopener noreferrer"&gt;debian.org's bug report section&lt;/a&gt;. The issue was reported by &lt;em&gt;Heinrich Schuchardt&lt;/em&gt; as the following:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Debian kernels are currently built with CONFIG_SYSTEM_TRUSTED_KEYS="debian/certs &lt;a href="mailto:benh@debian.org.cert.pem"&gt;benh@debian.org.cert.pem&lt;/a&gt;"&lt;br&gt;
This was introduced with this &lt;a href="https://alioth-lists-archive.debian.net/pipermail/kernel-svn-changes/2016-April/022904.html" rel="noopener noreferrer"&gt;https://alioth-lists-archive.debian.net/pipermail/kernel-svn-changes/2016-April/022904.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We are now two years after Ben's contribution and we are still not using kernel module signing (CONFIG_MODULE_SIG is not set in config).&lt;/p&gt;

&lt;p&gt;As there is no need for the kernel trusting Ben's certificate, please, remove the setting.&lt;/p&gt;

&lt;p&gt;Best regards&lt;/p&gt;

&lt;p&gt;Heinrich Schuchardt&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a result, I've cleared the &lt;code&gt;CONFIG_SYSTEM_TRUSTED_KEYS&lt;/code&gt; config in the configuration file.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;linux-4.20.1&lt;/code&gt; (which is my kernel source directory), there is a file named &lt;code&gt;.config&lt;/code&gt; which stores the configuration of the kernel. With the following command I opened it:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

vim .config


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then searched for &lt;code&gt;CONFIG_SYSTEM_TRUSTED_KEYS&lt;/code&gt; and changed&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

CONFIG_SYSTEM_TRUSTED_KEYS="debian/certs benh@debian.org.cert.pem"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;into this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

CONFIG_SYSTEM_TRUSTED_KEYS=""


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;then, saved and closed the file, and tried to compile again and &lt;em&gt;there was no issue.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I will publish the second example in another article since this one is already so long.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Please comment below your questions and feedback.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/omergulen" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F229624%2F981bfd43-591d-41b4-b782-82f688ad0506.jpg" alt="omergulen"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/omergulen/how-to-add-system-call-syscall-that-prints-elapsed-time-of-a-process-with-given-pid-to-the-kernel-and-test-it-335c" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How to add system call (syscall) that prints elapsed time of a process with given PID to the kernel and test it?&lt;/h2&gt;
      &lt;h3&gt;Ömer Gülen ・ Dec 27 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#kernel&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#unix&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#linux&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



</description>
      <category>kernel</category>
      <category>syscall</category>
      <category>linux</category>
      <category>unix</category>
    </item>
  </channel>
</rss>
