Pour générer le fingerprint (md5 ou sha256) de votre clé ssh publique
Vous pouvez faire :
ssh-keygen -lf ~/.ssh/id_ed25519.pub
ou
ssh-keygen -l -E md5 -f ~/.ssh/id_ed25519.pub
C’est pratique pour faire la correspondance entre clé publique en local sur son poste et le fingerprint dans l’interface de Github ((https://github.com/settings/keys)
j'avais perdu cet article, il est là!
not bad! better than unprotected socket access ;)
Solution 1 (recommended)
Apple updated its Technical Notes to indicate that since 10.12.2, macOS includes version 7.3p1 of OpenSSH and its new behaviors.
In ~/.ssh create config file with the following content:
Host * (asterisk for all hosts or add specific host)
AddKeysToAgent yes
UseKeychain yes
IdentityFile <key> (e.g. ~/.ssh/userKey)
Encore un exemple d'utilisation de env= dans le authorized keys
via skunnyk
variables=~/.ssh/variables
sshadd() {
source "$variables" > /dev/null
ssh-add -l > /dev/null 2>&1
case "$?" in
1)
ssh-add /root/.ssh/key > /dev/null 2>&1
;;
2)
rm "$variables"
sshagent
;;
esac
}
sshagent() {
if [ -f "$variables" ] ; then
sshadd
else
ssh-agent -s > $variables
sshadd
fi
}
sshagent
source /root/agentmanagement.sh
.ssh/authorized_keys
environment="GIT_AUTHOR_NAME=Arnaud M",environment="GIT_AUTHOR_EMAIL=arnaud@foo.bar",environment="GIT_COMMITTER_NAME=Arnaud M",environment="GIT_COMMITTER_EMAIL=arnaud@foo.bar" ssh-rsa .....
Peut être pratique quand plusieurs personnes commit depuis le même serveur (pour avoir un historique git avec les noms..)
To manage multiple identities
It took me some time to figure this one out, as everybody is using rsync and ssh-keys without passphrases, but I insist that an ssh-key should have a passphrase.
In my first attemts I got this error messages mailed to me by crontab:
Permission denied (gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive).
Here are the steps to automate a backup initiated from crontab using rsync, SSH and ssh-keys with a passphrase:
Make a set of SSH keys.
Setup SSH to use the agent automatically.
Login once as the user who's cron will run the backup script. You will be asked for a passphrase. When the machine reboots, you will need to login once more, to enter the passphrase again.
Make a backup script that includes some SSH variables.
This script could be as simple as this:
. /home/username/.ssh/variables
rsync -avz --delete /data/ example.com:data
N.B. This variables file only contains these lines:
SSH_AUTH_SOCK=/tmp/ssh-DmFcb18036/agent.18036; export SSH_AUTH_SOCK;
SSH_AGENT_PID=18037; export SSH_AGENT_PID;
echo Agent pid 18037;
Put that script in crontab.
That should do it for you, as it works like a charm for me!
variables=~/.ssh/variables
sshadd() {
source "$variables" > /dev/null
ssh-add -l > /dev/null 2>&1
case "$?" in
1)
ssh-add > /dev/null 2>&1
;;
2)
rm "$variables"
sshagent
;;
esac
}
sshagent() {
if [ -f "$variables" ] ; then
sshadd
else
ssh-agent -s > $variables
sshadd
fi
}
sshagent
-R [bind_address:]port:host:hostport
ssh -R *:8080:localhost:80 -N root@website.com
or
ssh -R 0.0.0.0:8080:localhost:80 -N root@website.com
or
ssh -R [::]:8080:localhost:80 -N root@website.com
Note that if you use OpenSSH sshd server, the server's GatewayPorts option needs to be enabled (set to yes or clientspecified) for this to work (check file /etc/ssh/sshd_config on the server). Otherwise (default value for this option is no), the server will always force port bound on the loopback interface only.
errs = []
def test_server(errs, server)
lambda do |options|
begin
server =~ /(.+)@(.+)/
server_name, user = $2, $1
puts server_name
s = Net::SSH.start(server_name, user)
s.close
s = server
rescue Errno::EHOSTUNREACH, SocketError
puts "echec connexion #{server} : #{$!}"
errs << server
s = nil
end
return s
end
end
Net::SSH::Multi.start(:on_error => :warn) do |session|
session.use &test_server(errs, 'root@srv-04')
session.use &test_server(errs, 'root@srv-07')
session.use &test_server(errs, 'root@srv-08')
session.use &test_server(errs, 'root@fail-08.local')
session.exec( "hostname" )
session.loop
end
unless errs.empty?
puts "The command failed on #{errs.size} hosts"
end
Dans les logs d'accès ssh (/var/log/auth.log), si vous avez passé le loglevel de SSH à VERBOSE, vous pouvez voir pour chaque accès la clé qui a été utilisé.
Le format est différent de celui dans authorized_keys pour des raisons de lisibilité j'imagine.. Pour faire la conversion depuis authorized_keys vers ce format :
echo key | base64 | md5
ou
ssh-keygen -lf key.pub
Trouver tous les pid des sessions ssh en cours :
ss -tnp|grep ':22'
ss -tp|grep ':22'|grep -Eo ',[0-9]+,'|grep -Eo '[0-9]+'
Trouver depuis quand ces process sont démarrés :
26205 329840
1419103787
Sat Dec 20 20:31:42 CET 2014
One liner :
start time
for pid in $(ss -tp|grep ':22'|grep -Eo ',[0-9]+,'|grep -Eo '[0-9]+'); do date -d@$(echo "$(date +%s) - $(ps -o etimes -p $pid --no-headers)"|bc); done
pid + start time
Si le log level de ssh est en VERBOSE, on peut chercher dans auth.log le fingerprint de la clé publique correspondant à la date à laquelle une session ssh a démarré :
Je préfère cette solution, à savoir passer sshd en VERBOSE.
L'autre solution consiste à utiliser environment dans le fichier authorized_keys puis à écrire dans un fichier (à l'aide de .bashrc par exemple). Mais l'utilisateur peut modifier ce fichier..
ssh-keygen -lf /path/to/public_key_file
wow could be useful
via skunnyk
petit rappel
cheatsheet bien faite
Bon ce code n'a pas marché chez moi... mais il faut bien garder en tête qu'une clé chargée avec un agent va résider en mémoire de manière non chiffrée ;)
Et de la même manière, elle va résider en mémoire sur un hôte distant si on se connecte en 'ssh -A'. Donc potentiellement, quelqu'un qui a un accès root sur cette machine distante peut récupérer les clés privés des gens qui s'y connectent (en -A)
github link via skunnyk
Quelques pistes pour monitorer le TCPforwarding
This morning a discussion with a friend about various shells lead me to think it would be nice if my bash shell could tab complete hostnames from .ssh/known_hosts when I type ‘ssh <tab>’. I soon found this blog post which nicely documents how to do it. I made a directory in $HOME called .bash.completion and then added this to my .profile, which loops round any files in there, sourcing them individually:
if [ -d ${HOME}/.bash.completion ]; then
for file in ${HOME}/.bash.completion/* ; do
source $file
done
fi
All sorted. However, it wasn’t long before I discovered that ‘ssh user@<tab>’ doesnt work, I tend to use this quite a lot so wanted to see if I could fix up the bash function to support that use case. Bit of hacking around and I’ve got it working, the replacement ssh-completion file is shown below:
__ssh_known_hosts() {
if [[ -f ~/.ssh/known_hosts ]]; then
cut -d " " -f1 ~/.ssh/known_hosts | cut -d "," -f1
fi
}
_ssh() {
local cur known_hosts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
known_hosts="$(__ssh_known_hosts)"
if [[ ! ${cur} == - ]] ; then
if [[ ${cur} == @ ]] ; then
COMPREPLY=( $(compgen -W "${known_hosts}" -P ${cur/@/}@ -- ${cur/*@/}) )
else
COMPREPLY=( $(compgen -W "${known_hosts}" -- ${cur}) )
fi
fi
return 0
}
complete -o bashdefault -o default -o nospace -F _ssh ssh 2>/dev/null \
|| complete -o default -o nospace -F _ssh ssh
Une astuce utile pour faire un audit des connexions par clé sur un compte (quand on a plusieurs personnes avec des clés différentes qui accèdent au meme user)
Implémentation rapide :
#On convertit le authorized_keys pour ajouter la variable d'env
cp ~/.ssh/authorized_keyz ~/.ssh/authorized_keyz.ORIGINAL
IFS=$'\n'
for line in $(cat .ssh/authorized_keys)
do
comment=$(echo $line|cut -d' ' -f3)
echo "environment=\"SSH_USER=$comment\" $line" >> ~/.ssh/authorized_keyz.COMMENT
done
cp ~/.ssh/authorized_keyz.COMMENT ~/.ssh/authorized_keyz
Puis dans le bashrc de l'user, ajouter ça :
if [ "$SSHUSER" != "" ]; then
now=$(date +%Y-%m-%d%H-%M-%S)
echo $now : User $SSH_USER logged in >> ~/ssh-audit.log
fi
Ahah a tester
alternatives à authorized_keys
via Skunnyk
Cool stuff :
ssh user@server 'bash -s' < local_script.sh > local_script.log 2>&1
not bad
$ sudo /usr/sbin/sshd -t
$ echo $?
In Firefox, the solution is easy. Simply type about:config in the address bar and set network.proxy.socks_remote_dns to true. This will have the remote end (i.e., the machine you are SSH’ing to) handle the DNS lookups.
Un serveur, mais surtout un client pour faire du port knocking
Pour utiliser machinerebond pour accéder aux *.vm automatiquement :
$ cat .ssh/config
Host=machinerebond
Hostname=machinerebond.fqdn.com
User=root
Host=*.distant
User=root
ProxyCommand=ssh -W %h:22 machinerebond
Alternative :
D'abord se co sur la machine rebond avec un -A pour ramener sa key dans l'agent distant
Ensuite on peut se co sur les autres machines, mais plus long.
Autres directives possibles :
ServerAliveInterval 30
ServerAliveCountMax 120
Port 22000
User fooey
IdentityFile ~/.ssh/github.key
LocalForward 9906 127.0.0.1:3306
more : http://www.openbsd.org/cgi-bin/man.cgi?query=ssh_config&apropos=0&sektion=0&manpath=OpenBSD+Current&arch=i386&format=html