-
Notifications
You must be signed in to change notification settings - Fork 0
ssh Guide
Following scenario:
You have one PC (let's call it host or server
) and another mobile PC (called guest
), you want to access your PC from your guest
via ssh
.
- Install ssh-server:
sudo apt-get install openssh-server
orsudo pacman -S openssh
- Check ssh-deamon status:
systemctl status sshd
- DEBIAN-base Systems only: Install ssh-client
sudo apt-get install openssh-client
First you need ssh-server installed on server
via:
# For Debian-based systems
sudo apt-get install openssh-server
# For Arch-based systems
sudo pacman -S openssh
Check status of ssh-daemon with:
sudo service sshd status
# should print something with 'Active: active (running)'
# If you use systemd
systemctl status sshd
# Start service via systemd
systemctrl start sshd
On your guest
you should have at least installed a ssh-client using:
sudo apt-get install openssh-client
On guest
you can now connet to server
via ssh:
ssh user@earthUrl
, where user
is a valid username on server
and earthUrl
is the public url for your earth PC.
While testing this command you need to type your password and you will get a ssh-terminal to the server
PC.
If you follow the guide you should exit this ssh session using CTRL+d or typing exit
as command.
In order to not enter your password all the time, ssh
has a public key authentification process.
All following steps have to be done on the guest
system. First you need to generate a public and private key:
ssh-keygen
Your keys are stored in ~/.ssh/
, you were ask to enter a password for your key, it is only needed (for security reasons) if you, e.g. are on a shared PC, but on guest
(your own personal PC) no password should be ok.
After you created the key-pairs, you need to add your public key to server
:
ssh-copy-id user@earthUrl
If everything was succesfully you are now able to ssh without a password to server
, just test it:
ssh user@earthUrl
The file ~/.ssh/authorized_keys
should appear on the host system (server
) which contains one line per key followed by username@computerName
.
-
ssh x-forwarding (but it is slow)
- edit
/etc/ssh/sshd_config
and uncommentX11Forwarding yes
(possibly you have to changeno
toyes
) - restart the ssh service:
systemctl restart
sshd.service` - Now try to start your application:
ssh -X <user>@<hostnameOrIP> thunar
- edit
-
sshfs (install it via:
sudo apt-get install sshfs
):you can mount folders (via
fullPath
) fromserver
aslocalFolder
(must exist) onguest
using:sshfs user@earthUrl:/fullPath localFolder
Assumes that you have google authenticator installed on a mobile device
- Installation:
yay -S libpam-google-authenticator
- Optional (to display the QR code)
sudo apt-get install libqrencode
(orqrencode
)
- Edit
sudo nano /etc/pam.d/sshd
- Add here
auth required pam_google_authenticator.so
at the top
- Add here
- Edit
/etc/ssh/sshd_confg
- Set
ChallengeResponseAuthentication yes
- Set
- Reload sshd service (
systemctl reload sshd.service
) - Generate a key:
google-authenticator
and follow the instructions
Optional: Generate a qrcode (or just type in your secret) manually:
- Install
qrencode
qrencode -o- -d 300 -s 10 "otpauth://totp/YOUR_IDENTIFICATION?secret=YOUR_SECRET" | display
- https://wiki.archlinux.org/index.php/Google_Authenticator
- qrencode command on SO
- https://github.com/google/google-authenticator-libpam
-
Install and setup ssh like shown above
-
Check in your
/etc/ssh/sshd_config
if this line is present:Subsystem sftp /usr/lib/ssh/sftp-server
orSubsystem sftp /usr/lib/openssh/sftp-server
-
If your data should lie in the home directory of the sftp user create the
jail directory
here (otherwise see here; onlyshare
and subfolders will have write access!):sudo chown root:root /home/<username> # It must be owned by root for chroot to work sudo chmod 0755 /home/<username> # Give root full access
-
Create a subfolder (e.g.
share
) within the previously created folder and change the permissions to in order to provide write access:sudo chown root:sftponly /home/<username>/share sudo chmod 0755 /home/<username>/share # Or 0777 if all other users should be access this folder (this can cause a security risk)
-
Optional (depends if you use
Match User
orMatch Group
later on): create a group:sudo groupadd sftponly
-
Add user (within group
sftponly
(optional) and no shell login; last path is the path to the desired directory for your sftp data):sudo useradd -g sftponly -s /usr/bin/nologin -d /home/<uname> <uname>
-
Set a strong (!) password to prevent potential "account is locked" error:
passwd <uname>
(we will disable password logins later) -
Adapt you
/etc/ssh/sshd_config
config:Match Group sftponly ChrootDirectory %h ForceCommand internal-sftp AllowTcpForwarding no X11Forwarding no PasswordAuthentication no
-
Follow steps in Fixing path for authorized_keys (the
echo
part must be done asroot
;sudo
does not work here; see here why; with "'ssh-rsa username@host'" the content of your public key is meant) -
Restart the
sshd.service
:systemctrl restart sshd.service
-
Test your login
Alternatively to step 5 you could so something like that:
topDir permissions: drwxr-x--- user1 group2
└── subDir permissions: drwxrwsr-x user1 group1 subdir
├── file1 permissions: -rw-r--r-- user1 group1 files
└── file2 ...
All members of group1 should be in group2 but not the opposite.
- SFTP_chroot
- SFTP identity file command line
- Convert keys for openssh/putty (PuTTY/WinSCP can convert those files automatically into its own format)
- Permission denied error when writing files
- SFTP cmd line with key
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License *.
Code (snippets) are licensed under a MIT License *.
* Unless stated otherwise