We’re spending Christmas break away from Ireland this year, so most of the week has been pre-scheduled posts as I’m offline until we come back.
Want to take a moment and wish you a very happy Christmas! Great time to spend time with family and maybe catch up on RSS and ebook reading on some of the quiet wintery mornings.
Will be back soon, hope you’re all having a great time!
Show Next Few Lines with grep
Now and then I have to find some information in text files – logs, config files, etc. with a unique enough approach: I have a hostname or parameter name to search by, but the actual information I’m interested in is not found in the same line. Simple grep will just not do – so I’m using one of the really useful command line options for the grep command.
Default grep Behaviour
By default, grep shows you just the lines of text files that match the parameter you specify.
I’m looking for the IP address of the home office server called “server” in .ssh/config file.
greys@maverick:~ $ grep server .ssh/config
Host server
So it’s useful in a sense that this confirms that I indeed have a server profile defined in my .ssh/config file, but it doesn’t show any more lines of the config so I can’t really see the IP address of my “server”.
Show Next Few Lines with grep
This is where the -A option (A – After) for grep becomes really useful: specify number of lines to show in addition to the line that matches search pattern and enjoy the result!
I’m asking grep to show me 2 more lines after the “server” line:
greys@maverick:~ $ grep -A2 server .ssh/config
Host server
HostName 192.168.1.55
ForwardAgent yes
Super simple but very powerful way of using grep. I hope you like it!
Now that Jekyll 4 is the default, you simply install jekyll gem and it will get you the latest version of the 4.x branch.
But Jekyll gem install would fail with one of the dependencies like this:
greys@mcfly:~/proj/gleb.reys.net $ sudo gem install jekyll
Password:
Fetching: public_suffix-4.0.1.gem (100%)
Successfully installed public_suffix-4.0.1
Fetching: addressable-2.7.0.gem (100%)
Successfully installed addressable-2.7.0
Fetching: colorator-1.1.0.gem (100%)
Successfully installed colorator-1.1.0
Fetching: http_parser.rb-0.6.0.gem (100%)
Building native extensions. This could take a while…
Successfully installed http_parser.rb-0.6.0
Fetching: eventmachine-1.2.7.gem (100%)
Building native extensions. This could take a while…
Successfully installed eventmachine-1.2.7
Fetching: em-websocket-0.5.1.gem (100%)
Successfully installed em-websocket-0.5.1
Fetching: concurrent-ruby-1.1.5.gem (100%)
Successfully installed concurrent-ruby-1.1.5
Fetching: i18n-1.7.0.gem (100%)
HEADS UP! i18n 1.1 changed fallbacks to exclude default locale.
But that may break your application.
Please check your Rails app for 'config.i18n.fallbacks = true'. If you're using I18n (>= 1.1.0) and Rails (< 5.2.2), this should be
'config.i18n.fallbacks = [I18n.default_locale]'.
If not, fallbacks will be broken in your app by I18n 1.1.x.
For more info see:
https://github.com/svenfuchs/i18n/releases/tag/v1.1.0
Successfully installed i18n-1.7.0
Fetching: ffi-1.11.1.gem (100%)
Building native extensions. This could take a while…
Successfully installed ffi-1.11.1
Fetching: sassc-2.2.1.gem (100%)
Building native extensions. This could take a while…
Successfully installed sassc-2.2.1
Fetching: jekyll-sass-converter-2.0.1.gem (100%)
ERROR: Error installing jekyll:
jekyll-sass-converter requires Ruby version >= 2.4.0.
Which means that we need to upgrade Ruby, easily done with brew.
greys@mcfly:~/proj/gleb.reys.net $ sudo gem install jekyll
Password:
Jekyll 4.0 comes with some major changes, notably:
Our link tag now comes with the relative_url filter incorporated into it.
You should no longer prepend {{ site.baseurl }} to {% link foo.md %}
For further details: https://github.com/jekyll/jekyll/pull/6727
Our post_url tag now comes with the relative_url filter incorporated into it.
You shouldn't prepend {{ site.baseurl }} to {% post_url 2019-03-27-hello %}
For further details: https://github.com/jekyll/jekyll/pull/7589
Support for deprecated configuration options has been removed. We will no longer
output a warning and gracefully assign their values to the newer counterparts
internally.
Successfully installed jekyll-4.0.0
Parsing documentation for jekyll-4.0.0
Done installing documentation for jekyll after 0 seconds
1 gem installed
Standard command like hostname will still work in macOS, but perhaps it’s best to use the native way of updating system information on your Mac? scutil command is here to help.
One of the least used and usually forgotten features in Linux/Unix filesystems, sticky bit is a great way to manage regular user access to a shared directory.
What is a sticky bit?
Sticky bit is a special flag that changes how a particular directory in Unix works. Without this flag, any user that has enough file permissions can remove or rename somebody else’s file in a directory. With sticky bit set, only the original owner of a file can remove or rename it – other users will get permission denied.
IMPORTANT: there’s also an even less popular scenario of using sticky bit for files – but I’ll explain it in a separate post.
How sticky bit for a directory looks
Here’s a directory I just created as myself on my laptop:
greys@maverick:~ $ ls -lad /tmp/try
drwxr-xr-x 2 greys wheel 64 17 Dec 08:33 /tmp/try
I plan on sharing this directory with another user, called unixtutorial. So I’m opening permissions wide (warning! do this only for shared directories that contain no sensitive data):
greys@maverick:~ $ chmod a+rwx /tmp/try
greys@maverick:~ $ ls -lad /tmp/try
drwxrwxrwx 2 greys wheel 64 17 Dec 08:33 /tmp/try
Let’s set the sticky bit for this /tmp/try directory:
greys@maverick:~ $ chmod +t /tmp/try
Here’s how it will look (note how last rwx in permissions changed to rwt – t is the sticky bit):
greys@maverick:~ $ ls -lad /tmp/try
drwxrwxrwt 2 greys wheel 64 17 Dec 08:33 /tmp/try
How sticky bit works
I’m creating a file in the sticky-bit protected directrory /tmp/try that any user on my OS can access for read and write:
greys@maverick:~ $ touch /tmp/try/file1
greys@maverick:~ $ ls -lad /tmp/try/file1
-rw-r--r-- 1 greys wheel 0 17 Dec 08:34 /tmp/try/file1
greys@maverick:~ $ chmod 666 /tmp/try/file1
greys@maverick:~ $ ls -lad /tmp/try/file1
-rw-rw-rw- 1 greys wheel 0 17 Dec 08:34 /tmp/try/file1
… but if I start another terminal session as user unixtutorial, I can’t remove this file even though rw- permissions should allow it:
unixtutorial@maverick:~ $ sudo su - unixtutorial
Password:
unixtutorial@maverick:~ $ cd /tmp/try
unixtutorial@maverick:/tmp/try $ rm /tmp/try/file1
rm: /tmp/try/file1: Permission denied
unixtutorial@maverick:/tmp/try $ ls -la /tmp/try/file1
-rw-rw-rw- 1 greys wheel 0 Dec 17 08:34 file1
If as my original user greys I remove the sticky bit from /tmp/try:
greys@maverick:~ $ chmod -t /tmp/try
greys@maverick:~ $ ls -ald /tmp/try
drwxrwxrwx 2 greys wheel 64 17 Dec 08:36 /tmp/try
… I can now remove the file as another user:
unixtutorial@maverick:/tmp/try $ rm /tmp/try/file1
unixtutorial@maverick:/tmp/try $ ls -al /tmp/try/file1
ls: /tmp/try/file1: No such file or directory
Sticky Bit Clarifications
You can’t apply sticky bit to a user or group, as you would with other file access permissions. It works on a file or directory level, not user or group level.
This is invalid: chmod u+t
This is correct: chmod +t
Sticky bit only controls regular users – super user root can still remove any files in sticky-bit protected directories, even owned by other users
That’s it for today. Hope you have learned something new!
When it comes to reporting basic OS information in one terminal screen for easy screenshot, I’ve been using screenFetch most of the time. Turns out, it’s not the only software of this kind – and neofetch is a great alternative.
neofetch or screenFetch?
Notice how I didn’t say netofetch VS screenFetch!
Because mostly the choice between neofetch and screenFetch is a personal preference: both tools are quite capable and available on majority of popular operating systems.
Reasons to use neofetch
Compared to screenFetch, neofetch has a few really cool features:
Has it been a year since VirtualBox 6.0 release already? Time flew! This week brought us the first major release of the VirtualBox 6.x family, with lots of improvements – traditionally focus is on performance and stability.
Implemented support for importing a VM from Oracle Cloud Infrastructure – VirtualBox 6.0 previously introduced exporting VMs into the same cloud – this is now a complete workflow
New style 3D support (VBoxSVGA and VMSVGA) – old style using VBoxVGA is gone
Support YUV2 and related texture formats with hosts using OpenGL (macOS and Linux), which accelerates video playback when 3D is enabled by delegating the color space conversion to the host GPU
Virtualization core: recompiler is gone, meaning full CPU hardware virtualization is required now
vboximg-mount: Experimental support for direct read-only access to NTFS, FAT and ext2/3/4 filesystems inside a disk image without the need for support on the host – sounds like you can use Windows to run a VM that would access Linux filesystems on attached storage – pretty cool!
I’m quite happy with Parallels Desktop on my macOS systems, but install VirtualBox among the first 5 apps on any Linux laptop or desktop. Have upgraded to 6.1 on my Dell XPS already, will post more screenshots soon!
I’m finishing up the VM based Ubuntu 19.10 setup and decided to make a few screenshots to test new workflow. Instead of random topic, I documented the task of configuring Ubuntu to use static IP address.
Why use Static IP?
Desktops don’t benefit from this much, but servers benefit from having static (fixed) IP addresses because you configure DNS once and then use server hostname and same fixed IP address for using any services.
Even if all you need is connect to server via SSH, static IP address will make things much easier – plus it usually comes as default for most dedicated hosting plans.
In my home lab I prefer to configure most virtual machines with static IP addresses because this allows me to easily access them remotely (working from one of the laptops).
Things to Know Before Configuring Static IP
Assuming you’re going to assisg a static IP from your local network (LAN), you need to have the following information confirmed:
what’s the local network addressing in use? It most likely starts with 192.168 or 10.0, but you need to know for sure. You need the full network – so something like 192.168.0.x or 192.168.1.x
static IP address you plan to use (from the same network)
make sure this IP is available – currently not in use (and at least does not answer to ping)
default route (sometimes called gateway) address – this is usually the IP address of your broadband modem. In my case it’s 192.168.1.1
DNS server IP address – usually the same as your gateway IP
you can also easily use external DNS server like 1.1.1.1 (CloudFlare) or 8.8.8.8 (Google)
Configure Ubuntu 19.10 for Static IP
Here are the screenshots walking you through the static IP configuration:
Go to app listing:
… choose Settings:
and select Network:
Inspect current settings (they would be automatic IP address and DNS details via DHCP):
…and now go to IPv4 settings:
Now, select Manual option for IPv4 method:
…and provide new values:
Now click Apply (top right) and in the next screen turn the Wired network off:
Now turn it back on and you can review the result:
That’s it – it’s a rather simple procedure but I still find it more comfortable (and easier to manage) when I update configuration directly or via Ansible.
Back to basics day: a very simple tip on working with one of the most useful things available in Unix and Linux filesystems: symbolic links (symlinks).
greys@mcfly:~/proj/unixtutorial $ ls -la total 435736 -rwxr-xr-x 1 greys staff 819 14 Dec 2018 .gitignore drwxr-xr-x 3 greys staff 96 20 Nov 09:27 github drwxr-xr-x 4 greys staff 128 20 Nov 10:58 scripts lrwxr-xr-x 1 greys staff 30 10 Dec 20:40 try -> /Users/greys/proj/unixtutorial drwxr-xr-x 44 greys staff 1408 20 Nov 10:54 wpengine
Show Symlink Destination with readlink
As with pretty much everything in Unix, there’s a simple command (readlink) that reads a symbolic link and shows you the destination it’s pointing to. Very handy for scripting:
It has a very basic syntax: you just specify a file or directory name, and if it’s a symlink you’ll get the full path to the destination as the result.
If readlink returns nothing, this means the object you’re inspecting isn’t a symlink at all. Based on the outputs above, if I check readlink for the regular scripts directory, I won’t get anything back: