Introduction

This is a collection if various handy commands and shortcuts I’ve discovered while working with Ubuntu, Java, SVN, Maven, Git and other tools and applications at work and at home. They are usually tasks that I don’t do so often that I remember how, but do frequently enough to make it annoying to have to search Google every time. I’ve also discovered a few things that made me go “hey, that was really handy, why didn’t I know that years ago”. So I decided to create this page, partly to share the goodies and partly for my own sanity’s sake. It probably should have been a wiki, and maybe it will some day.

If you know a handy tip or trick for power-users, please get in touch. Thank you!

It’s worth noticing that all Ubuntu tips might work fine in other Linux distributions, but they are only tested on Ubuntu. Some of the commands and configuration changes mentioned below can seriously mess up your system. Always back up the files you plan to edit before you actually make any changes to them.

Ubuntu: Disable a startup of service

update-rc.d -f remove

Maven2: Get Jetty to load log4j.properties

I could not for the life of me get Jetty to load the log4j.properties file - even if it was available in the classpath. The solution was to set the location of the file as a system property when starting Maven.

mvn -Dlog4j.configuration=file:./target/classes/log4j.properties jetty:run

Alternatively, you can configure this in your Jetty configuration on the Maven POM file inside the configuration element of the Jetty-plugin:

log4j.configuration
file:./target/classes/log4j.properties

This way you will not have to add the -Dlog4j system property when starting Jetty from Maven.

Maven2: Debug web application running on Jetty

Handy when you have to debug a web application running on Jetty in Maven. First we need to export a few MVN_OPTS.

export MAVEN_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n"

Then start Maven and Jetty in the normal way.

mvn jetty:run

SNV: View changes from a specific revision to the HEAD revision

(Yes, I’m this old.)

svn log -r  -v

For example

svn log -r 15384 -v

Linux: Check what graphics card is installed in the system

lspci | grep VGA

Ubuntu 10.04: Move the windows buttons back to the right

This tip is Ubuntu 10.04 specific, but it’s very likely that it will also work on 10.10 and any later releases with the windows buttons moved to the left. Here’s how to move them back to the right.

gconf-editor

Find to the folder apps/metacity/general and change the button_layout item value to menu:maximize,minimize,close.

Ubuntu: Control screen brightness on a Lenovo T510

If you have a Lenovo T510 with a Quadro NVS 3100M graphics card, like me, you might have some serious issues with controlling the screen brightness - also just like me. Using the Fn key plus the brightness controls doesn’t do a thing, expect for showing you a screen indication that the brightness should change. To fix it, simply add the following line to the Device section in /etc/X11/xorg.conf:

Option "RegistryDwords" "EnableBrightnessControl=1"

Save and restart X (or simply restart your laptop if you don’t know how to restart X). It’s quite possible that you have to do this every time the NVIDIA drivers are updated, but that’s OK. We still love them for providing top notch drivers for Linux.

Maven2: Building without running tests

Of course you shouldn’t skip running the tests when you build a project, but sometimes you have no choice. Like when someone on your team checked in a code change that breaks the tests and left for a dentist appointment and you can’t figure out what the hell is going on. To skip the tests, add -Dmaven.test.skip=true, like this:

mvn -Dmaven.test.skip=true clean install

Linux: What version of my Linux distribution am I running?

lsb_release -a

Windows: How to modify the routing table

At work we’re currently buried knee deep in testing. During some of the manual tests, we want to simulate network connection problems to see how the application will behave. In a perfect world, this would be a matter of unplugging the network cable, but we’re using terminals connected to virtual Windows servers. So, how to simulate network problems in such an environment? Easy as pie! First, find the IP of the server the application will normally try to connect to. In this case, we use 192.168.123.50. Then you manually corrupt Windows' routing table with the following command.

route add 192.168.123.50 MASK 255.255.255.255 192.168.123.2

Make sure the last IP address is an IP that is on the same subnet as the first IP and that it’s not in use, or at least a server that doesn’t have the same service running as the server your application would normally connect to - or else you will start to rip your hair out. All requests to 192.168.123.50 will now be routed to 192.168.123.2 and you have yourself a good old simulated network problem

To clear up the mess, simply remove the entry from the routing table with the following command.

route delete 192.168.123.50

Linux: How to batch resize images from the command line

For this neat trick, we’ll use ImageMagick. It’s mostly likely already installed on your system, if not you can install it by running this command:

sudo apt-get install imagemagick

When that is done, navigate to the directory containing the images you want to resize. The command below will overwrite the original images, so make sure you have backups before you start! Run the following command to resize all images with an JPG extension to 25% of their original size:

mogrify -resize 25% *.JPG

It should also be possible to substitute 25% with absolute pixel values if that is necessary, like this:

mogrify -resize 640x480 *.JPG

Maven2: Dependency Scopes

I can’t seem to ever remember all the Maven dependency scopes and what they mean. So here they are:

  • compile - This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided - This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  • runtime - This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test - This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
  • system - This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • import (only available in Maven 2.0.9 or later) - This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM’s section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

IntelliJ IDEA: Compilation fails with the message “Process terminated with exit code 3”

The solution to this problem is to give IntelliJ more heap memory for compiling. You can increase this value in Settings | Compiler (source)

Microsoft SQL Server: Removing -2 SPID locks

If you end up with threads in SQL Server that is blocked by another thread with SPID -2 you either have to restart the database server or kill the offending SPIDs with some SQL magic. The latter approach is preferred since it won’t terminate all connection to the database. Note that you need access to master..syslockinfo to do this. First, run the follow query:

SELECT req_transactionUOW
FROM master..syslockinfo
WHERE req_spid = -2
AND req_transactionUOW > '00000000-0000-0000-0000-000000000000'

This will give you the req_transactionUOW of every -2 SPID. Next, kill the transaction lock (I’m not sure what you are actually killing) with the following command:

KILL 'F600C62D-3FC4-4210-9EF2-74FE9DBEF5D6' --Replace the ID with one in your list.

This should release any locks in your database held by a -2 SPID.

Java: SEVERE: WSS1906: Invalid key provided for encryption/decryption

I got this error when working on connecting a Java client to a MCF webservice that required the use of a secure connection, but you might get a similar error when working on other cryptography-related tasks. To solve this you have to install the strong encryption JARs from Oracle. Download from the Java download page. I’ve not included a direct link to the download itself because Oracle tend to change the URLs.

Maven: Increase JVM Heap Size and PermGen Space (Windows)

Open a command line window and type the following:

set MAVEN_OPTS=%MAVEN_OPTS% -Xmx1024m -XX:MaxPermSize=512m

This will set the JVM heap size to 1024 megabytes. Modify the number based on your needs. The %MAVEN_OPTS%-part makes sure that other MAVEN_OPTS that you have defined are not removed.

Java: The Most Common Java Keytool Keystore Commands

Note: If you prefer to use a graphical user interface to edit the keystore, I recommend KeyStore Explorer.

  • Generate a Java keystore and key pair: keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -keysize 2048
  • Generate a certificate signing request (CSR) for an existing Java keystore: keytool -certreq -alias mydomain -keystore keystore.jks -file mydomain.csr
  • Import a root or intermediate CA certificate to an existing Java keystore: keytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks
  • Import a signed primary certificate to an existing Java keystore: keytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks
  • Generate a keystore and self-signed certificate (see How to Create a Self Signed Certificate using Java Keytool for more info): keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
  • Check a stand-alone certificate: keytool -printcert -v -file mydomain.crt
  • Check which certificates are in a Java keystore: keytool -list -v -keystore keystore.jks
  • Check a particular keystore entry using an alias: keytool -list -v -keystore keystore.jks -alias mydomain
  • Delete a certificate from a Java Keytool keystore: keytool -delete -alias mydomain -keystore keystore.jks
  • Change a Java keystore password: keytool -storepasswd -new new_storepass -keystore keystore.jks
  • Export a certificate from a keystore: keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks
  • List Trusted CA Certs: keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts
  • Import New CA into Trusted Certs: keytool -import -trustcacerts -file /path/to/ca/ca.pem -alias CA_ALIAS -keystore $JAVA_HOME/jre/lib/security/cacerts

(Source)

Linux: Check disk space

df -h
ln -s

Windows: How to flush DNS cache

ipconfig /flushdns

DNSMasq: Routing DNS names to local IPs

This can be very convenient if you, like me, host a server on your local LAN and have Apache configured with virtual hosts on different domains. I have my router with DD-WRT and DNSMasq configure in such a way that if I do lookups for any of my domains while I’m on my local network, I get routed to the local IP and not the public IP, which would just screw tings up.

address=/vegard.net/buingo.com/vbox-host.com/192.168.1.3

You can add as many domains as you wish (source).

Glassfish: How to prevent the damn admin console from hanging

This is caused by the admin console looking for updates when you log in. If your server is not connected to the internet, you’re behind a proxy that has not been configured or Oracle’s update servers are down, login in can take ages or never happen at all. Here’s how to get rid of this huge annoyance:

  1. In $GLASSFISH_HOME/glassfish/modules, rename the file console-updatecenter-plugin.jar to console-updatecenter-plugin.jar.old.
  2. Restart Glassfish.

You should now be able to log in quickly no matter the state of your internet connection and Oracle’s servers (source.)

Glassfish: How to enable secure admin

From the command line:

asadmin --host localhost --port 4848 enable-secure-admin

Glassfish: Admin REST services return 400 Bad Request

So, you had everything working in Glassfish 3.0, but after an upgrade to 3.1 all of your requests to the admin REST services return 400 Bad Request? Try to add the following header to your request:

X-Requested-By:GlassFish REST HTML interface

(Source)

Maven: How to build a specific submodule and its dependencies

Example: You have a huge Maven project with plenty of modules that uses intricate internal dependencies but you only want to build parts of it (say, the web services, but not the thick client). Building the correct modules can be a pain and building the entire project to make sure all dependencies are built can take forever. Thankfully, the Maven reactor can analyze your POM files and automagically build only the necessary modules in your project:

mvn -pl -am

Example:

mvn clean install -pl submodule/another-module,submodule2/the-second-module -am

(Source)

Maven: Continue on test failure

mvn -Dmaven.test.failure.ignore

Eclipse: Wipe all cached data from the command line on startup

Using Eclipse is a painful experience, but every now and then you’re forced into using it. Occasionally and without any warning, Eclipse tends to go full “I can’t let you do that, Dave” and refuses not work properly until you wipe its memory. Close Eclipse and run it again from the command line with the -clean flag (source) to get the application to behave again.

eclipse -clean

OpenWrt: Upgrade all packages

Log in to the router as root via SSH, and run the following two commands.

opkg update
opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade

There’s a good chance the router has to be rebooted for some of the updated packages to be effective.

Quickly create favicons for your website

If you need to create favicons, a service like iconifier.net can be quite handy. Upload a picture, and it’ll generate a bunch of different favicons, and provide you with instructions on how to use them.

MySQL: Upgrade AUTO_INCREMENT value

ALTER TABLE picture AUTO_INCREMENT=1972

Setup a private Git server on Ubuntu 18.04

Read this article. Or use this link if the article is gone from the web.

Quickly Show/Hide Hidden Files on macOS Sierra, OS X El Capitan & Yosemite

CMD + SHIFT + . (source)

Set your own custom background during Microsoft Teams video call

  • On Windows PCs:

    • Prepare a custom background JPEG file with size around 1 MB.
    • Place the image in %AppData%\Microsoft\Teams\Backgrounds\Uploads folder
    • Now during video call in Teams, click the background icon. Your new custom background will be listed along with default set of backgrounds.
  • On Macs:

    • Prepare a custom background JPEG file with size around 1 MB.
    • Place the image in /users/<username>/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads folder.
    • Now during video call in Teams, click the background icon. Your new custom background will be listed along with default set of backgrounds.

(source)

Convert ASCII to Hex

…and other free text conversion tools. Binary, BASE64, decimal, ROT13, URL encoded, HTML entities: www.asciitohex.com.

Show the Emoji Keyboard on Mac

CTRL + CMD + SPACE aka control + command + SPACE aka ^ + ⌘ + SPACE depending on what keyboard you´re using.

Show the Emoji Keyboard on Windows

WIN + . or WIN + ;

See all URLs that archive.org has archived from a site

(The Internet Archive, The Wayback Machine)

http://web.archive.org/web/*/www.your-site.net/* (source)

Format USB Drive in Linux Command Line

df -h
sudo umount [device name]
sudo mkfs.vfat [device name]

(Source)

Remap keys in Pop!_OS and Solus

This answer doesn’t really contain enough details to properly swap all keys, it’s more for my own future reference. I wanted to swap two keys, the key for | and Β§ with the key for < and >.

Add the following to ~/.xmodmap-keys:

keycode 0x31 =  less    greater less    greater onehalf threequarters   onehalf threequarters
keycode 0x5E =  bar     section grave   asciitilde      brokenbar       paragraph

Add the following to ~/.config/autostart/swap-keys.desktop:

[Desktop Entry]
Name=Swap keys
Exec=xmodmap /home/HOME_DIR/.xmodmap-keys
Terminal=false
Type=Application

Replace HOME_DIR with your Pop!_OS username. The keys will now swap when Pop!_OS boots.

If this doesn’t work as expected, another possible solution is available in the Ubuntu Community Help Wiki. (Intern Archive mirror).

Change Function Key behavior in Pop!_OS, Ubuntu, and Solus

This section of the document describe how to change the behavior of ‘fn’ key to better match what user expect.

Here a description of each behavior :

  1. 0 = disabled : Disable the ‘fn’ key. Pressing ‘fn’+‘F8’ will behave like you only press ‘F8’
  2. 1 = fkeyslast : Function keys are used as last key. Pressing ‘F8’ key will act as a special key. Pressing ‘fn’+‘F8’ will behave like a F8.
  3. 2 = fkeysfirst : Function keys are used as first key. Pressing ‘F8’ key will behave like a F8. Pressing ‘fn’+‘F8’ will act as special key (play/pause).

Temporarily

The following command will change the behaviour of ‘fn’ key with immediate effect, but restarting will reset the configuration:

echo 2 | sudo tee /sys/module/hid_apple/parameters/fnmode

Permanently in Pop!_OS and Ubuntu

Methods described in this section will change the behavior permanently.

Run the following command to append the configuration line to the file /etc/modprobe.d/hid_apple.conf creating it if necessary:

echo options hid_apple fnmode=2 | sudo tee -a /etc/modprobe.d/hid_apple.conf

Trigger copying the configuration into the initramfs bootfile.

sudo update-initramfs -u -k all

Optionally, reboot.

sudo reboot

Permanently in Solus

This is not working properly because the script runs before the device is ready

Put the following in the file /usr/lib/systemd/system/mac-keyboard.service:

[Unit]
 Description=mac-keyboard
 After=systemd-user-sessions.service plymouth-quit-wait.service
 After=rc-local.service
 Before=getty.target
[Service]
 Type=oneshot
 ExecStart=/bin/sh -c "echo 2 > /sys/module/hid_apple/parameters/fnmode"
 ExecStop=/bin/sh -c "echo 1 > /sys/module/hid_apple/parameters/fnmode"
 RemainAfterExit=yes
[Install]
 WantedBy=multi-user.target

Reload systemd.

sudo systemctl --system daemon-reload

Start the service you just made.

sudo systemctl start mac-keyboard.service

Make sure the service starts automatically when the system boots.

sudo systemctl enable mac-keyboard.service

Upgrade Pi-hole

Pi-hole is a Linux network-level advertisement and Internet tracker blocking application which acts as a DNS sinkhole. You can read more about how it works and how to install and use it in my post How To Install Pi-hole on a Headless Raspberry Pi.

To upgrade your Pi-hole installaton to the most recent version, run the following command:

pihole -up