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) 

How to use User-secret to protect secrets in dotnet application.

    Using user secrets in a .NET Web API project to securely manage your database password is an excellent practice. It keeps sensitive info...