Showing posts with label server. Show all posts
Showing posts with label server. Show all posts

SSH key handling

If you have to work with servers, especially with Linux ones, sooner or later you'll have a confrontation with SSH.

I'll give here some tips and tricks I used so far.

Force user and port for certain servers

When you try to login into a server, by default ssh uses your username and port 22 by default.
But maybe you need to use always another user name or another port.
You could specify this always as parameters for ssh:
ssh -p 22022 [notme@]server-ip

But it is much easier to configure your ssh client to these by default.
Therefore, you just add the following to your ~/.ssh/config file:
host server-ip another-server-name
Port 22022
User notme

host *
User root
You can put here as many hosts with different parameters as you want, '*' is also supported for creating regular expressions.

Change order of authentication methods

There's a another very useful parameter that you might want to add to your server configurations:
host *
User root
PreferredAuthentications publickey,password

This would only allow to use keys or passwords and prevents to use other methods which might not work in your setup and slow down connection attempts.
Put this whenever you notice that it take several seconds to log into a server.

Copy your own key to server

This used to be the first step I do, whenever I access a certain server several times.
So I don't have to give the password each time I access the server.
# Copy my machines public key to the server (will prompt for password):
ssh-copy-id [user@]server-ip

# Unfortunately, ssh-copy-id only works with SSH port 22, so if you have to specify another
# one, you might use this instruction:
ssh-add -L | ssh -p22022 [user@]server-ip "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys"

# Verify the granted access (now without password prompt):
ssh [user@]server-ip
# or when copying
scp afile.txt server-ip:test/ 

Fix non-working ssh public keys

Sometimes, the sshd server doesn't accept a previous copied ssh-key (ssh-copy-id). In that case, make sure you have the following configuration in /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

# If it still doesn't work apply the correct permissions to folders and files:
# chmod go-w ~
# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/authorized_keys

# If it still doesn't work, then disable the permissions checking:
StrictModes no
Restart the ssh daemon again after applying this new configuration.

Debugging and sorting out further problems

The permissions of files and folders is crucial. You can get debugging information from both the client and server. if you think you have set it up correctly, yet still get asked for the password, try starting the server with debugging output to the terminal. /usr/sbin/sshd -d
To connect and send information to the client terminal ssh -v ( or -vv) username@host's

Remove authorized keys

If you have hundreds of keys in your machines ~/.ssh/authorized_keys and you're to lasy to edit that by hand, these one-line shell commands maybe handy.
Just use sed to rip out anything which doesn’t match the regex pattern (for example machine names, part of the hash, whatever:
# rip out anything which doesn’t match the regex pattern
sed ‘/your host name/ ! D’ -i.old ~/.ssh/authorized_keys

# or something more complicated
sed ‘/\(host1\|host2\)/ ! D’ -i.old ~/.ssh/authorized_keys

# with many patterns it is easier with this command
cp ~/.ssh/authorized_keys{,.old} && for p in pat1 pat2 pat2 ; do sed '/$pat1/ ! D' ~/.ssh/authorized_keys ; done

Just the ones specified will be maintained, a backup file will be created.
If you want to do the opposite, remove some specific keys and let the rest untouched:
sed ‘/\(host1\|host2\)/ D’ -i.old ~/.ssh/authorized_keys

Remote Desktops

UltraVNC is a powerful, easy to use and free software that can display the screen of another computer (via internet or network) on your own screen. The program allows you to use your mouse and keyboard to control the other PC remotely. It means that you can work on a remote computer, as if you were sitting in front of it, right from your current location.

UltraVNC runs under Windows operating systems (95, 98, Me, NT4, 2000, XP, 2003...). Its embedded Java Viewer allows you to connect (and make File transfers) from a simple Web Browser on any Operating system supporting Java (Linux, Mac OS...) to an UltraVNC server.

You can connect directly from your Ubuntu desktop with tsclient (Applications --> Internet --> Terminal Server Client).
Previously, you have to ensure you have the VNC Client installed as well:
sudo apt-get install vnc-viewer

Then you can select VNC as connection protocol to connect to your Windows box from Linux.

Tip: Use the special F8 key


Most of the VNC clients have a special mapping for the F8 key. A menu will open and you can
  • send the F8 key to the remote desktop
  • send Ctlr-Alt-Del to the remote desktop
  • change the preferences of the VNC client
  • and other stuff depending on the client

P2P network down-/uploader

mldonkey is a multi-platform, multi-protocol client application.
It includes support for eDonkey2000, Overnet, BitTorrent, Gnutella and Fasttrack.

It can also be used as file downloader, like wget, FlashGet, GetRight.
So if you have a big file for download, just add it and mldonkey takes care of downloading, taking in account your download-limit.

mldonkey installs as a background daemon and does everything without your interaction.
Of course, you'll have to add something to download.
Therefore, we need to access mldonkey somehow, as it acts like a service without GUI.

There are several possibilities, sorted by the easiest to the more complex one:
  1. Install and use a GUI client application. In Ubuntu there comes one called mldonkey-gui. There are others out there which can be found on the web page.
    They communicate with mldonkey-server on the socket 4001 by default.
  2. Use the web interface provided by mldonkey-server.
    Just open your favourite webbrowser on http://localhost:4080.
  3. On the web interface you can do almost everything, but if you need something special just open a shell and issue
    telnet localhost 4000.
    Here you can manage mldonkey-server with a huge set of commands (use longhelp to see all of them).
Okay, doesn't seem to be the most easiest usage.
That's right, but this application is really powerful and gives you access to all download schemas at once.

Some setup notes


  • The Ubuntu package doesn't enable BitTorrent by default, so in the web interface just tick it in Options...Net...enable_bittorrent(true)
  • Where are my downloaded files?
    On Ubuntu they are downloaded to /var/lib/mldonkey/incoming.
    You should change the settings of the shares to your own folders or just stick with it:
    • Create symbolic links in both incoming folders:
      sudo ln -s /home/yourname/incoming /var/lib/mldonkey/incoming/files/yourname
      sudo chown mldonkey.mldonkey /var/lib/mldonkey/incoming/files/yourname
      The same for incoming/directories.
    • In mldonkey enter yourself as new user:
      useradd yourname  mldonkey yourname
    From now on, when you enter mldonkey with your username, mldonkey will copy all your downloaded files to your ~/incoming folder.


Troubleshooting


Once my harddisk got full and somehow mldonkey got stuck in a very unstable state. It shut down, 'cause of missing download space, but thereafter it didn't wanted to start again. There were no explication in the log files, why it inmediately died after launched, even that I had cleaned up space again for downloads. At the end I had to remove some temporary files with this command and everything worked fine again:
sudo rm /var/lib/mldonkey/*.tmp


Other applications


I used lots of applications in the past from BitComet, Azureus, uTorrent and Lphant.
Especially, I liked lphant, because it is able to download from eDonkey and BitTorrent at once, but I wasn't able to get it run on Linux (it's based on .NET 2.0, so even with Mono and Wine it isn't possible).
In the P2P community, the most used application for BitTorrent is uTorrent, a very light-weight and easy to use program.

Switching to mldonkey


When switching from another eDonkey/eMule client in most of the cases you can import the partial downloaded files to mldonkey.
I successfully imported the data from lphant to mldonkey with the import_temp command.
But be careful, the partial downloaded data will be erased from the imported folder, so just in case make a backup first.
In the Wiki there are guides about importing data from BitTorrent clients, but this seems to be quite complicated. So maybe the easiest way is to copy your .torrent files into /var/lib/mldonkey/torrent/incoming and after a while they'll show up in the download list, but starting from 0%.