WebAREANA Indigo VHSでUbuntuサーバーへのSSH接続のポートを変更

Uncategorized

WebAREANA Indigo (VPS) にSSH接続するやり方の復習

プロキシ接続して、日本からしかアクセスできないサービスに色々とアクセスしていたのですが、久しぶりにサーバーにSSH接続しようとしたら、完全にやり方忘れておりました。。。

SSH bob@123.456.789.012
#SSHで、123.456.789.012というサーバーに、bobというユーザー名でアクセス

コマンドの履歴にこんなのが残っていたので打ってみると、

Bob@BobsMacBook-Pro ~ % SSH bob@123.456.789.012
bob@123.456.789.012: Permission denied (publickey).

結果は、Permission denied(publickey)。

公開鍵!?

公開鍵と秘密鍵がなんだっけ。という状態のダメダメマンですが、鍵、といえば、以前発行されたキーを使ってたな、と思い出しました。

そして、どこかにしまってあったprivate_key.txtなるものを見つけ出し、リトライするも、、、

撃沈。

Bob@BobMacBook-Pro ~ % SSH -i Documents/webarenasshkey/private_key.txt bob@123.456.789.012 -p 22
bob@123.456.789.012: Permission denied (publickey).
#SSHで123.456.789.012というサーバーにbobというユーザーで接続。i(identity_file)で公開鍵認証に

ubuntuの初期ユーザーは、ubuntuという記述を発見。大事なことなのに忘れていました。

ubuntuのマークらしいです

初期ユーザーのubuntuでアクセスを試みると。。。

Bob@BobMacBook-Pro ~ % SSH -i Documents/webarenasshkey/private_key.txt ubuntu@123.456.789.012 -p 22
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-46-generic x86_64)

ついに成功です。ヒューヒュー!!!

ポートを22から10022に変更したいので、使用ポートを確認

公開鍵認証を使用しているので、セキュリティも心配することはないと思うのですが、皆さん、口を揃えて、ポート22を使わない、とおっしゃっているので、ポートも変更することに。

/etc/ssh/に、設定ファイルがあるというので見てみると、

ubuntu@i-13100000389060:~$ ll /etc/ssh/
total 556
drwxr-xr-x  4 root root   4096 Jul 11 10:29 ./
drwxr-xr-x 98 root root   4096 Sep  3 06:40 ../
-rw-r--r--  1 root root 505426 Feb 25  2022 moduli
-rw-r--r--  1 root root   1650 Feb 25  2022 ssh_config
drwxr-xr-x  2 root root   4096 Feb 25  2022 ssh_config.d/
-rw-------  1 root root   1393 Jul 11 10:29 ssh_host_dsa_key
-rw-r--r--  1 root root    611 Jul 11 10:29 ssh_host_dsa_key.pub
-rw-------  1 root root    513 Jul 11 10:29 ssh_host_ecdsa_key
-rw-r--r--  1 root root    183 Jul 11 10:29 ssh_host_ecdsa_key.pub
-rw-------  1 root root    411 Jul 11 10:29 ssh_host_ed25519_key
-rw-r--r--  1 root root    103 Jul 11 10:29 ssh_host_ed25519_key.pub
-rw-------  1 root root   2610 Jul 11 10:29 ssh_host_rsa_key
-rw-r--r--  1 root root    575 Jul 11 10:29 ssh_host_rsa_key.pub
-rw-r--r--  1 root root    342 Dec  7  2020 ssh_import_id
-rw-r--r--  1 root root   3252 Apr 20 18:30 sshd_config
drwxr-xr-x  2 root root   4096 Feb 25  2022 sshd_config.d/

ssh_configと、sshd_configがあります。

え?sshdって何?と思って、ssh_configの中身を見てみると

ubuntu@i-13100000389060:~$ cat /etc/ssh/ssh_config

# This is the ssh client system-w

WordPressのfunctions.phpを編集する前は、編集前に必ずバックアップを取って保存してください。もし編集後、エラーが出るようでしたら、バックアップファイルを元に復元してください。

ide configuration file. See # ssh_config(5) for more information. This file provides defaults for # users, and the values can be changed in per-user configuration files # or on the command line.
続き # Configuration data is parsed as follows: # 1. command line options # 2. user-specific file # 3. system-wide file # Any configuration value is only changed the first time it is set. # Thus, host-specific definitions should be at the beginning of the # configuration file, and defaults at the end. # Site-wide defaults for some commonly used options. For a comprehensive # list of available options, their meanings and defaults, please see the # ssh_config(5) man page. Include /etc/ssh/ssh_config.d/*.conf Host * # ForwardAgent no # ForwardX11 no # ForwardX11Trusted yes # PasswordAuthentication yes # HostbasedAuthentication no # GSSAPIAuthentication no # GSSAPIDelegateCredentials no # GSSAPIKeyExchange no # GSSAPITrustDNS no # BatchMode no # CheckHostIP yes # AddressFamily any # ConnectTimeout 0 # StrictHostKeyChecking ask # IdentityFile ~/.ssh/id_rsa # IdentityFile ~/.ssh/id_dsa # IdentityFile ~/.ssh/id_ecdsa # IdentityFile ~/.ssh/id_ed25519 # Port 22 # Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc # MACs hmac-md5,hmac-sha1,umac-64@openssh.com # EscapeChar ~ # Tunnel no # TunnelDevice any:any # PermitLocalCommand no # VisualHostKey no # ProxyCommand ssh -q -W %h:%p gateway.example.com # RekeyLimit 1G 1h # UserKnownHostsFile ~/.ssh/known_hosts.d/%k SendEnv LANG LC_* HashKnownHosts yes GSSAPIAuthentication yes

長すぎて読めない。。。そこでgrep。

ubuntu@i-13100000389060:~$ cat /etc/ssh/ssh_config|grep port
ubuntu@i-13100000389060:~$ cat /etc/ssh/ssh_config|grep Port
#   Port 22

portでは出てこないので、Portで出てきました。

これか、これなのか!

とりあえずポートチェック!よくわからないが、まずはssだ!

ubuntu@i-13100000389060:~$ ss
Netid State  Recv-Q Send-Q                Local Address:Port          Peer Address:Port  Process 
u_dgr ESTAB  0      0               /run/systemd/notify 15202                    * 0             
u_dgr ESTAB  0      0      /run/systemd/journal/dev-log 15228                    * 0             
u_dgr ESTAB  0      0       /run/systemd/journal/socket 15230                    * 0             
u_str ESTAB  0      0       /run/dbus/system_bus_socket 17899                    * 17898         
u_str ESTAB  0      0                                 * 18436                    * 18437         
u_dgr ESTAB  0      0                                 * 17469                    * 15228         
u_str ESTAB  0      0                                 * 16913                    * 16914    
〜長すぎて以下略〜

相変わらずよくわからないので、ss -atnだ!

ubuntu@i-13100000389060:~$ ss -atn
State     Recv-Q    Send-Q        Local Address:Port            Peer Address:Port     Process    
LISTEN    0         4096          127.0.0.53%lo:53                   0.0.0.0:*                   
LISTEN    0         128                 0.0.0.0:22                   0.0.0.0:*                   
ESTAB     0         52          123.456.789.012:22           987.654.321.109:123456               
LISTEN    0         128                    [::]:22                      [::]:*                   
LISTEN    0         256                       *:3999                       *:*          

よくわからんが、22が出てきた!3999も、以前、プロキシのために開放したポートだ。

lsofというコマンドもおすすめされたが、

ubuntu@i-13100000389060:~$ lsof 
COMMAND   PID TID TASKCMD               USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
systemd     1                           root  cwd   unknown                                        /proc/1/cwd (readlink: Permission denied)
systemd     1                           root  rtd   unknown                                        /proc/1/root (readlink: Permission denied)
systemd     1                           root  txt   unknown                                        /proc/1/exe (readlink: Permission denied)
〜以下略〜

さらによくわからない。PIDといえば、立ち上がってるプロセスか?

SSHで使用するポート番号を22から10022に書き換えるぞ!

よくよく調べてみると、ssh_configは、ssh接続元の設定、sshd_configは接続される側の設定ということで、sshd_configにポートの記載をすることがわかったのだが、一応中身も見ておきたい。

ubuntu@i-13100000389060:~$ cat /etc/ssh/sshd_config

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

# 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 override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
続き #HostKey /etc/ssh/ssh_host_rsa_key #HostKey /etc/ssh/ssh_host_ecdsa_key #HostKey /etc/ssh/ssh_host_ed25519_key # Ciphers and keying #RekeyLimit default none # Logging #SyslogFacility AUTH #LogLevel INFO # Authentication: #LoginGraceTime 2m #PermitRootLogin prohibit-password #StrictModes yes #MaxAuthTries 6 #MaxSessions 10 #PubkeyAuthentication yes # Expect .ssh/authorized_keys2 to be disregarded by default in future. #AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 #AuthorizedPrincipalsFile none #AuthorizedKeysCommand none #AuthorizedKeysCommandUser nobody # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts #HostbasedAuthentication no # Change to yes if you don't trust ~/.ssh/known_hosts for # 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 yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) KbdInteractiveAuthentication no # Kerberos options #KerberosAuthentication no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #KerberosGetAFSToken no # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes #GSSAPIStrictAcceptorCheck yes #GSSAPIKeyExchange no # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the KbdInteractiveAuthentication and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via KbdInteractiveAuthentication 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 KbdInteractiveAuthentication to 'no'. UsePAM yes #AllowAgentForwarding yes #AllowTcpForwarding yes #GatewayPorts no X11Forwarding yes #X11DisplayOffset 10 #X11UseLocalhost yes #PermitTTY yes PrintMotd no #PrintLastLog yes #TCPKeepAlive yes #PermitUserEnvironment no #Compression delayed #ClientAliveInterval 0 #ClientAliveCountMax 3 #UseDNS no #PidFile /run/sshd.pid #MaxStartups 10:30:100 #PermitTunnel no #ChrootDirectory none #VersionAddendum none # no default banner path #Banner none # Allow client to pass locale environment variables AcceptEnv LANG LC_* # override default of no subsystems Subsystem sftp /usr/lib/openssh/sftp-server # Example of overriding settings on a per-user basis #Match User anoncvs # X11Forwarding no # AllowTcpForwarding no # PermitTTY no # ForceCommand cvs server

バッチリ、Port 22がコメントアウトされてますね!

いけ!sshd_configの編集だ。vimとviは何が違うのか!

ubuntu@i-13100000389060:/etc/ssh$ sudo vim sshd_config

さあ、ポートを変更した!でも、一応見直してみよう!

ubuntu@i-13100000389060:~$ head -n 20 /etc/ssh/sshd_config

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

# 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 override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

#Port 22
Port 10022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key

Port 10022の10文字が眩しい。

仕上げは、SSHデーモン?プロセス?の再起動だ!

ubuntu@i-13100000389060:~$ sudo /etc/init.d/ssh restart
Restarting ssh (via systemctl): ssh.service.
ubuntu@i-13100000389060:~$ 

ポート22で接続していますが、接続は切れないんですね。。。一度ログアウトしてログインしてみましょう。

ubuntu@i-13100000389060:~$ exit
logout
Connection to 164.70.92.236 closed.
bob@BobsMacBook-Pro ~ % SSH -i Documents/webarenasshkey/private_key.txt ubuntu@123.456.789.012
ssh: connect to host 123.456.789.012 port 22: Connection refused
chikaramaeda@ChikaranoMacBook-Pro ~ % 

お、port 22が塞がっているようです。

どーん!要塞が誕生しました
bob@BobsMacBook-Pro ~ % SSH -i Documents/webarenasshkey/private_key.txt ubuntu@123.456.789.012 -p 10022
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-47-generic x86_64)

おお、ポート番号10022で接続できました!

今度、時間と気力ができたら、貧困エンジニアUさんの教えてくれている、

$ssh jitaku

とするだけでSSH接続できてしまう環境を整えてみようと思います。

なぜ、あえて「痴漢は犯罪です」のポスターがサムネイルなのでしょうか。。。

コメント

タイトルとURLをコピーしました