Monthly Archives: July 2017
How to create a swap file on Ubuntu
Swap is particularly useful in situations where physical memory is limited, especially when dealing with low memory VPS/systems. This does not mean that swap is useless in all the other situations. Any system with close to 100% RAM utilization without swap enabled will cause application crashes or even worse, a system crash.
Moreover, some applications, in rare cases, require swap to be enabled regardless of memory capacity/usage, otherwise they simply crash.
To enable swap on Ubuntu follow the steps below (requires administration rights):
Create an empty file called “swapfile” using fallocate:
1 | fallocate -l 2G /swapfile |
Feel free to change the “2G” value if you believe you require more or less swap space.
Change the permissions of the swap file to 600 in order to make it accessible only for the root user:
1 | chmod 600 /swapfile |
Mark the “swapfile” as swap space:
1 | mkswap /swapfile |
Enable the swap file:
1 | swapon /swapfile |
Note: It is extremely important to note that any changes we have made so far are temporary and will be removed when we reboot the machine.
In order to make our changes permanent we will need to update the fstab file located at /etc/fstab. You can do so by editing the file and adding the following line inside:
1 | /swapfile none swap sw 0 0 |
Or simply by using this command do insert the line for you in the file:
1 | echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab |
We put together a one line command using the instructions above. It comes in handy when deploying new machines and we want to speed up the process. Feel free to use and modify it to suit your needs.
1 | fallocate -l 2G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile && echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab |
Wake-on-Lan (WoL) in C#
Wake on LAN is a computer networking standard that allows devices to be powered on when they receive a specific network message, if your device supports this feature.
In this example we will demonstrate how to craft this “magic” packet in C#. The concept of the packet is fairly simple. The packet must contain anywhere in the payload six (6) 255 bytes in a row (FF in hexadecimal) followed by the MAC address of the device repeated sixteen times.
As an example, if your device MAC address is 01-00-00-00-00-02 the payload will look like this:
1 | FFFFFFFFFFFF010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002010000000002 |
You can find the implementation of WoL in C# below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | var macAddress = "01-00-00-00-00-02"; // Our device MAC address macAddress = Regex.Replace(macAddress, "[-|:]", ""); // Remove any semicolons or minus characters present in our MAC address var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { EnableBroadcast = true }; int payloadIndex = 0; /* The magic packet is a broadcast frame containing anywhere within its payload 6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal), followed by sixteen repetitions of the target computer's 48-bit MAC address, for a total of 102 bytes. */ byte[] payload = new byte[1024]; // Our packet that we will be broadcasting // Add 6 bytes with value 255 (FF) in our payload for (int i = 0; i < 6; i++) { payload[payloadIndex] = 255; payloadIndex++; } // Repeat the device MAC address sixteen times for (int j = 0; j < 16; j++) { for (int k = 0; k < macAddress.Length; k += 2) { var s = macAddress.Substring(k, 2); payload[payloadIndex] = byte.Parse(s, NumberStyles.HexNumber); payloadIndex++; } } sock.SendTo(payload, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 0)); // Broadcast our packet sock.Close(10000); |
Feel free to leave your questions or feedback in the comment section below.