sshfs – mount files over ssh

Mount a file system on a different computer via ssh:

sshfs -o follow_symlinks user@server:/home/user/ /path/to/mount/point

server is the other computer, user is your username on the other computer and /home/user/ is the folder you want to include from the other computer. /path/to/mount/point is the place on your drive where the files will be located. It needs to be a folder that exists and is empty.

To get rid of the mounted folder again use

umount /path/to/mount/point

How to get WiFi running on Suse Leap 42.3 (Broadcom driver)

After the update from Suse Leap 42.2 to Suse Leap 42.3, my Wifi stopped working. Which is kind of bad, because I need internet to figure out what is wrong…

This was the situation right after the update, when it was not working:

> lspci -nnk | grep -A 3 "Network"
04:00.0 Network controller [0280]: Broadcom Corporation BCM43142 802.11b/g/n [14e4:4365] (rev 01)
        Subsystem: Hewlett-Packard Company Device [103c:804a]
        Kernel driver in use: bcma-pci-bridge
        Kernel modules: bcma
> hwinfo --short
network:
  eth0                 Realtek RTL8101/2/6E PCI Express Fast/Gigabit Ethernet controller
                       Broadcom BCM43142 802.11b/g/n
network interface:
  eth0                 Ethernet network interface
  lo                   Loopback network interface
> iwconfig
lo        no wireless extensions.
eth0      no wireless extensions.
> lsmod | grep "wl"

No WiFi to be seen!

So now this is what I did:

  1. Remove the old driver:
    > rpm -e broadcom-wl broadcom-wl-kmp-default 
    
  2. Find out my exact kernel version (the last part is the part we need, i.e., “default”):
    > uname -r
    4.4.104-39-default
    
  3. Add the Packman repository to my repositories:
    > zypper addrepo http://packman.inode.at/suse/openSUSE_Leap_42.3/ packman
    
  4. Install the drivers, paying attention to my kernel type (…-“default”):
    > zypper install broadcom-wl-kmp-default broadcom-wl
    

    You can also download the rpm by hand and install it. In that case, you need to pay attention to the full kernel number. Meaning, for my kernel 4.4.104-39, I should install the driver from broadcom-wl-kmp-default-6.30.223.271_k4.4.49_19-3.6.x86_64.rpm where the numbers after the k match exactly. Using Packman does that for you.

    Another issue I had with manual installation was missing keys. At least my configuration forces a valid PGP key and aborts if no key is in the key list. And I didn’t have a key for the downloaded rpms. It is possible to tell rpm to install the packages without checking the key (option --nosignature), but that did not properly install the package (without error messages, of course). When installing with zypper it looks for the key itself and you don’t have to worry.

  5. I rebuilt the loaded modules list and then restarted, but I am not sure it is necessary:
    > mkinitrd
    

Finally, the outputs of the above commands are (for reference, the next time it breaks):

> lspci -nnk | grep -A 3 "Network"
04:00.0 Network controller [0280]: Broadcom Corporation BCM43142 802.11b/g/n [14e4:4365] (rev 01)
        Subsystem: Hewlett-Packard Company Device [103c:804a]
        Kernel driver in use: wl
        Kernel modules: bcma, wl
> hwinfo --short
network:
  eth0                 Realtek RTL8101/2/6E PCI Express Fast/Gigabit Ethernet controller
  wlan0                Broadcom BCM43142 802.11b/g/n

network interface:
  wlan0                WLAN network interface
  eth0                 Ethernet network interface
  lo                   Loopback network interface
> iwconfig
lo        no wireless extensions.
wlan0     IEEE 802.11abg  ESSID:"..."  
          Mode:Managed  Frequency:2.412 GHz  Access Point: ...   
          Bit Rate=65 Mb/s   Tx-Power=200 dBm   
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:off
          Link Quality=70/70  Signal level=-39 dBm  
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0
eth0      no wireless extensions.
> lsmod | grep "wl"
wl                   6451200  0 
cfg80211              610304  1 wl

And it only took all afternoon … sometimes I hate Linux 🙁

Setting computer time from the internet [hacky way]

Most of my pool computers show the wrong time and most of them are different. Just for fun, here are the times shown by those running at the moment of the poll:

8:36 (2x), 8:40, 9:36 (2x), 10:35, 10:36 (3x), 10:39 (6x), 11:36 (2x), 11:40

I assume it is the result of setting the time wrong in the installation and then a few semesters of trying to fix some of them (those running at the moment, the first three rows, until the admin was bored, a single one now and then, …), adjusting to daylight savings time or forgetting it and so on.

So this is what I tried to get them back on track (courtesy of AskUbuntu.com):

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"

The line first gets a random web page (here google.com) and prints the header of the HTTP response, e.g.,:

  HTTP/1.1 302 Found
  Cache-Control: private
  Content-Type: text/html; charset=UTF-8
  Referrer-Policy: no-referrer
  Location: http://www.google.de/?gfe_rd=cr&dcr=0&ei=mVYyWqvyKNHPXuKYpeAP
  Content-Length: 266
  Date: Thu, 14 Dec 2017 10:46:49 GMT

The line then retrieves the part of the response with the date using grep. It splits the line with the date at spaces with cut -d ' ' and uses the parts 5 to 8. In this line, part 4 is the day of the week, part 3 is the text Date: and parts 1-2 are empty because of the leading spaces. So using parts 5 to 8 results in a date and time in a format that the tool date can understand. Before passing the time on to date, the letter Z is appended. This Z stands for UTC, meaning the time zone set on the computer will be taken into account.

So the line after evaluating wget, grep and cut for the example page we got will be:

sudo date -s "14 Dec 2017 10:46:49Z"

The option -s sets the date to the specified value. So if the request ran in a reasonable time, we should have a reasonably accurate time set for the computer.

PS: Yes, I know that there is such a thing as NTP and I know that time synchronization is not a problem that you need to hack on your own. But this version is much more freaky and cool!! [Also NTP and the university firewall don’t seem to be friends]

Using GIMP to draw a rectangle

GIMP is not your typical program for drawing, but is is the only thing related to graphics that is installed on my linux. So I have this screenshot and I want to draw a red rectangle around the part that needs to be clicked. This is how:

  1. Open your graphic file with GIMP.
  2. Use the "Rectangle Select Tool" and mark the place where you want your rectangle to be.
  3. Select the color you want to draw the rectangle in as foreground color (in my case that would be red).
  4. In the menu "Edit" choose "Stroke selection".
  5. In the dialogue that comes up, choose "Stroke line" with "solid colour" (it will take the current foreground color), you can adjust the width and if you open up "Line style" you can do more things (e.g., rounded edges).
  6. Click "Stroke" and voila!

Executing a command on a remote server

This is probably a very old hat for Linux-savy people. You can use ssh to execute commands on a remote server, just pass them on as an additional argument:

ssh me@my.server.de "cd bla ; ls ; python test.py "

I use double quotes (“) istead of single quotes (‘) to interpret variables. Different commands are separated with semicolon (;). You can use any command you like, but for some reason when I call some GUI I don’t get the output on the command line until the window is closed.

Open SSH folder in Dolphin using a SSH-key

Dolphin allows you to connect to folders on other machines per SSH, but there is no option to specify a key file. But you can add the key to your general SSH configuration (with the added benefit that you also won’t have to specify the keyfile anywhere else, no more -i on the command line!). This is how it works:

  1. Locate your private key file, say it’s ~/.ssh/myidentity_rsa.
  2. Open or create the file ~/.ssh/config and add the lines
    Host myhost.net
      HostName myhost.net
      IdentityFile ~/.ssh/myidentity_rsa
    
  3. Type fish://user@myhost.net/path/to/folder/ into the location bar.
  4. Now it should ask for the passphrase to your key.

Done!

Get folder/filename from a path

Get folder name, filename, file extension from a path:


# File name: Strip from start longest match of [*/]
FILENAME="${BASEFILE##*/}"
echo "FILENAME $FILENAME"

# Folder: Substring from 0 to start of filename
FOLDER="${BASEFILE:0:${#BASEFILE} - ${#FILENAME} - 1}"
echo "FOLDER $FOLDER"

# File prefix: Strip from end longest match of [dot plus at least one non-dot char]
FILEPREFIX="${FILENAME%.[^.]*}"
echo "FILEPREFIX $FILEPREFIX"

# File extension: Strip from start shortest match of [at least one non-dot char plus dot]
EXTENSION="${FILENAME##[^.]*.}"
echo "EXTENSION $EXTENSION"

Replace newlines with sed

Sed is a commandline linux tool to replace text in a file or input stream. Typically sed works line-oriented, i.e., a line is read, the expression applied, then the next line is read. Say we have a file where one line is one word. We want to reconstruct the sentence. How to replace all linebreaks in the file with a space? Simple:

sed "{:q;N;s/\n/ /g;t q}" 

The regular expression ‘s/\n/ /’ says substitute linebreaks (\n) by a space. ‘g’ says apply this globally. ‘N’ says append the next line to what is processed. Using only ‘N’ would replace linebreaks in every second line. The rest of the thing is a trick to join all lines together. We define the label q (‘:q;’), then we say that in case that there was a sucessfull substitution, go to label q (‘t q’).

Now we have all words in one line. Across sentences! Sentences are separed by an empty line. So easy – replace linebreaks by spaces, replace two adjacent spaces by a linebreak. Gives you one sentence per line, words separated by spaces. Voila:

cat  | sed "{:q;N;s/\n/ /g;t q}" | sed "{s/  /\n/g}"

Which process has an open handle on my file x (fuser, lsof or Process Explorer)?

Here’s how to find out if a file is locked because of another process that still has an open file handle.

On Linux/Unix just use: fuser or lsof

lsof | grep
fuser -v

On Windows the Sysinternals Process Explorer is a great answer to this (and many other questions):

Just Ctrl+F and enter the name or part of it and search