Thursday, 23 February 2017

Puppet Resources and Types

Resources in Puppet Configuration - In the last article on Puppet - "How to Install and Configure Puppet Master and Puppet Agent in Linux", we learned to create and configure Puppet master and Puppet agent node. In this article, we will be learning about the very basic component of Puppet configuration, known as Resources.

puppet-resources-and-types

What are Resources?

As mentioned, Resources are the fundamental components of a system, which have certain Attributes - state it can be in (present or absent, enabled or disabled, etc.) or an action associated with it (create, delete or modify). With these resources, we can have control on system parameters (like files, users, packages, services, etc.).

Depending on what system parameter a resource can manipulate and configuration it controls, each resource belongs to a particular Resource Type. The complete list of the resource types is provided here. In this article, we will see some of the basic resource types used in writing a Puppet code (also known as Manifest).

1. file

This resource type is used to manage files, directories and symbolic links (Read more about symbolic links). As I mentioned, every resource has a set of attributes associated with it. So lets check out come important attributes associated with a file resource.

  • path - It states the absolute path of the file/directory.
  • Example - '/usr/local/nagios/etc/nagios.cfg'
  • ensure - It states the existence of a file on the system i.e. it is present or absent. If it is present, whether it is a file, directory or a link. If it is a link, one should also mention about the target attribute, representing the source file it must point to.
  • content - This attribute represent what text the file will contain. In this case, you can mention the content within single quotes, like 'Hello world!' or you can use the source attribute, which expects a path to a file. So that, both these files will have same contents.
  • mode - This attributes represents the permissions on the file in octal notations, like 0755, 0644, etc.
  • owner and group - These attributes are associated with ownership of the file.
  • replace - Valid values for this attributes are true, false, yes and no. It states whether the file should be replaced if already present or otherwise.
  • source - As mentioned above, this attribute expects a path to a file, on the same system or the Puppet master. Thus, this file will have the same contents as that of the source file mentioned.

2. package

This resource type is used to manage packages on the Puppet agent nodes, including installation, updation and removal. Lets see some of the attributes associated with this resource type.

  • name - Name of the package.
  • Example - 'httpd'
  • ensure - It states the existence of a package on the system i.e. it should be present or absent.
  • provider - This attribute represent what backend/package manager, like yum or apt or zypper, it should use to install/remove the package.
  • source - This attribute is used to represent where to find a package, in case a package is not provided by the provider and available in a remote central repository.

3. service

This resource type is used to manage services on the Puppet agent nodes. It includes starting, stopping and restarting the service. Not only this, to add or remove the service from startup. Lets see some of the attributes associated with this resource type.

  • name - Name of the service.
  • Example - 'sshd'
  • ensure - It denotes the state of a service on the system i.e. it should be running or stopped.
  • enable - This attribute represents whether a service should start at system boot or not. So, the valid values are true and false.
  • path - With this attribute, we can specify the path where Puppet can find the init scripts.

4. user

This resource type is used to manage system users on the Puppet agent nodes. It includes addition, deletion and modification of the users. Lets see some of the attributes associated with this resource type.

  • name - Name of the user.
  • Example - 'nagios'
  • ensure - It denotes the state of a user on the system i.e. it should be present or absent.
  • comment - This attribute represents description of the user, like 'Nagios Admin User'.
  • expiry - With this attribute, we can specify the date, in YYYY-MM-DD format, when the user account expires.
  • uid/gid - UID and GID of the user.
  • shell - Shell of the user, like '/bin/bash', '/bin/ksh', '/sbin/nologin', etc.

5. exec

This resource type is used to execute system commands on the Puppet agent nodes, provided that, the command should be Idempotent (meaning that, it should always produce same result when executed multiple times). Lets see some of the attributes associated with this resource type.

  • command - Command name.
  • Example - '/bin/mkdir'
  • cwd - Name of the directory from which command should be executed. If directory does not exist, command execution will fail.
  • creates - File that should be looked for before executing the command. If file doesn't exist, command will be executed and file will be created.
  • onlyif - With this attribute, we can set dependency of the executing based on exit status of another command. So, if the other command executes successfully, this command will be executed, otherwise not.
  • path - The path that should be searched to find the command.
  • returns - Exit status of the command which should be returned, must be an integer between 0-255.
  • timeout - The maximum time the command should take for completing the execution.
  • unless - Opposite of onlyif. The command will be executed if exit status of other command is non-zero.

That's it for this article, which was supposed to introduce you with Puppet Resources. In the next article, we will learn about another new concept - Puppet Manifests. Till then, stay tuned!

Wednesday, 22 February 2017

Bash Scripting - 'select' Loop and 'select-case' Statement

Bash 'select' loop - We have been learning about loops in shell scripting and in the recent articles, we have covered two of its kinds - for loop and while loop. In this article, we will be covering another type of a loop - select loop.

bash-select-loop-and-select-case-statement

Similar to one of the cases in for loop, select loop accepts a sequence of strings/characters separated by whitespace. This sequence of strings is provided as numbered options, just like multiple choice questions, on the terminal screen. You have to select from these options and corresponding command will be run. Lets learn this command in more details.

Basic 'select' Loop

In select loop, we have the similar syntax as that of for loop. As mentioned above, select loop accepts a sequence to iterate through it and present it as a numbered menu. For this, we use the in keywordas shown in below syntax.

Syntax:

select variableName in choice1 choice2 ... choiceN
do

    -- Block of Commands --
   
done

Example:

#!/bin/bash

echo "Which is Your Favorite Linux Distribution..?"

select os in Ubuntu LinuxMint CentOS RedHat Fedora
do
    echo "I also like $os !" 
done

In above example, we have provided five strings in the sequence. So, we expect the select to provide these stings in the numbered menu. We then have to select from the choices, one at a time. With each selection, the variable $os will be assigned with the value of the choice we make and echo statement will be executed.

Result:

Which is Your Favorite Linux Distribution..?                                                                                                                             
1) Ubuntu                                                                                                                                                                
2) LinuxMint                                                                                                                                                             
3) CentOS                                                                                                                                                                
4) RedHat                                                                                                                                                                
5) Fedora                                                                                                                                                                
#? 1                                                                                                                                                                     
I also like Ubuntu !                                                                                                                                                     
#? 2                                                                                                                                                                     
I also like LinuxMint !                                                                                                                                                  
#? 3                                                                                                                                                                     
I also like CentOS !                                                                                                                                                     
#? 4                                                                                                                                                                     
I also like RedHat !                                                                                                                                                     
#? 5                                                                                                                                                                     
I also like Fedora !                                                                                                                                                     
#? 6                                                                                                                                                                     
I also like  !                                                                                                                                                           
#? ^C

From above result, we have 3 observations-
  1. The loop is infinite, it will stop only when you press 'Ctrl + C'.
  2. When I entered '6', which is not a valid choice, it will still print something on the screen.
  3. The default prompt is #?, which does not make any sense.
The first two issues can be taken care of with the use of case statement in the select loop. The provision of default value in the case statement comes to the rescue here. We will not cover the case statement here, please check our article on case statement in bash scripting for that. For now, we introduce a case statement in our select loop.

Example - With case statement :

#!/bin/bash

echo "Which Operating System Do You Use..?"

select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP Mac
do
    case $os in
        "Ubuntu" | "LinuxMint")
            echo "I also use $os ..!"
            ;;

        "Windows8" | "Windows7" | "WindowsXP")
            echo "Why don't you try Linux..?"
            ;;

        "Mac")
            echo "You must be Very Rich..!"
            ;;
        *)
            echo "Invalid option. Program will exit now."
            break
            ;;
    esac
done 

Output :

Which Operating System Do You Use..?                                                                                                                                     
1) Ubuntu     3) Windows8   5) WindowsXP                                                                                                                                 
2) LinuxMint  4) Windows7   6) Mac                                                                                                                                       
#? 1                                                                                                                                                                     
I also use Ubuntu ..!                                                                                                                                                    
#? 2                                                                                                                                                                     
I also use LinuxMint ..!                                                                                                                                                 
#? 3                                                                                                                                                                     
Why don't you try Linux..?                                                                                                                                               
#? 4                                                                                                                                                                     
Why don't you try Linux..?                                                                                                                                               
#? 5                                                                                                                                                                     
Why don't you try Linux..?                                                                                                                                               
#? 6                                                                                                                                                                     
You must be Very Rich..!                                                                                                                                                 
#? 7                                                                                                                                                                     
Invalid option. Program will exit now.

So, we have dealt with two of the issues we observed earlier. Now, for the third one, we set a shell variable PS3 to the value we desire to appear as the prompt, as below.

Example - With the custom prompt :

#!/bin/bash

echo "Which Operating System Do You Use..?"

PS3="Enter your choice (must be a number): "

select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP Mac
do
    case $os in
        "Ubuntu" | "LinuxMint")
            echo "I also use $os ..!"
            ;;

        "Windows8" | "Windows7" | "WindowsXP")
            echo "Why don't you try Linux..?"
            ;;

        "Mac")
            echo "You must be Very Rich..!"
            ;;
        *)
            echo "Invalid option. Program will exit now."
            break
            ;;
    esac
done 

Output:

Which Operating System Do You Use..?                                                                                                                                     
1) Ubuntu     3) Windows8   5) WindowsXP                                                                                                                                 
2) LinuxMint  4) Windows7   6) Mac                                                                                                                                       
Enter your choice (must be a number): 1                                                                                                                                  
I also use Ubuntu ..!                                                                                                                                                    
Enter your choice (must be a number): 4                                                                                                                                  
Why don't you try Linux..?                                                                                                                                               
Enter your choice (must be a number): 6                                                                                                                                  
You must be Very Rich..!                                                                                                                                                 
Enter your choice (must be a number): 9                                                                                                                                  
Invalid option. Program will exit now.

With this, we close our discussion on select loop. In this article, we learned how a basic select loop is constructed, we observed the issues with the basic usage, then to mitigate those we introduced case statement in the loop. We also learned how we can use PS3 variable to set a custom prompt to add values to our program. This is it for this article and stay tuned for more ones. Thanks for stopping by.

Tuesday, 21 February 2017

Bash Scripting - 'case' Statement

Bash 'case' statement - We have been learning about loops in shell scripting and in the recent articles, we have covered two of its kinds - for loop and while loop. Before we move further and discuss about another type of a loop - select loop, it is of utmost importance to know about a control flow statement - case statement. Before we proceed, I recommend you to read our article on another kind of control flow statement - if-elif-else statement in bash scripting.

bash-scripting-case-statement

'case' Statement

The case statement executes any one block of commands, based on the outcome of a pattern match. We have a variable that stores a value to be matched and a number of patterns in the order they are arranged, which may or may not be regular expressions, against which the value is matched. If a match is found, block of commands corresponding to the pattern is executed. If the value doesn't match any of the patterns, the default block is executed.

Syntax:

case $varName in
    pattern1)       
        ...
        Block of Commands #1
        ...
        ;;
    pattern2)
        ...
        Block of Commands #2
        ...
        ;;
    ...
    ...
    patternN)
        ...       
        Block of Commands #N
        ...
        ;;
    *)
        ...       
        Default Block of Commands
        ...          
esac

OR

case $varName in

    pattern1|pattern2|pattern3)       
        ...
        Block of Commands #1
        ...
        ;;
    pattern4|pattern5|pattern6)
        ...
        Block of Commands #2
        ...
        ;;
    pattern7|pattern8|patternN)      
        ...
        Block of Commands #N
        ...
        ;;
    *)
        ...
        Default Block of Commands
        ...
esac

Example:

#!/bin/bash

echo "Which is your Favorite Operating System..?"
read os

case $os in

    "Linux") echo "Woww!! I am also a Linux Fan!!"
    ;;

    "Mac") echo "You must be very Rich!"
    ;;

    "Windows") echo "You Should Try Linux Once.. You would love it!"
    ;;

    *) echo "I've never used that one!"

esac

In above example, we ask the user about his favorite operating system, using echo command. We read the user input and store it in $os variable, with the use of read os. With this variable used in case statement as a switch, we compare it against different operating systems, such as Linux, Windows and Mac. The content stored in $os is matched against these patterns and corresponding block of code is executed. If match is not found, the default block (the one beginning with *)) is executed.

Output:

Which is your Favorite Operating System..?                                                                                                                               
Linux                                                                                                                                                                    
Woww!! I am also a Linux Fan!!                                                                                                                                           

Which is your Favorite Operating System..?                                                                                                                               
Mac                                                                                                                                                                      
You must be very Rich!                                                                                                                                                   

Which is your Favorite Operating System..?                                                                                                                               
Windows                                                                                                                                                                  
You Should Try Linux Once.. You would love it!                                                                                                                           

Which is your Favorite Operating System..?                                                                                                                               
Unix                                                                                                                                                                     
I've never used that one! 

Great! The script works just fine. But there is a minor bug in that. What if 'Linux' is indeed my favorite and I enter linux when prompted for?

Which is your Favorite Operating System..?                                                                                                                               
linux                                                                                                                                                                    
I've never used that one!

This is not what we wanted. The pattern matching being case sensitive and the patterns may or may not be regular expressions, we can change the pattern for 'Linux' to [lL]inux, which will match both upper case and lower case 'L', but it won't match LINUX. Please Google about 'Regular expressions' for more details.

Example:

#!/bin/bash

echo "Which is your Favorite Operating System..?"
read os

case $os in

    [lL]inux) echo "Woww!! I am also a Linux Fan!!"
    ;;

    [mM]ac) echo "You must be very Rich!"
    ;;

    [wW]indows) echo "You Should Try Linux Once.. You would love it!"
    ;;

    *) echo "I've never used that one!"

esac

Output:

Which is your Favorite Operating System..?                                                                                                                               
linux                                                                                                                                                                    
Woww!! I am also a Linux Fan!!                                                                                                                                           

Which is your Favorite Operating System..?                                                                                                                               
Linux                                                                                                                                                                    
Woww!! I am also a Linux Fan!!                                                                                                                                           

Which is your Favorite Operating System..?                                                                                                                               
LINUX                                                                                                                                                                    
I've never used that one!

Example - Using another syntax :

#!/bin/bash

echo "Which is your Favorite Operating System..?"
read os

case $os in

    "Linux" | "Ubuntu" | "Linux Mint" | "CentOS") echo "Woww!! I am also a Linux Fan!!"
    ;;

    "Mac") echo "You must be very Rich!"
    ;;

    "Windows") echo "You Should Try Linux Once.. You would love it!"
    ;;

    *) echo "I've never used that one!"

esac

Output :

Which is your Favorite Operating System..?                                                                                                                               
CentOS                                                                                                                                                                   
Woww!! I am also a Linux Fan!!                                                                                                                                           

Which is your Favorite Operating System..?                                                                                                                               
Ubuntu                                                                                                                                                                   
Woww!! I am also a Linux Fan!!                                                                                                                                           

Which is your Favorite Operating System..?                                                                                                                               
Windows                                                                                                                                                                  
You Should Try Linux Once.. You would love it!                                                                                                                           

Which is your Favorite Operating System..?                                                                                                                               
Fedora                                                                                                                                                                   
I've never used that one!

With this, we close our discussion on case statement. In this article, we learned how a basic case statement is written and used with regular expression matching. In the next article, we resume our discussion on loops in bash scripting and learn about select loop. Please share your feedback in the comment section below and stay tuned. Thank you!

Sunday, 19 February 2017

How to Install and Configure Puppet Master and Puppet Agent in Linux

Puppet? What is it?

If you are a system administrator or a DevOps Engineer and you have been maintaining hundreds or thousands of server, you must know the pain of maintaining a consistent configuration on each of the system. An easy way to do that is to maintain a checklist and whenever a server is setup, ensure that every point in the checklist is ticked. But, whenever there is a minor change to be made in the configuration on every server, it becomes a pain in the butts. Go to every server, edit the file, add the line, save the file, restart the service and repeat. By the time you have completed your task, it would already have costed your organization significant amount of money, time and man power, which could have been saved if there were a thing called - 'Automation'.

puppet-installation-configuration

When you think about automating things and especially about maintaining a consistent configuration on hundreds or thousands of servers, one tool you should always know and that is 'Puppet'. Puppet is an open source software, a configuration management tool, written in Ruby language. It is developed and being maintained by Puppet Labs. Puppet works on Linux, Unix, Mac, Windows operating systems and their flavors, may it be physical servers or virtual ones.

In Puppet, with a few lines of code, you can have a consistent configuration on all your servers. You just need to write a code, test it on one server and apply that configuration on all your servers. For this, you have a centralized server, called as 'Puppet Master', which has all the information about all the servers in your infrastructure and the configurations to be applied on them. The other servers which will fetch the relevant information from Puppet master are known as 'Agent Nodes' or simple 'Nodes'.

In this article, we will learn how to install Puppet on the Master node and agent nodes. For this tutorial, we have below setup:
  • Puppet Master - CentOS7 - 192.168.186.223
  • Puppet Agent Node - CentOS7 - 192.168.186.224

Installation of Puppet Master

Note : Before we proceed for Puppet Master installation, ensure that you have configured NTP server properly, in order to set system time correctly.

1. Download PuppetLabs Package

To download the PuppetLabs package, use wget command as shown below:

$ cd ~
$ wget http://yum.puppetlabs.com/puppetlabs-release-el-7.noarch.rpm

2. Install the RPM

To install the downloaded RPM, use rpm command as shown below:

$ rpm -Uvh puppetlabs-release-el-7.noarch.rpm

3. Install Puppet Master

To install Puppet Master, use yum command as shown below:

$ yum install puppet-server

This will install Puppet Master in your system. To verify the same, you can use rpm command as shown below:

$ rpm -qa | grep puppet-server
puppet-server-3.8.7-1.el7.noarch

The output will show the RPM package we have just installed. To check the Puppet Master version, we can use puppet --version command as shown below:

$ puppet --version
3.8.7

4. Start the Puppet Master

Once we have Puppet Master installed, we can start the puppetmaster service right away.

$ service puppetmaster start
# OR
$ systemctl start puppetmaster

5. Add the service to startup

This is just to ensure that, the service puppetmaster is started on every system reboot.

$ systemctl enable puppetmaster

Installation of Puppet Agent Node

1. Download PuppetLabs Package

To download the PuppetLabs package, use wget command as shown below:

$ cd ~
$ wget http://yum.puppetlabs.com/puppetlabs-release-el-7.noarch.rpm

2. Install the RPM

To install the downloaded RPM, use rpm command as shown below:

$ rpm -Uvh puppetlabs-release-el-7.noarch.rpm

3. Install Puppet Agent

To install Puppet Master, use yum command as shown below:

$ yum install puppet

4. Configure Puppet Master in Agent Node

Now that, we have installed Puppet agent on a node. But it should know which Puppet master it should refer to in order to fetch the configurations. For this, we need to use FQDN (Fully Qualified Domain Name) of the Puppet master, which we can get from the DNS server or add the entry in /etc/hosts. In this case, we use the later option.

$ cat /etc/hosts
192.168.186.223         centos-server.example.com
192.168.186.224         centos-client.example.com

Now, in the Puppet configuration file, we must mention which Puppet master it should refer to, for fetching the node-specific configurations. We do this by adding following line in the [agent] section of /etc/puppet/puppet.conf :

$ cat /etc/puppet/puppet.conf
[main]
    ...
    ...
    server=centos-server.example.com
    ...
    ...

5. Start the Puppet Agent

Once we have Puppet Agent installed, we can start the puppet service straight away.

$ service puppetmaster start
# OR
$ systemctl start puppet

6. Add the service to startup

This is just to ensure that, the service puppet is started on every system reboot.

$ systemctl enable puppet

Sign the Certificates

Before a Puppet node can use the configurations present on master, Puppet master must sign the certificate created for the agent node. To view the unsigned certificates, you can run puppet cert list command as below:

$ puppet cert list
  "centos-client.example.com" (SHA256) CC:E8:31:4F:2A:0F:08:36:2E:A7:52:28:B8:84:EC:3F:89:51:6A:88:EB:B4:60:99:42:18:12:CF:B7:48:6F:4A

In order to sign the certificate from centos-client.example.com, run the puppet cert sign command as below:

$ puppet cert sign centos-client.example.com
Notice: Signed certificate request for centos-client.example.com
Notice: Removing file Puppet::SSL::CertificateRequest centos-client.example.com at '/var/lib/puppet/ssl/ca/requests/centos-client.example.com.pem'

Now, we run the puppet cert list command to view if there are any changes.

$ puppet cert list



So, there are no unsigned certificates now, indicating that, we have successfully configured the Puppet master and agent. You can verify the same by running puppet agent -t command on the Puppet agent node.

$ puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for centos-client.example.com
Info: Applying configuration version '1487459767'
Notice: Finished catalog run in 0.02 seconds

Common error

When you run the command puppet agent -t, you might get below error message -

Error: Could not request certificate: Connection refused - connect(2)
Exiting; failed to retrieve certificate and waitforcert is disabled

To resolve this, you should ensure that-
  1. Ping is okay from master to agent and vice versa.
  2. Port 8140 is open on master (netstat -nltap | grep 8140)
  3. You are able to telnet to port 8140 on master, from agent node (telnet puppet-master-ip 8140).
  4. If telnet is not working, you can add following rule to iptables - iptables -I INPUT -p tcp -m tcp --dport 8140 -j ACCEPT


Friday, 13 January 2017

How To: Install or Upgrade to Linux Kernel 4.9.3 in Ubuntu/Linux Mint

The Linux Kernel 4.9.3 is now available for the users, announced Linus Torvalds. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.9.3 in your Ubuntu or Linux Mint system.


Installation

For 32-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-headers-4.9.3-040903_4.9.3-040903.201701120631_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-headers-4.9.3-040903-generic_4.9.3-040903.201701120631_i386.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-image-4.9.3-040903-generic_4.9.3-040903.201701120631_i386.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.3*.deb linux-image-4.9.3*.deb
Reboot the system.
sudo reboot

For 64-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-headers-4.9.3-040903_4.9.3-040903.201701120631_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-headers-4.9.3-040903-generic_4.9.3-040903.201701120631_amd64.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-image-4.9.3-040903-generic_4.9.3-040903.201701120631_amd64.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.3*.deb linux-image-4.9.3*.deb
Reboot the system.
sudo reboot

To uninstall,
sudo apt-get remove 'linux-headers-4.9.3*' 'linux-image-4.9.3*'

How To: Install or Upgrade to Linux Kernel 4.9.2 in Ubuntu/Linux Mint

The Linux Kernel 4.9.2 is now available for the users, announced Linus Torvalds. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.9.2 in your Ubuntu or Linux Mint system.


Installation

For 32-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-headers-4.9.2-040902_4.9.2-040902.201701090331_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-headers-4.9.2-040902-generic_4.9.2-040902.201701090331_i386.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-image-4.9.2-040902-generic_4.9.2-040902.201701090331_i386.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.2*.deb linux-image-4.9.2*.deb
Reboot the system.
sudo reboot

For 64-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-headers-4.9.2-040902_4.9.2-040902.201701090331_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-headers-4.9.2-040902-generic_4.9.2-040902.201701090331_amd64.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-image-4.9.2-040902-generic_4.9.2-040902.201701090331_amd64.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.2*.deb linux-image-4.9.2*.deb
Reboot the system.
sudo reboot

To uninstall,
sudo apt-get remove 'linux-headers-4.9.2*' 'linux-image-4.9.2*'