All posts by admin

How to Use Git

Motivation:

You want to use Git to version your files or share your files with other people.

Solution:
  • Register a GitHub or a GitLab account.
  • Create a GitHub repository or a GitLab project.
  • Download and install a Git client.
  • Generate a personal access token.
  • Pull (checkout) a remote repository (e.g. https://github.com/huybien/asp.net-core.git) to an empty local folder (e.g. C:\Users\admin\Downloads\code):
    git init
    git config user.email "[email protected]"
    git config user.email
    git config user.name "Huy Bien"
    git config user.name
    git config credential.helper ""
    cd C:\Users\admin\Downloads\code
    git remote add origin -f https://github.com/huybien/asp.net-core.git
    git pull origin main
    / * or * /
    git pull origin master
    / * Enter username and token. */
  • Pull (checkout) a remote repository (e.g. https://github.com/huybien/asp.net-core.git) to a local folder that contains existing code (e.g. C:\Users\admin\Downloads\code):
    cd  C:\Users\admin\Downloads\code
    git init --initial-branch=main
    git config user.email "[email protected]"
    git config user.email
    git config user.name "Huy Bien"
    git config user.name
    git config credential.helper ""
    git remote add origin https://github.com/huybien/asp.net-core.git
    git fetch --all
    / * Enter username and token. */
    git add *.*
    git commit -m "new files added"
    git push -u origin main
    / * Enter username and token. */
  • Push local files to a remote empty repository:
    git init
    git config user.email "[email protected]"
    git config user.email
    git config user.name "Huy Bien"
    git config user.name
    # Tell Git to use the Git Credential Manager as its credential helper, which is a tool that securely stores and retrieves credentials (such as usernames, passwords, and personal access tokens) when interacting with remote repositories over HTTPS.
    git config credential.helper manager
    git config credential.username huybien
    git config --global credential.useHttpPath true
    git config --global credential.helper manager
    git add *.*
    git commit -m "first commit"
    git branch -M main
    git remote add origin https://github.com/huybien/asp.net-core.git
    git push -u origin main
  • Push changes to a remote repository:
    git config user.email "[email protected]"
    git config user.email
    git add *.*
    git commit -m "CP form"
    git branch -M main
    git push -u origin main
  • Update (fetch and merge) a local repository:
    git pull origin main
    /* or */
    git branch --set-upstream-to=origin/main main
    git pull
  • Force updating (fetch and overwrite) the current repository:
    git fetch --all
    git reset --hard origin/main
    git clean -fd git pull
  • Force updating (fetch and overwrite) a local repository (e.g. C:\Users\admin\Downloads\code):
    git -C C:\Users\admin\Downloads\code fetch --all 
    git -C C:\Users\admin\Downloads\code reset --hard origin/main 
    git -C C:\Users\admin\Downloads\code clean -fd
    git -C C:\Users\admin\Downloads\code pull
  • Reset (Revert) a local repository to a previous version:
    cd C:\Users\admin\Downloads\code
    git log --oneline
    git reset --hard 4355842
    // where 4355842 is a version id.
  • Remove all cached files:
    git rm -r --cached .
  • Display remote URL:
    git config --get remote.origin.url
  • Discard local changes of file:
    git restore "Features/01_How-to Guides.png"
  • Update GitHub personal access token on Windows:
    # Go to Control Panel, open Credential Manager, click Windows Credentials tab, and remove the related stored Git account if necessary.
    # Tell Git to remember your personal access token so that you don't have to re-enter them every time you push, pull, or fetch from a remote repository.
    git config --global credential.helper manager
    # Tell Git to key credentials by full path (host + owner + repo).
    git config --global credential.useHttpPath true
    # Set explicit username per repo (helps the helper pick the right entry).
    git config credential.username your-personal-username
    # First pull will ask for credentials
    git pull
    # After entering the token, it will be saved for future use.

     

     

     

     

    How to Hide Your IP Location using VPN on Windows 10

    Motivation:

    You have VPN credentials and want to hide your IP location for a specific purpose.

    Solution:

    • Setup a VPN connection using your VPN credentials.
    • Type and click View network connections in Search box to open the Network Connections window.
    • Right-click the VPN connection and select Properties in the menu.
    • Select the Networking tab.
    • Click the Internet Protocol Version 4 (TCP/IPv4) row, then click the Properties button.
    • Click the Advanced… button.
    • Select Use default gateway on remote network option.

    How to Quickly and Reliably Fix a Bug

    Problem:

    You need to fix a bug. However it takes you a lot of effort to fix. The fix may also not be reliable. How can you avoid this situation?

    Solution:
    1. Try to understand the scenario or use case. Ensure that you and the tester are talking about the same thing and agree about the missing or incorrect elements by comparing the final user interface with the original approved use case or user story or specification.
    2. Try to reliably reproduce the bug. It is okay if this attempt may not be successful.
    3. Try isolating the bug in the same or similar environment by using specific, smaller data and fewer settings. It is okay if this attempt may not be done due to your inherent complex software.
    4. Search for an existing solution using error message generated by the system. Include any library or framework name and version, and operating system name and version in search key words. If there is already an existing solution then this attempt can save us a lot of effort.
    5. Try to understand all the concepts in error message.
    6. Debug and log messages to identify the exact location in the source code that causes the issue. In order to to this we need to do the followings.
      • Identifying the flow of the data, i.e. the use case, the entry point and the exit point in the code related to the issue.
      • Trying to understand programming language syntax in the code. Do not guess anything.
      • Trying to understand purpose, inputs and outputs of library functions related to the use case. Again, do not guess anything.
      • Trying to understand data structure and a part of the database schema related to the use case.
      • Trying to review some concrete values inside the database if possible.
      • Trying to understand concepts, algorithms and architecture related to the use case. Again, do not guess anything.
      • These steps may be done in parallel and iteratively.
    7. Guess a cause of the problem based on the information that you can get in the sixth step.
    8. Try to isolate the issue, i.e. try to reproduce the issue using specific code and unit tests, if possible.
    9. Search for or propose a solution for the cause, i.e. propose a fix.
    10. Implement and test the fix.
    11. Repeat from step 5 to step 10 if necessary.

     

    How to Fix a Hacked WordPress Website

    Problem:

    When you visit your WordPress website you are randomly redirected to unwanted websites.

    Verification:
    • Log in your website as an Administrator.
    • Go to Appearance >> Theme File Editor.
    • Click on the Theme Functions link on the right side.
    • Verify if malicious code was injected into the functions.php file. Example of malicious code:
      <?php @ini_set('display_errors', '0'); 
      error_reporting(0); 
      global $zeeta;
    • Download the wp-config.php file to your machine via FTP or SSH.
    • Verify if malicious code was injected into the wp-config.php file. Example of malicious code:
      include_once(ABSPATH . WPINC . '/header.php');
    Solution:
    • Stop the website.
    • Download the whole website to your local machine.
      cd /var/hosting/huybien.com
      ls
      sudo zip -r huybien.zip /var/hosting/huybien.com/html
    Configuration:
    • Log in your website as an Administrator.
    • Change your Administrator’s password.
    • Change file owner and group to www-data:
      sudo chown -R www-data:www-data /var/hosting/huybien.com/html
    • IMPORTANT STEP – Set minimum permissions for folders and files:
      cd /var/hosting/huybien.com/html
      sudo find . -type d -exec chmod 755 {} \; # directory permissions rwxr-xr-x
      sudo find . -type f -exec chmod 644 {} \; # file permissions rw-r--r--
    • Remove all the unused plug-ins or themes.
    • Install, activate and configure a CAPTCHA plug-in to protect Login Form, Registration Form, Lost Password Form, Reset Password Form and Comment Form if there is no one.
    • Disable insecure FTP access if there is one.
    • Install and activate the Simple History plugin to review access to your website. After 1 or 2 days, review the access information, and possibly block the malicious IP addresses using the Windows Firewall.
    • Install, activate and configure Cerber Security plug-in to automatically detect and block the malicious IP addresses.
    • Back up database.
      cd /home/ubuntu
      ls
      mysqldump -u root -p -h localhost huybiencomwp > huybiencomwp.sql
    • Back up files.
      cd /home/ubuntu
      ls
      sudo zip -r /home/ubuntu/huybien.zip /var/hosting/huybien.com/html
    • Download database and files backup.
    • Remove the backups.
      sudo rm -rf /home/ubuntu/huybiencomwp.sql
      sudo rm -rf /home/ubuntu/huybien.zip
      ls

     

    How to Fix “The parameter is incorrect” Issue When Disabling the Sync Host OneSyncSvc Service

    Problem:

    The Sync Host OneSyncSvc service was not started correctly and caused error in the Server Manager.

    You wanted to disable this service. However you got the “The parameter is incorrect” error message when disabling it.

    Solution:
    1. Click on the Search icon, type regedit, press Enter.
    2. Locate the key below
    Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\OneSyncSvc

    3.  Change the Start value from 2 to 4 (Disabled).

    4. Restart the server.

     

    How to Fix the “We can’t sign into your account” Issue in Windows

    Problem:

    You remotely connect o a computer using a Windows Domain account and get the error message below.

    We can't sign into your account
    Solution:
    1. Login the computer as a Local Administrator.
    2. Open C:\Users and delete the folder of the Windows Domain account.
    3. Click Search icon, enter regedit, and press Enter.
    4. Navigate to the path below.
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

    4. Look for the Profile of the Windows Domain account by reviewing the ProfileImagePath value.

    5. Delete the Profile key of the Windows Domain account.

    6. Restart the machine.

    How to Manually Install PHP 7.4 on Windows Server 2019

    Motivation:

    • You want to deploy a PHP application or WordPress on Windows Server 2019.
    • You want to update PHP to any version to address compatibility or security issues in Windows.
    • You want to understand how PHP works with IIS.

    Solution:

    • Install CGI for IIS on Turn Windows features on or off > Internet Information Services > World Wide Web Services > Application Development Features > CGI.
    • Download VC15 x86 Non Thread Safe package here or under PHP 7.4 section from http://windows.php.net/download/
    • Extract the ZIP file to the C:\Program Files (x86)\php-7.4.9-nts-Win32-vc15-x86 folder.
    • Rename the php-.ini-development file to php.ini.
    • Open the php.ini file and add the following line at the end of the file.
      extension=php_wincache.dll
    • Uncomment the following lines
      fastcgi.impersonate = 1;
      
      cgi.fix_pathinfo=1;
      cgi.force_redirect = 1 (and change the value to 0, i.e. cgi.force_redirect = 0)
      
      extension_dir = "C:\Program Files (x86)\php-7.4.9-nts-Win32-vc15-x86\ext"
      
      extension=php_curl.dll
      extension=php_fileinfo.dll
      extension=php_mbstring.dll
      extension=php_exif.dll
      extension=php_mysqli.dll
      extension=php_pdo_mysql.dll
      extension=php_openssl.dll
      
      error_log = "C:\Program Files (x86)\php-7.4.9-nts-Win32-vc15-x86\php_errors.log"
      
      error_log = syslog
    • A sample php.ini file can be download here.
    • Add C:\Program Files (x86)\php-7.4.9-nts-Win32-vc15-x86 to System Path.
    • Download x86 package of WinCache 2.0 for PHP 7.4 here or from https://sourceforge.net/projects/wincache/.
    • Extract and copy the php_wincache.dll file to C:\Program Files (x86)\php-7.4.9-nts-Win32-vc15-x86\ext
      folder.
    • Open IIS, click on Server name, double click on Handler Mappings, click on Add Module Mapping, and enter below information.
      Request path = *.php
      
      Module = FastCgiModule
      
      Executable = "C:\Program Files (x86)\php-7.4.9-nts-Win32-vc15-x86\php-cgi.exe"
      
      Name = PHP 7.4
      
      Request Restrictions = File or folder
    • Download and install VC_redist.x86.exe of Microsoft Visual C++ Redistributable for Visual Studio 2019 here or on https://visualstudio.microsoft.com/downloads/#microsoft-visual-c-redistributable-for-visual-studio-2019
    • en a Command Prompt, execute below command and ensure that NO WARNINGS APPEAR.
      php --version
    • Create phpinfo.php file with below content in the root website folder.
      <?php phpinfo(); ?>
    • Open http://localhost/phpinfo.php URL and verify PHP information.

     

    How to Run Docker on a Windows 10 Hyper-V Virtual Machine

    Motivation:

    You want to have an environment with Docker for development and testing  without interfering your stable machine.

    Context:

    You have enabled Hyper-V, created a virtual machine and install Windows 10 to the virtual machine.
    When installing Docker in the virtual machine, Docker requires that the Nested Virtualization feature must be enabled for the virtual machine.
    You open PowerShell, execute below command in the virtual machine:

    Get-ComputerInfo -property "HyperV*"

    You find that find that the HyperVRequirementVirtualizationFirmwareEnabled feature is not available.

    Procedure:

    1. Download this PowerShell script. The main interesting command in this script is the command below that enables Nested Virtualization for a virtual machine.

    On the host machine execute below command:

    Set-VMProcessor -VMName $vmName -ExposeVirtualizationExtensions $true

    2. Open PowerShell as Administrator and execute 2 commands below.

    Set-ExecutionPolicy -ExecutionPolicy "Unrestricted"
    .\Enable-NestedVm.ps1 "Windows 10 Dev"
    
    "Windows 10 Dev" is the virtual machine name without quotes.

    3. Install Docker. Restart the virtual machine.

    4. Install any additional software if needed, restart the virtual machine again.

    5. Open a Command Prompt and test Docker installation.

    docker pull hello-world && docker run hello-world
    
    Rerun the command if you get an issue in the first run.
    

     

     

    How to Enable Processor Resource Controls in Hyper-V

    Context:

    You got a warning message below when configuring Number of virtual processors in Hyper-V.

    Hyper-V is not configured to enable processor resource controls.

    Problem:

    How do you enable processor resource controls in Hyper-V?

    Solution:

    1. What is the difference between core and logical processor?

    • A socket is a slot contains one or more mechanical components providing mechanical and electrical connections between a microprocessor and a printed circuit board (PCB). This allows for placing and replacing the central processing unit (CPU) without soldering.
    • A core is a physical processor unit (hardware component) inside your processor.
    • Logical processor or logical core is the processor as seen by the operating system. Logical processor does not exist physically.

    Logical Processor = (# of Core) * (# of Thread in each Core) = 4 * 2 = 8

    Example:

    2. What is SMT?

    Simultaneous multithreading, or SMT, is a technique utilized in modern processor designs that allows the processor’s resources to be shared by separate, independent execution threads.

    Processors supporting SMT are available from both Intel and AMD. Intel refers to their SMT offerings as Intel Hyper Threading Technology, or Intel HT.

    3. How does Hyper-V virtualize processors?

    • Hyper-V creates and manages virtual machine partitions, across which compute resources are allocated and shared, under control of the hypervisor. Partitions provide strong isolation boundaries between all guest virtual machines, and between guest VMs and the root partition.
    • The root partition is itself a virtual machine partition, although it has unique properties and much greater privileges than guest virtual machines. The root partition provides the management services that control all guest virtual machines, provides virtual device support for guests, and manages all device I/O for guest virtual machines. Microsoft strongly recommends not running any application workloads in the root partition.
    • Each virtual processor (VP) of the root partition is mapped 1:1 to an underlying logical processor (LP). A host VP always runs on the same underlying LP – there is no migration of the root partition’s VPs.
    • By default, the LPs on which host VPs run can also run guest VPs.
    • A guest VP may be scheduled by the hypervisor to run on any available logical processor. While the hypervisor scheduler takes care to consider temporal cache locality, NUMA topology, and many other factors when scheduling a guest VP, ultimately the VP could be scheduled on any host LP.

    4. What are Hyper-V hypervisor scheduler types?

    Starting with Windows Server 2016, the Hyper-V hypervisor supports several modes of scheduler logic, which determine how the hypervisor schedules virtual processors on the underlying logical processors. These scheduler types are:

    • The classic scheduler provides a fair share, preemptive round- robin scheduling model for guest virtual processors.
    • The core scheduler offers a strong security boundary for guest workload isolation, and reduced performance variability for workloads inside of VMs that are running on an SMT-enabled virtualization host.
    • The root scheduler cedes control of work scheduling to the root partition. The NT scheduler in the root partition’s OS instance manages all aspects of scheduling work to system LPs.

    5. Determine your current Hyper-V Hypervisor Scheduler Type

    Execute the command below.

    Get-WinEvent -FilterHashTable @{ProviderName="Microsoft-Windows-Hyper-V-Hypervisor"; ID=2} -MaxEvents 1

    • 1 = Classic scheduler, SMT disabled
    • 2 = Classic scheduler
    • 3 = Core scheduler
    • 4 = Root scheduler

    6. Enable processor resource controls in Hyper-V by setting Scheduler Type to Core or Classic.

    • Open a Command Prompt as Administrator.
    • Execute the command below.
    C:\Windows\System32\bcdedit.exe /set hypervisorschedulertype Core

    • Restart the computer.

     

     

    How to Convert MP4 or MKV File to MP3 or M4A File

    Motivation:

    You have downloaded some MP4 or MKV video files.

    You want to extract audio parts so that you could copy them to your iPhone to listen to them while turning off the display of your phone.

    Procedure:

    • Download this portable application to your Windows.
    • Unzip the files to a location.
    • Open Mp4ToMp3_64.exe.
    • Click on the Add files button to add MP4 or MKV files to the application.
    • If you want to extract the original audio (for example the M4A format) then select Try extract original audio stream option below the Audio section on the right panel.
    • Click the Convert button.
    • Copy the MP3 or M4A files to your phone.

    This procedure can be used for extracting audio from M4B (Apple audiobook file format) file to M4A or MP3 file too.