alan

linux

This is one very, very, very interesting piece of ancient wisdom.

Suppose you need to change permissions for some directories. Maybe the permissions are too strict, e.g. something like 700 for directories and 600 for files, and you need to give access to some other group and not just the owner.

As you know, execute permission on directories is required for traversal. So you might think doing something like:

chmod -R g+rx /var/somedirectory

This would probably work, but would leave you in a sad state: you'll get the executable bit set on files that are not supposed to be executable at all, which may be just annoying but may get risky as well.

A great alternative is:

chmod -R g+rX /var/somedirectory

the capital X means: set the executable bit only if the target a) is a directory b) has already at least one executable bit set for any one of user, group, others.

There's another alternative that may be appealing in some situations, at least when you want to give the group the same permissions as the owner:

chmod -R g=u /var/somedirectory

This will give any file the group permissions identical to the user's permission.

#MostlyUnixish #linux #ancientwisdom #chmod

I was tired of repeating the same old pattern. Send a kill signal. Check with pgrep. Did the process exit? Send a kill -9.

Now all my knowledge has been permanently dumped here:

Kill Me Softly on GitHub

If you want to kill a process by letting it to exit gracefully, and yet kill it without mercy if it's too slow to exit, that's your new favourite shell script.

You can discuss this on Hacker News as well

#MostlyUnixish #linux #processmanagement #kill

A desktop freeze is something that's not so rare nowadays – that's not good, but we shouldn't get desperate about that.

I like Compiz with Unity Desktop on my Ubuntu Trusty very much; I feel it's a step forward compared to traditional desktops. The only major nuisance I had on the previous versions was the inability to disable the global menu (which I like by itself, but I don't like the global menu disappearing when the mouse is not over it), but you can now disable it and have the menu on the window – even though you can't still enable an always-visible behaviour.

The problem is: compiz sometimes gets buggy. Running a different window manager, like metacity and/or xcompmgr just doesn't seem good, you can experience other issues and you're using the stack in a way it wasn't designed for. I don't like that approach.

So, what can you do when your desktop freezes? The symptoms are usually very clear: you can move your mouse, but you can't click nor type anything.

You don't need to reboot nor to restart X. You can fix this without losing your work.

Get a raw tty

Press Ctrl+Alt+F1 and get to a raw linux console, and login with your credentials. Make sure you use the same user you're logged into X with.

Find and kill your stuck process

pgrep compiz

This will print the PID of our stuck window manager.

Let's kill it

pkill compiz

This won't usually be enough because the process is really stuck: if you repeat the pgrep command above, you'll find it still running – i.e. pgrep will return the very same PID as above.

So let's kill it, really this time!

pkill -9 compiz

Then wait some 15-20 seconds.

Now, if you repeat the pgrep , you'll either see a) no result or b) a different PID.

If you see a different PID, it's Ubuntu that noticed the crashed process and relaunched it; press Ctrl+Alt+F7, and you should just get back a working desktop!

Relaunching

If you see nothing with pgrep, you'll need to relaunch Compiz manually.

You cannot simply relaunch compiz on the console, because it must connect to X: the proper way to do it is:

DISPLAY=:0 compiz

Now switch back to your X instance with Ctrl+Alt+F7 and enjoy!

#MostlyUnixish #linux #ancientwisdom #community #ubuntu #compiz

I seem very ancientwisdom-oriented these days.

Sometimes you've got a system user on your linux server that's used to run a specific service; it's a good practice to employ those users to achieve privilege separation, since it limits the impact of many security breaches. It's security 101, really.

If you've got a system user, most probably its shell would be /bin/false or /bin/nologin. Again, this is a good practice, since it would be harder for an attacker to gain shell access to your machine through that user.

But I find that many people just change such shell, because sometimes they need to customize the environment for a user, and if you try doing something like

su - systemuser

you'll find yourself just back to your original shell, because su tries to invoke the shell which is set for the target user, and sudo doesn't generally help here, because its --shell option doesn't start a proper login shell for that user (most of the environment is that of the calling user).

Very well, gentlemen: this is a wrongdoing and a potential security hole. You should never enable a shell for your system users. Instead, you should leverage an option of su:

su - systemuser --shell /bin/bash

That lets you choose your favourite shell on a per-launch basis.

#MostlyUnixish #linux #ancientwisdom #su #security

This is another old trick, but I like to tell the world about such amenities.

I often find people writing command lines such as:

ssh arcane-system-user@someextremelylong.andimpossibiletotype.anderrorprone.hostname.com

or, worse

ssh idliketorememberwhichuser@10.20.30.40

or maybe it was

ssh someuserimashamedof@1.2.3.4

yes, sometimes ssh is simply tedious.

But you can do better. In fact, you can do much better.

your ~/.ssh/config or /etc/ssh/ssh_config can contain Host configuration sections; I have already blogged about such directives when I told the world about stopping key verification compliants – they're very flexible, indeed.

Consider adding Host directives in your configuration for your most used services:

Host myniceserver
    Hostname myniceserver.mylove.com
    Port 12345
    User teddybear

And then you can just

ssh myniceserver

You can override most options on a per-server basis and/or select a certain private key for connections.

#MostlyUnixish #linux #ssh #ancientwisdom

This is a very old trick, but I find many people doesn't know about it yet.

If you're using sudo (and you definitely should), one minor complaint is that you need to re-enter your password quite often.

One common pseudo-solution is to completely disable the password for executing sudo with such user; while it may be a good idea sometimes, it does decrease the overall security – if you happen to forget an open shell or screen/tmux lingering somewhere, you give total power to whoever gets in touch with it.

If you happen to let people into your sudo-enabled account with your ssh key, anybody that can get hold of your ssh key will get root access to your servers. It's not the best possible scenario.

There's a very simply workaround: just edit your sudoers file!

sudo visudo

and add a line like that (or modify it if it's already there)

Defaults    timestamp_timeout=120

This means that sudo will ask for a password every two hours instead of the default (15 minutes on most installations). You can use -1 in order to never let it expire (I don't suggest that – 8-10 hours should be enough for everybody.)

The only drawback is that such timeout is per shell, not system-wide – i.e. if you open multiple terminals, you'll have to enter the password again for each new terminal instance.

#MostlyUnixish #linux #sudo

It might be high time to update to the latest Ubuntu 14.04.

I think that the upgrade process that is provided by Canonical is slightly underoptimal: if you're using a local mirror which can provide a very fast download of all packages, they'll usually be disabled during the upgrade process, and an official mirror (possibile one very far away) will be employed.

This can lead to a very slow and painful upgrade process.

The solution is easy.

The following instructions regard moving from 12.04 (precise) to 14.04 (trusty)

Switch your sources

Assuming that your official ubuntu sources are configured from /etc/apt/sources.list just do something like:

sudo sed -i.old -e 's/precise/trusty/g' /etc/apt/sources.list

I'm not considering other sources in /etc/apt/sources.list.d, since they'll be disabled whatsoever; but if you happen to have ubuntu sources there as well, you should repeat the SEDding process for each file.

Update your index

sudo apt-get update

Download updated packages without installing

sudo apt-get dist-upgrade --download-only

Revert to old sources

sudo mv -f /etc/apt/sources.list.old /etc/apt/sources.list

Update index again

sudo apt-get update

Rejoice!

Now you can just follow the upgrade instructions and almost nothing will be downloaded during the upgrade, because everything is already on your hard drive.

#MostlyUnixish #ubuntu #linux #upgrade

Sometimes when performing your daily apt-get update, you'll experience messages like

E: The package lists or status file could not be parsed or opened.

or

The package index files are corrupted

Such messages usually don't go away for quite a long time, and may prevent legitimate updates from installing.

This usually happens even more frequently if using a proxy to access apt archives.

While I don't know what's the root problem, I've got a quick workaround that seems to fix the immediate error:

sudo -s -H bash -c 'apt-get clean && rm -f /var/lib/apt/lists/* ; rm -f /var/lib/apt/lists/partial/* && apt-get clean && apt-get update'

#MostlyUnixish #linux #ubuntu #debian #deb #apt

If you develop enough software, most probably you'll have a test/development network at your service.

In such networks, which are usually handled through a virtualization infrastructure, machines come and go very quickly. But ssh clients are usually unhappy about that:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
The authenticity of host '192.168.0.208 (192.168.0.208)' can't be established.
RSA key fingerprint is 31:4d:8b:97:c8:57:04:85:6a:1b:72:54:46:ab:04:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.208' (RSA) to the list of known hosts.
root@192.168.0.208's password: 

The first time you connect, you're asked if the fingerprint is correct. The second time you'll just be allowed to connect:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
root@192.168.0.208's password: 

What then if the machine at such ip address changes, because it's rebuilt or modified and the ssh host key is not retained?

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
7b:42:03:5f:5c:8d:5a:2b:6a:a4:1e:d6:0d:05:de:38.
Please contact your system administrator.
Add correct host key in /Users/alan/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/alan/.ssh/known_hosts:111
  remove with: ssh-keygen -f "/Users/alan/.ssh/known_hosts" -R 192.168.0.208
RSA host key for 192.168.0.208 has changed and you have requested strict checking.
Host key verification failed.

From the point of view of openssh this may be a serious security breach, and you should fear such message if it happens in the wild if you haven't touched a server - a MitM attack is probably going on!

What about your local network? ssh-keygen -R 192.168.0.208 will solve that, but you probably didn't need host key authentication since the beginning.

So, just add this:

Host 192.168.0.*
    CheckHostIP no
    UserKnownHostsFile /dev/null
    StrictHostKeyChecking no

at the beginning of your /etc/ssh/ssh_config file, and feel good.

Now, anytime you try connecting inside your network you'll be greeted by this:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
Warning: Permanently added '192.168.0.208' (RSA) to the list of known hosts.
root@192.168.0.208's password: 

No confirmations whatsoever.

#MostlyUnixish #linux #ssh

Whenever you use Thunderbird on Linux you may encounter one annoying behaviour.

If you open a message which is got an attachment – you just need to open the message, not to save the attachment! – Thunderbird will place such attachment in our temporary directory, usually /tmp. You find the file there, and you think you used the Save as function to place it there, so you open it and you edit it.

Then you close Thunderbird, and poof! It clears its temporary files and deletes your modified attachment, possibily containing a day's work.

The workaround is simple: just use a launcher that changes Thunderbird TMP directory and use that to start Thunderbird!

#!/bin/bash -e
mkdir -p /tmp/thunderbird
TMP=/tmp/thunderbird /usr/bin/thunderbird "$@"

You may need to adjust the original binary path, then you can put such script in your path before your /usr/bin directory and you're done.

#MostlyUnixish #linux #thunderbird #temporary