Thursday, 16 May 2024

Ssh to linux machine without using password



To SSH connect to a linux machine, a raspberry pi in my example from a PC without using a username and password, you can set up SSH key-based authentication. This method involves generating a public and private key pair on your PC and copying the public key to the Raspberry Pi. Here’s a step-by-step guide to do this:

Step 1: Generate SSH Key Pair on Your PC

  1. Open a terminal on your PC.

    • On Linux or macOS, open the terminal.
    • On Windows, you can use PowerShell or Git Bash.
  2. Generate the key pair:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    • The -t rsa -b 4096 options specify the type of encryption (RSA) and the key size (4096 bits).
    • The -C option adds a comment (usually your email) to the key for identification purposes.
  3. Follow the prompts:

    • When prompted to "Enter file in which to save the key," you can press Enter to accept the default location (usually ~/.ssh/id_rsa).
    • Choose whether to set a passphrase. A passphrase adds an extra layer of security, but for passwordless login, you can leave it empty by pressing Enter.

Step 2: Copy the Public Key to the Raspberry Pi

  1. Transfer the public key:

    • Use the ssh-copy-id command to copy the public key to your Raspberry Pi. Replace pi@raspberrypi with your actual username and Raspberry Pi hostname or IP address:
      ssh-copy-id pi@raspberrypi
    • If you haven't changed the default user, pi is the username, and raspberrypi is the hostname. If you've changed them, use your custom values.
  2. Enter the password:

    • You will need to enter the password for the Raspberry Pi user one last time. After this, the public key will be added to the ~/.ssh/authorized_keys file on the Raspberry Pi.

Step 3: Connect to the Raspberry Pi Using SSH

  1. Initiate the SSH connection:
    ssh pi@raspberrypi
    • You should now be able to connect without being prompted for a password.

Step 4: (Optional) Adjust SSH Configuration for Convenience

  1. Edit your SSH config file:

    • Open the SSH config file in a text editor:
      nano ~/.ssh/config
    • Add the following configuration:
      Host raspberrypi HostName raspberrypi User pi IdentityFile ~/.ssh/id_rsa
    • Adjust HostName and User according to your setup.
  2. Save and exit:

    • Press Ctrl+X to exit, Y to confirm changes, and Enter to save.
  3. Now you can simply connect using:

    ssh raspberrypi

Troubleshooting Tips

  • Ensure SSH is enabled on the Raspberry Pi: You can enable SSH via the Raspberry Pi configuration tool or by placing an empty file named ssh (without any extension) on the boot partition of the SD card.
  • Correct file permissions: Ensure that your .ssh directory and files have the correct permissions:
    chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa chmod 644 ~/.ssh/id_rsa.pub chmod 600 ~/.ssh/authorized_keys

By following these steps, you should be able to SSH into your Raspberry Pi from your PC without needing to enter a username and password each time.

Thursday, 1 December 2022

Generate SQL script from entity framework using powershell/visual studio


to run the below command , start PowerShell in visual studio. Select the database project.

In PowerShell:

 Script-Migration

this command will generate script from blank database to the latest migration.


Script-Migration migrationname1

this command will generate script from migration named migrationname1 to the latest migration.


Script-Migration migrationname1 migrationnamex

this command will generate script from migration named migrationname1 to migrationnamex



Monday, 28 November 2022

Adding custom prerequisite software to Visual studio installer project

 One of my application require asp.net runtime to be installed in the client system to run the application. Visual studio installer project doesn't have option to add Microsoft ASP.NET Core 6.0.11 Shared Framework (x64) to the project pre-requisite screen in visual studio.



The list of Prerequisite is loaded from configuration available in 

C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\BootstrapperPackages

Or on the location where visual studio is installed. :D



Solution:

note: Microsoft ASP.NET Core 6.0.11 Shared Framework (x64) needs to be installed in the system so that we can get some info from control panel.


  1) clone a folder and rename it to "aspnet6coreruntime_x64"

2) remove all the files/folders other than these:

    



3)  modify package.xml to : 

<?xml version="1.0" encoding="utf-8" ?>

<Package xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper" Name="DisplayName" Culture="Culture">


  <!-- Defines a localizable string table for error messages-->

  <Strings>

    <String Name="DisplayName">Microsoft ASP.NET Core 6.0.11 Shared Framework (x64)</String>

    <String Name="Culture">en</String>

    <String Name="AdminRequired">You do not have the permissions required to install the ASP.NET Core 6.0 Runtime (v6.0.11). Please contact your administrator.</String>

    <String Name="InvalidOS">Installation of the ASP.NET Core 6.0 Runtime (v6.0.11) (x64) is supported only on x64 machines.</String>

    <String Name="GeneralFailure">A failure occurred attempting to install the ASP.NET Core 6.0 Runtime (v6.0.11).</String>

    <String Name="InvalidPlatformWinNT">Installation of the Microsoft ASP.NET Core 6.0 Runtime (v6.0.11) is not supported on this operating system.</String>

  </Strings>

</Package>


4) modify product.xml to :

<?xml version="1.0" encoding="utf-8" ?> 


<Product xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper" ProductCode="Microsoft ASP.NET Core 6.0.11 Shared Framework (x64)">


  <!-- Defines list of files to be copied on build -->

  <PackageFiles CopyAllPackageFiles="false">

    <PackageFile Name="aspnetcore-runtime-6.0.11-win-x64.exe"

                 HomeSite="https://download.visualstudio.microsoft.com/download/pr/e874914f-d43d-4b61-8479-f6a5536e44b1/7043adfe896aa9f980ce23e884aae37d/aspnetcore-runtime-6.0.11-win-x64.exe"

                 PublicKey="0" />

    <PackageFile Name="NetCoreCheck.exe" />

  </PackageFiles>


  <!-- Run the NetCoreCheck tool that will determine if the necessary framework is installed -->

  <InstallChecks>

    <ExternalCheck Property="NetCoreCheck" PackageFile="NetCoreCheck.exe" Arguments="Microsoft ASP.NET Core 6.0.11 Shared Framework (x64)"/>

  </InstallChecks>


  <!-- Defines how to invoke the setup for the .Net Runtime 6.0 -->

  <Commands Reboot="Defer">

    <Command PackageFile="aspnetcore-runtime-6.0.11-win-x64.exe" Arguments=' /q '>


      <!-- These checks determine whether the package is to be installed -->

      <InstallConditions>

        <!-- Block install on any platform other than x64 (Arm64 will usually work too) -->

        <FailIf Property="ProcessorArchitecture" Compare="ValueEqualTo" Value="Intel" String="InvalidOS" BeforeInstallChecks="true"/>

        <FailIf Property="ProcessorArchitecture" Compare="ValueEqualTo" Value="Arm" String="InvalidOS" BeforeInstallChecks="true"/>

        <!-- Block install on less than Windows 7 RTM -->

        <FailIf Property="VersionNT" Compare="VersionLessThan" Value="6.1.0" String="InvalidPlatformWinNT"/>

        <!-- NetCoreCheck returning 0 means the runtime is already installed -->

        <BypassIf Property="NetCoreCheck" Compare="ValueEqualTo" Value="0"/>

        <!-- Block install if user does not have admin privileges -->

        <FailIf Property="AdminUser" Compare="ValueEqualTo" Value="false" String="AdminRequired"/>

      </InstallConditions>


      <ExitCodes>

        <ExitCode Value="0" Result="Success"/>

        <ExitCode Value="3010" Result="SuccessReboot"/>

        <DefaultExitCode Result="Fail" FormatMessageFromSystem="true" String="GeneralFailure" />

      </ExitCodes>


    </Command>

  </Commands>

</Product>


4.1) packageFile is the name of the file downloaded from microsoft site: https://download.visualstudio.microsoft.com/download/pr/e874914f-d43d-4b61-8479-f6a5536e44b1/7043adfe896aa9f980ce23e884aae37d/aspnetcore-runtime-6.0.11-win-x64.exe

4.2) HomeSite is the url of downloadable location. 
4.3) Arguments is the name of the installed library which can be found in control panel. 



5) Now restart visual studio and open the project solution. 
6) 

Thursday, 17 February 2022

Raspberry pi: how to find raspberry pi model details

In the raspberry pi console type: 

 pinout

output:

,--------------------------------. | oooooooooooooooooooo J8 +==== | 1ooooooooooooooooooo | USB | +==== | Pi Model 3B V1.2 | | +----+ +==== | |D| |SoC | | USB | |S| | | +==== | |I| +----+ | | |C| +====== | |S| | Net | pwr |HDMI| |I||A| +====== `-| |--------| |----|V|-------' Revision : a02082 SoC : BCM2837 RAM : 1024Mb Storage : MicroSD USB ports : 4 (excluding power) Ethernet ports : 1 Wi-fi : True Bluetooth : True Camera ports (CSI) : 1 Display ports (DSI): 1 J8: 3V3 (1) (2) 5V GPIO2 (3) (4) 5V GPIO3 (5) (6) GND GPIO4 (7) (8) GPIO14 GND (9) (10) GPIO15 GPIO17 (11) (12) GPIO18 GPIO27 (13) (14) GND GPIO22 (15) (16) GPIO23 3V3 (17) (18) GPIO24 GPIO10 (19) (20) GND GPIO9 (21) (22) GPIO25 GPIO11 (23) (24) GPIO8 GND (25) (26) GPIO7 GPIO0 (27) (28) GPIO1 GPIO5 (29) (30) GND GPIO6 (31) (32) GPIO12 GPIO13 (33) (34) GND GPIO19 (35) (36) GPIO16 GPIO26 (37) (38) GPIO20 GND (39) (40) GPIO21


This commands returns few important information like Model and revision of the raspberry pi, Memory, SoC, Wifi/Bluetooth, and Pin ports detail.

Wednesday, 29 September 2021

How to push changes to github after password authentication is deprecated

I tried to check-in some code to my github repository after more than 6 months today. I found that normal username and password for authentication does not work anymore. Instead I had to use Personal access token (PAT).

There's a simple step to generate PAT. Keep the code secure in a secure place as it will be required to push the change to the git. 

Create a token in github:

1) login to github site. 

2) on the top right corner, Click user icon.

3) on the dropdown menu,  select Settings.

4) click on Developer settings 

5) Select Personal Access token. 

6) Click Generate new token. And follow the instruction. 

7) select required permissions. 

Use token to push code:

1) Open console and locate the source folder. Run the code below: 

git push https://<GITHUB_ACCESS_TOKEN>@github.com/<GITHUB_USERNAME>/<REPOSITORY_NAME>.git
Example: 
git push https://ghp_N4dLdh4feZyO2F4XIt8NufA3056jSU2qlkoi@github.com/sanjeeb/myproject2.git

Saturday, 11 September 2021

simple use of Record in c#9 and newer versions

 c# 9 introduces a new keyword Record which makes an object immutable. Properties in the record can be initialised at the time of creation or constructor call only. Record can be written like how we write a class. It has same syntax as class.

For example:

public record Friend

        {

            public Friend()

            {

            }

            public Friend(string name, string surname)

            {

                this.Name = name;

                this.Surname = surname;

            }

            public string Name { get; init; }

            public string Surname { get; init; }

        }

use of Friend record:

var newFriend = new Friend("Tom", "Pandey");

//--Note: we called here using constructor.

var newFriend1 = new Friend{Name="Tom",Surname= "Pandey"};

//Note: this calls the constructor without parameter. constructor without parameter is not required to to call to initiate the record. 

This record is equivalent to below code which don't have a constructor:

public record Friend

        {

            public string Name { get; init; }

            public string Surname { get; init; }

        }


use of Friend Record:

var newFriend = new Friend("Tom", "Pandey");

Note: we called here using constructor, but we don't have constructor defined. so It will throw an error. Instead we create object friend like this:

var newFriend1 = new Friend{Name="Tom",Surname= "Pandey"};

In both the example of Record, we can not modify the property Name or Surname after the object is created. 

The Friend record with constructor can be written as below code in one line:

public record Friend(String Name, String Surname);

use of Friend Record: 

Friend friend = new Friend("John","Pandey" );

Note: since this is constructor type record, we must create record object using constructor.for example below code will throw error:

var newFriend1 = new Friend{Name="Tom",Surname= "Pandey"};

On compilation, this is converted to class with immutable properties.

init only property in c#9 or later versions

 c# 9 introduces new Init-Only property that allow to make immutable properties in a class. This means the property with "init" keyword in place of "set" keyword allows the property to be initialized at the construction step of an object. it doesn't allow you to set the value later, once the object is initialised. 

For example

 public class Friend

        {

            public Friend(string name, string surname)

            {

                this.Name = name;

                this.Surname = surname;

            }

            public string Name { get; init; }


            public string Surname { get; init; }

        }


Use of Friend

   public void SomeMethod()

        {

            Friend friend = new Friend("John", "Sharma");

            var newFriend = new Friend("Tom", "Pandey");

        }

Use of init property after initialization throws an error. for eg.

 public void SomeMethod()

        {

            Friend friend = new Friend("John", "Sharma");

            var newFriend = new Friend("Tom", "Pandey");

            friend.Surname = "xxx"; //<--- Error here.

        }

Create a Directory from existing disk in proxmox server

Scenario:   I have an NVMe disk with an ext4 partition that was previously used as a directory in a Proxmox server. After reinstalling Proxm...