windows

Start privilege Linux daemon when windows boot

Windows 10 WSL

With WSL, you can install many Linux distros in Windows, e.g. officially Ubuntu, Ubuntu1804, Debian, … and unofficial like Archlinux.  To start a backgroud linux daemon when Windows start, you need

  1. Recent version of  Windows 10 (e.g. 1803, or maybe 1709 but not tested)
  2. Enable Windows subsystem for Linux
  3. Install a linux distro from Windows store (e.g. Ubuntu1804)

To test, create a simple script in your home folder, e.g. /home/user/testscript.sh


#! /bin/bash
while [ 1 ]
do
 echo `date` >> /mnt/c/tmp/test.txt
 sleep 10
done

Now at Windows schedule task, crate a new task, with

command=bash

argument = -c /home/username/testscript.sh

Modify this task to “Run whether user is logged on or not”.

wsl-roottask_wintask1

wsl-roottask_wintask2

Here is the output

wsl-roottask

Reboot you windows machine to see if the task is started or not.

Run Linux system daemon in Windows

Warning: You should understand completely before doing the following because it allows normal user to run as root.

You can start any task, even “cron” can be started without problem.  But if you want to start cron, you need a special trick, setuid.  This is because normal user cannot start privilege daemon like /usr/sbin/cron.

The command to start ‘cron’ in Windows task scheduler is similar,


bash -c /etc/init.d/cron start

But before you do it, you must setuid of the task, /usr/sbin/cron (login as root, chmod u+s /usr/sbin/cron).  You may ask some Linux guys on how to limit which user(s) can run the ‘cron’ daemon.

Alternatively, you can modify the sudo configuration, e.g. /etc/sudoers.d and add the default user there so that the default user don’t need password to sudo the cron daemon.

I create the following cron task


* * * * * /bin/bash -c "echo hello from cron" >> /mnt/c/tmp/test.txt

You already notice the tail output screen above consists of this cron task.

Advertisements

Native OpenSSH on Windows 10

Just found accidentally, OpenSSH server as well as client are available in Windows 10 Professional 1709 (Fall Creators Update).

You can enable it under Settings -> Apps & features -> Manage optional features -> Add a feature -> OpenSSH Client (Beta) and OpenSSH Server (Beta).

win10-openssh-install

Remember to turn off Windows firewall for ssh (port 22) to access your machine remotely using ssh and/or putty.

win10-openssh

Snappy Ubuntu Core on Hyper-V

Do you want to test drive the Snappy ubuntu core on hyper-v? Here are some simple steps.

1. Download the ova file from the official ubuntu website

https://developer.ubuntu.com/en/snappy/start/#ova
http://cloud-images.ubuntu.com/ubuntu-core/15.04/core/stable/current/core-stable-amd64-cloud.ova

2. Use 7-zip (Windows) or tar (Linux) to untar the ova file
3. Use a image file converter (like virtualbox) to convert the vmdk file to vhd file.

e.g. VBoxManage.exe clonemedium core-stable-amd64-cloud-disk1.vmdk core-stable-amd64-cloud-disk1.vhd –format VHD

4. You can use standard cloud-init way to setup the system (not discuss here) or;

5. mount the VHD file in any linux machine, modify the /etc/shadow (in partition 3) to clear the root password;

6. Create a ubuntu-core machine in hyper-v and mount the vhd image.

7. Start the machine and enjoy.

Little Windows Batch to get yyyymmdd (year, month, day of month)

Do you need to write a windows batch and need to get yyyymmdd?  It is a nightmare if you deploy your batch file to multiple machines (e.g. different windows version, different locale setting, etc).

Unlike linux, windows build-in date command will output different format for different Windows version (e.g. xp, 2003, 2012) or different localized version.  And the user can also customize the output format.

In the old days, I’ll write a little program to do this.  But now there is a little tool, yyyymmdd.bat, https://github.com/litalidev/yyyymmdd which seems to work on different Windows versions.

Openwrt in Hyper-V

Update: 2017-11-21 The below procedures also work in LEDE 17.01.4

Want to add a openwrt machine in hyper-v for testing?  Sadly, the openwrt did not include the hyper-v device driver for the network device.

(Un)luckily there is a legacy network driver in hyper-v that can be used in openwrt.

Steps to add openwrt machine inside hyper-v:

  • In hyper-v, add a machine with 64-128MB ram, legacy network device.
  • Copy the openwrt image to the virtual hard disk.
  • Mount the virtual hard disk (mount /dev/sda2 /mnt) and copy the tulip driver to the hard disk.
  • Boot the openwrt machine.
  • Inside the openwrt machine, add the tulip driver (cd /; opkg install kmod-tulip_3.3.8-1_x86.ipk)
  • ifconfig -a to check the name of the network device.
  • Modify /etc/config/network as usual and restart the network (or reboot).