Skip to content

Instantly share code, notes, and snippets.

@bhubr
Last active April 29, 2021 22:30
Show Gist options
  • Save bhubr/a05905df2c0e58b9f12003ea85591124 to your computer and use it in GitHub Desktop.
Save bhubr/a05905df2c0e58b9f12003ea85591124 to your computer and use it in GitHub Desktop.
Linux tips

Random Linux tips

Shut down an external hard drive

Even if you did unmount all the volumes on an external drive, the drive itself is probably still turned on. This will shut it down:

sudo udisksctl power-off -b /dev/sdb

Switch keyboard layout (X11)

As a French guy, I use an US keyboard (Qwerty) but often have to switch between regular US layout and international US layout.

Switch to regular US:

setxkbmap -layout us

Switch to international US:

setxkbmap -layout us -variant intl

Control external displays

List all displays:

xrandr

On my desktop PC with an HDMI and a VGA monitor, I get this output:

Screen 0: minimum 320 x 200, current 4480 x 1440, maximum 8192 x 8192
HDMI-1 disconnected primary (normal left inverted right x axis y axis)
HDMI-2 connected 2560x1440+0+0 (normal left inverted right x axis y axis) 597mm x 336mm
   2560x1440     59.95*+
   1920x1080     60.00    50.00    59.94  
   1920x1080i    60.00    50.00    59.94  
   1680x1050     59.88  
   1600x900      60.00  
   1280x1024     75.02    60.02  
   1280x800      59.91  
   1152x864      75.00  
   1280x720      60.00    50.00    59.94  
   1024x768      75.03    60.00  
   832x624       74.55  
   800x600       75.00    60.32  
   720x576       50.00  
   720x576i      50.00  
   720x480       60.00    59.94  
   720x480i      60.00    59.94  
   640x480       75.00    60.00    59.94  
   720x400       70.08  
DP-1 disconnected (normal left inverted right x axis y axis)
HDMI-3 disconnected (normal left inverted right x axis y axis)
DP-2 connected 1920x1080+2560+0 (normal left inverted right x axis y axis) 510mm x 287mm
   1920x1080     60.00*+
   1680x1050     59.95  
   1280x1024     60.02  
   1440x900      59.89  
   1280x720      60.00  
   1024x768      60.00  
   800x600       60.32  
   640x480       59.94  
   720x400       70.08  

Hence my displays are HDMI-2 (HDMI) and DP-2 (VGA).

Turn a display off:

xrandr --output DP-2 --off

Turn it back on, to the right of the HDMI monitor:

xrandr --output DP-2 --right-of HDMI-2 --auto

Kill the app which listens own a given port

When you develop with Node.js and interrupt an app (e.g. HTTP server) with Ctrl+C, it sometimes happens that the port it was listening on isn't freed at once. In that case, knowing which port it is, you can kill the process blocking the port like so (here the port is TCP 4000):

fuser -k 4000/tcp

Increase the number of watchers for a "live reload" app

Front-end frameworks, and Nodemon (for Node.js), provide a "live reload" feature that reloads the app when one of its file is modified. This consumes system resources (file watchers). You'll most likely stumble on this error message after a while:

ENOSPC: System limit for number of file watchers reached

This solves the problem once and for all:

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

Clear node_modules (Node.js)

If you're a Node.js developer and are low on disk space, you might want to remove node_modules folders from some projects.

This will clear all of them under the current directory (beware):

find . -name "node_modules" -type d -prune -exec rm -rf '{}' +

Source: https://rtmccormick.com/2018/01/10/clear-node-modules-folders-recursively-mac-linux/

NFS with Linux server and MacOS client

Linux Server - Append this to /etc/exports:

/media/nfs	192.168.1.0/24(rw,sync,insecure,all_squash,no_subtree_check)

Options are well explained here: https://blog.natetodd.com/mount-linux-nfs-share-from-macos-10-13-high-sierra/

Mount on MacOS client (create /media/nfs first):

sudo mount -o resvport,rw -t nfs 192.168.1.67:/media/nfs /media/nfs

Mounting Windows 10 WSL2 virtual disk from Linux

On a dual-boot Windows/Linux computer, you might want to access the WSL2 drive from Linux. It's an ext4 partition inside a .vhdx file. Now how to find it? An official Microsoft tutorial has a section titled Mount a VHD in WSL giving a hint: the ext4.vhdx file is located under C:\Users\[user]\AppData\Local\Packages\[distro]\LocalState\[distroPackageName].

Since my C: drive is mounted as /media/c on my Linux system, here's the actual path:

/media/c/Users/Ben/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc/LocalState/ext4.vhdx

Now, how can I attach a VHDx or VHD file in Linux?. Just follow the instructions!

sudo apt-get install libguestfs-tools
sudo mkdir /media/wsl2
sudo guestmount --add /media/c/Users/Ben/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc/LocalState/ext4.vhdx --inspector --ro /media/wsl2

Only drawback here is I need sudo privileges to just read anything under /media/wsl2! To be continued...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment