Ryan Harrison My blog, portfolio and technology related ramblings

WSL - Creating a shortcut to Windows Documents

Due to the fact that you should never change Linux files using Windows apps and tools when running the WSL, I find myself during the vast majority of work in my Windows documents area and then accessing them directly via Bash.

Your Windows drives are accessible via /mnt using the same drive letters as found in Windows explorer - e.g to access your Windows Documents area via Bash you can cd to:

/mnt/c/Users/<username>/Documents

This works just fine, but it’s a bit long winded when most of your frequently accessed files are located there (plus it looks ugly having such a long path in your prompt all the time). Thankfully, the process gets much simpler through the use of symlinks. Using the following command you can create a symlink pointing directly to your Windows Documents folder:

ln -s /mnt/c/Users/<username>/Documents ~/docs

You can then just do a quick cd ~/docs and end up right in your Windows home directory. The great part is that your prompt will also just show ~/docs instead of the full path via /mnt. I have a number of these symlinks set up, pointing to various places in my Windows drive, and they make the whole experience when using Bash much more fluid.

Read More

Ubuntu - Clear Disk Cache

The Linux kernel often uses available memory for disk caching unless its otherwise needed by a program running on the system. This feature can speed up the system, but the RAM will be marked as used in the top command. It might seem that you are out of memory and need to do something to resolve the problem, when in fact everything is normal and the kernel will simply give programs resources from the disk cache when required. It never takes resources away from programs, just makes use of idle memory sitting in your system. This is such a common question that it even has its own website - http://www.linuxatemyram.com/.

If you really want to clear the disk cache and free the memory, run the following command:

sudo sync && sudo echo 3 | sudo tee /proc/sys/vm/drop_caches

Note: there isn’t much point though, as the kernel will just start using the memory again for disk caching straight after you free it.

Read More

FFmpeg - Convert Video to MP3

FFmpeg can easily be used to extract the audio tracks from virtually all video files and save them to new audio files:

Copy audio track from video file:

This example assumes that the video’s audio track is already an MP3 so can be simply copied over without re-encoding.

ffmpeg - i video.mkv -acodec copy audio.mp3

Convert audio track to MP3 (CBR):

This example assumes that the video’s audio track is something other than MP3 (or whatever target format you want). FFmpeg will re-encode the audio track so you can also specify some additional quality options.

ffmpeg -i video.mkv -b:a 192K -vn audio.mp3

In this command we specify the that the bit rate of the new audio file will be a constant 192kb/s (CBR).

Convert audio track to variable bit rate MP3 (VBR)

This command creates a variable bit rate (VBR) MP3 instead of the CBR file as in the above example. This will reduce the size of the new MP3, but may sacrifice some quality.

ffmpeg -i video.mkv -q:a 0 -map a audio.mp3

Read More

Ubuntu Server Setup Part 1 - Logging In

In this set of posts I’ll be running through setting up a new Ubuntu server - from initial setup and security through to installation of some of the most common packages. You can get your very own server/VPS through companies such as DigitalOcean or OVH - in this case we’ll be using Ubuntu Server 18.04, but the same concepts should apply to previous and future releases as well.

In this post we’ll be covering how to login to your server for the first time through SSH and creating a new user with root privileges.

Logging in as Root

When you create your new server, you should receive an email with directions on how to login to your server. This will normally include the public IP address of your server and a randomly generated password which will allow you to login as the main root user. The root user is the main administrator in a Linux system and so has privileges to do pretty much anything. You therefore generally don’t want to be logged in as root for day-to-day operations where you can potentially cause damage quite easily. We do however need to login as root initially (as at this point we don’t have any other users).

In this post we’ll be using PuTTY as an SSH client, however all other alternatives will work just as well. Inside your initial email you should have the public IP of your server along with the root password. In the PuTTY client window enter the IP address into the Host Name field and press ‘Open’ (assuming SSH is selected and the port is set to the default 22).

PuTTY

You should be greeted with a black window prompting you for the user your wish to login as - in this case ‘root’. Enter the root password when prompted. As this is the first time you are logging in, you will also be prompted to change the default root password (make sure to choose something strong).

PuTTY Login Screen

If you see a new bash shell, then you have successfully logged in to your server and you can move on to setting up your own new user.

PuTTY Bash Shell

Creating a New User

As I said before, working as root is generally considered a bad idea. Instead, we will be creating a new user which we will be logging in as in all future sessions.

$ adduser demo

This command will create a new user called ‘demo’ (change the name to be something more relevant). You will be prompted for a new password (again choose a strong one) along with some general information for the new user (you can skip this be pressing Enter).

Granting Root Privileges

We have just created a new user, but by default they have very limited privileges which prevents them from installing new packages or modifying any system files. Obviously for us this is not ideal, so instead we can grant the new user ‘sudo’ privileges, which allows us to run commands as root without having to actually be logged in as root. To grant these privileges, run the following as root:

$ gpasswd -a demo sudo

or

$ sudo usermod -aG sudo demo

You can now run commands as root by simply adding sudo to the beginning - essentially meaning that you never need to login as root anymore. For additional security we can also completely disable root login through ssh (which stops a lot of brute force attacks) which will be covered in a future post.

Now we have a new user setup, we still need to do some more work to better secure our server. In the next post we will set up public key authentication which is a much more secure method than the simple password we have at the moment.

Read More

Bash - Redirecting stdout and stderr

Redirect stdout/stderr to a truncated file:

# StdOut
cmd > out.txt # (stderr printed to console)
# StdErr
cmd 2> err.txt # (stdout printed to console)

Redirect stdout/stderr to a file (appending):

# StdOut
cmd >> out.txt # (stderr printed to console)
# StdErr
cmd 2>> err.txt # (stdout printed to console)

Redirect both stdout and stderr in same command to different files (truncating):

cmd > out.txt 2> err.txt

Redirect both stdout and stderr to same output (truncating):

cmd > out.txt 2>&1

“Redirection statements are evaluated, as always, from left to right. >> file - STDOUT to file (append mode) (short for 1>> file) 2>&1 - STDERR to ‘where stdout goes’ Note that the interpretation ‘redirect STDERR to STDOUT’ is wrong”

Source

Read More