Allowing access to the IBM i via SSH using passwords is a bad practice. This allows for people to brute-force their way into your system. This article will explain how to lock down SSH by disallowing passwords and root login, and setting up public keys to be used instead. To keep this article short and to the point, I assume the reader has a basic knowledge of SSH. The SSH Essentials article from Digital Ocean is a good read to become familiar with SSH otherwise.
I suggest setting up at least one user with a key before disallowing logins via password.
Creating a local SSH key for the user
- Follow steps 2-4 of Github’s Generating a new SSH key article.
- Step 3: Just press enter when it prompts for the file name, and it will default to id_rsa.
- Step 4: Just leave the passphrase blank, unless you want to type a passphrase every time the key is used. This is a another authentication layer.
- The local public key can now be found in
C:\Users\[username]\.ssh\id_rsa.pub
on Windows and/home/[username]/.ssh/id_rsa.pub
on most Unix systems. This is the key that will be put on the IBM i to allow the user to SSH in.
Adding a key to a user on the IBM i
- If it doesn’t already exist, create
/home/[username]/.ssh/authorized_keys
on the IBM i. This file will hold all the keys that allows that user access. It can hold as many keys as one wants, each on a new line. So, for example, one could have their home, work, laptop, and other PC keys in this file. - Edit the
authorized_keys
file and add/append the public key. The public key is the entire contents of theC:\Users\[username]\.ssh\id_rsa.pub
file created on the user’s local machine. - Make sure the permissions are correct by running the following commands on the IBM i through either bash, or qp2term:
chown -R [username] /home/[username]
– Gives ownership of the user’s folder to the user.chmod 751 /home/[username]
– Sets the correct permissions for the home folder.chmod 700 /home/[username]/.ssh
– Gives owner read, write, and execute permissions.chmod 600 /home/[username]/.ssh/authorized_keys
– Gives owner read and write permissions.
Disallow logging in with passwords & root login
Change the following parameters found in /QOpenSys/QIBM/UserData/SC1/OpenSSH/openssh-[latest]p1/etc/sshd_config
:
PermitRootLogin yes PasswordAuthentication yes PermitEmptyPasswords no
to
# Lock the server SSH down to only allow key login PermitRootLogin no # Do not permit the root user to login via SSH PasswordAuthentication no # Do not allow passwords. PermitEmptyPasswords no # Do not allow empty passwords.
and make sure they are uncommented. This will require a restart of *SSHD to take effect.
Done!
You should now be able to SSH into the IBM i using the key system. Passwords will no longer be allowed, so if someone tries logging in via SSH without a key, they will immediately be denied access.
Example id_rsa.pub file (local)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDY7D60w303zFxMEkB/VZ17Vf8orxTEbmnCMIw1+LRRU+KW9D/z1ywuV/flFQnoGjKritXYy6o4CbXhn8NHwpEp7a3BxxT1lr1rzYyaP9pOmQTtoqm9PvpHD54hYS/3rzB8orN0+i6FtZ+8RdUuILijUedFlhwP1pCIVkQr6xXwGlN18mux6cFqM9FW4BNDXRV/nxbHy/1LMOvM2WtqFfIWVjlTmzkt3IV94og6tSyIlY5+8IpLzjOBDoGQDT8ZrVAZWsenPskE4XehoKTQM37e1VSmZDqKMfWoi9wrDX03krAmftr7/NYCIIpxMVVVXuMP7GJeQy7W9YWo35Fm+AdzXEh0z5hnes5tEqcRA++hsoaKFtoBOzaY9Yjap9dDmPNaz96RP/uCKxTqJFT+zOUaTMa1jwQk0CNO2LI2QAKgqOLxw3Yvj9W30WzVwAnDXEiegOvZrfODoLKc2jBsbkat0acnoBLwC6XCUBEgSx/K0ChiLpgV/YkJyXGa6kBJVBIC0VbK6Ms/mNDPNJFHuhpXpNV9II6MFIjp0a40nCd/Gqa/rAyPjBmsFUOlNYC5mnfNSbIqSxG4RKzp/Zf+WI2yLw9JSHb94UtKxyVOtdCT61Ah5kzceR80p+o37w1ak0kIMDOkzkavBWEHWMcBqqgBkSmGDSfumaAWqAZQIK8x+Q== your_email@example.com
Example authorized_keys file (IBM i)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDY7D60w303zFxMEkB/VZ17Vf8orxTEbmnCMIw1+LRRU+KW9D/z1ywuV/flFQnoGjKritXYy6o4CbXhn8NHwpEp7a3BxxT1lr1rzYyaP9pOmQTtoqm9PvpHD54hYS/3rzB8orN0+i6FtZ+8RdUuILijUedFlhwP1pCIVkQr6xXwGlN18mux6cFqM9FW4BNDXRV/nxbHy/1LMOvM2WtqFfIWVjlTmzkt3IV94og6tSyIlY5+8IpLzjOBDoGQDT8ZrVAZWsenPskE4Xeho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imXCE5gzj9BsBkhsy72MbqmY0LALa1/At0DRYDCM2uFne198LcMkJ+0mQPkIcJIw== another_key@example.com
Example sshd_config file (IBM i)
# $OpenBSD: sshd_config,v 1.75 2007/03/19 01:01:29 djm Exp $ # This is the sshd server system-wide configuration file. See # sshd_config(5) for more information. # This sshd was compiled with PATH=/QOpenSys/usr/bin:/usr/ccs/bin:/QOpenSys/usr/bin/X11:/usr/sbin:.:/usr/bin # The strategy used for options in the default sshd_config shipped with # OpenSSH is to specify options with their default value where # possible, but leave them commented. Uncommented options change a # default value. #Port 22 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress :: # Disable legacy (protocol version 1) support in the server for new # installations. In future the default will change to require explicit # activation of protocol 1 Protocol 2 # HostKey for protocol version 1 #HostKey /QOpenSys/QIBM/ProdData/SC1/OpenSSH/etc/ssh_host_key # HostKeys for protocol version 2 #HostKey /QOpenSys/QIBM/ProdData/SC1/OpenSSH/etc/ssh_host_rsa_key #HostKey /QOpenSys/QIBM/ProdData/SC1/OpenSSH/etc/ssh_host_dsa_key # Lifetime and size of ephemeral version 1 server key #KeyRegenerationInterval 1h #ServerKeyBits 768 # Logging # obsoletes QuietMode and FascistLogging #SyslogFacility AUTH #LogLevel INFO # Authentication: #LoginGraceTime 2m PermitRootLogin no #StrictModes yes #MaxAuthTries 6 #RSAAuthentication yes #PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys # For this to work you will also need host keys in /QOpenSys/QIBM/ProdData/SC1/OpenSSH/etc/ssh_known_hosts #RhostsRSAAuthentication no # similar for protocol version 2 #HostbasedAuthentication no # Change to yes if you don't trust ~/.ssh/known_hosts for # RhostsRSAAuthentication and HostbasedAuthentication #IgnoreUserKnownHosts no # Don't read the user's ~/.rhosts and ~/.shosts files #IgnoreRhosts yes # To disable tunneled clear text passwords, change to no here! PasswordAuthentication no #PermitEmptyPasswords no # Change to no to disable s/key passwords #ChallengeResponseAuthentication yes # Kerberos options #KerberosAuthentication no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #KerberosGetAFSToken no # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the ChallengeResponseAuthentication and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via ChallengeResponseAuthentication may bypass # the setting of "PermitRootLogin without-password". # If you just want the PAM account and session checks to run without # PAM authentication, then enable this but set PasswordAuthentication # and ChallengeResponseAuthentication to 'no'. #UsePAM no #AllowTcpForwarding yes #GatewayPorts no #X11Forwarding no #X11DisplayOffset 10 #X11UseLocalhost yes #PrintMotd yes #PrintLastLog yes #TCPKeepAlive yes #UseLogin no UsePrivilegeSeparation no #PermitUserEnvironment no #Compression delayed #ClientAliveInterval 0 #ClientAliveCountMax 3 #UseDNS yes #PidFile /var/tmp/sshd.pid #MaxStartups 10 #PermitTunnel no # no default banner path #Banner /some/path # override default of no subsystems Subsystem sftp /QOpenSys/QIBM/ProdData/SC1/OpenSSH/libexec/sftp-server # Example of overriding settings on a per-user basis #Match User anoncvs # X11Forwarding no # AllowTcpForwarding no # ForceCommand cvs server