LoginSignup
7
4

More than 1 year has passed since last update.

WindowsからサーバへのSSH接続とポート開放

Last updated at Posted at 2019-02-15

はじめに

これは私がConoHa VPSにSSH接続した時のメモです。間違えやご指摘があれば教えてくれると嬉しいです。

環境

[Client]
OS :Windows 10 pro
作業OS :Ubuntu 18.04 LTS(WSL)

[Server]
OS :CentOS 7.6(64bit)
プラン :630円/月
リージョン :東京
接続許可ポート :全て許可

SSH接続

client
$ ssh root@IPアドレス -p 22
# もしyesかnoか聞かれたらyesを入力
# rootパスワード入力

設定

作業用ユーザーの追加

server
$ adduser ユーザー名
$ passwd ユーザー名
New password:好きなパスワード
Retype new password:もう一度
passwd: all authentication tokens updated successfully.

$ usermod -g wheel ユーザー名

最後の行で新規作成したユーザーにsudo実行権限を与えるためwheelグループに追加しました。
wheelに追加されたかどうかは

$ id ユーザー名
$ groups ユーザー名

などのコマンドで確認できます。

server
[root@IPAddress ~]$ su ユーザー名
[ユーザー名@IPAddress root]$ sudo echo 'hello'
hello
[ユーザー名@IPAddress root]$

となれば成功。
一度抜けてクライアントから作成したユーザーで接続してみましょう。

client
$ ssh ユーザー名@IPアドレス -p 22
#作成したユーザーのパスワードを入力

これからはこの作成したユーザーで作業をします。rootでは作業しないので気をつけてください。
また、wheelグループ以外のユーザーがsudoコマンドを使えないようにしましょう。

server
$ sudo vim /etc/pam.d/su
:
:
auth required pam_wheel.so use_uid #を外す
:

鍵認証でSSH接続する

準備

server
$ mkdir .ssh
$ chmod 700 .ssh

サーバー上にクライアントの公開鍵を置くディレクトリを作成しました。
パーミッションを700にしました。

$ ls -al
:
drwx------  2 ユーザー名 wheel 4096 Feb 15 15:34 .ssh
:

とかになってたらokです。

sshのconfig編集

server
$ sudo vim /etc/ssh/sshd_config

# 以下の設定を確認

PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

コメントアウトを外したり。viの操作方法はわからなければ[vi 編集]とかでググってください。

鍵生成

クライアントでの作業です。

client
$ cd .ssh


$ ssh-keygen -t rsa -b 4096
Enter file in which to save the key (/.../.ssh/id_rsa): conoha (自由に名前を指定)
Enter passphrase (empty for no passphrase): パスワード作成
Enter same passphrase again: もう一度



$ ls
conoha  conoha.pub  known_hosts

$ cat conoha
# 鍵が出てくる

公開鍵のアップロード

client
[~/.ssh]$ scp conoha.pub ユーザー名@IPアドレス:~/.ssh/authorized_keys

conoha.pub                                                                   100%  369     3.4KB/s   00:00

試しに認証鍵で接続してみる。

client
$ ssh ユーザー名@IPアドレス -i '~/.ssh/conoha.pub'

ssh conoha で接続できるように

いちいち上のコマンドを打って接続するのは面倒なので

client
[client~]$ vim .ssh/config

#以下を記入
Host conoha
     HostName IPアドレス
     Port 22
     User ConoHaの作業用ユーザー名
     IdentityFile ~/.ssh/conoha


$ chmod 600 ~/.ssh/config
client
$ ssh conoha
#パスワード入力

できたら成功

セキュリティ設定

認証鍵でしかログインできなくしたり22番ポートの変更とかします。
コメントアウトされてたら外してください。
ここでポートを変更した後サーバーからログアウトしてしまうと接続できなくなってしまうので気をつけてください。

server
$ sudo vim /etc/ssh/sshd_config

#以下を編集

#ポート番号の変更。番号は基本的になんでも
Port 50022

PasswordAuthentication no

PermitRootLogin no



#設定の反映
$ sudo systemctl restart sshd.service

firewalldでポートを開放

SoftEtherで443接続できなかったのでfirewalldで開放してみました。
22番ポート変更とかはこちらを参考にさせていただきました。(ありがとうございます。)
ポート変更が終わったあとからの説明になります。
https://qiita.com/EisKern/items/3a12bd61ba572e8430d9#firewalldで設定
設定↓

server
#許可されてるポートやサービスの確認
$ sudo firewall-cmd --list-all --zone=public
#開放していく
$ sudo firewall-cmd --add-service=https --zone=public --permanent
$ sudo firewall-cmd --add-service=http --zone=public --permanent
$ sudo firewall-cmd --add-port=80/tcp --zone=public --permanent
$ sudo firewall-cmd --add-port=443/tcp --zone=public --permanent
$ sudo firewall-cmd --add-port=992/tcp --zone=public --permanent
$ sudo firewall-cmd --add-port=1194/tcp --zone=public --permanent
$ sudo firewall-cmd --add-port=5555/tcp --zone=public --permanent
$ sudo firewall-cmd --add-port=500/udp --zone=public --permanent
$ sudo firewall-cmd --add-port=4500/udp --zone=public --permanent
$ sudo firewall-cmd --add-port=50022/tcp --zone=public --permanent
# 50022に変更した22を開けるのもわすずに

#保存
$ sudo firewall-cmd --reload

#確認
$ sudo firewall-cmd --list-all --zone=public

接続できないときはVPSを再起動してください

クライアント側でポートの設定

ポートを22から50022に変えたためクライアントの設定を変更しなければなりません。

client
[client~]$ vim ~/.ssh/config

Host conoha
     HostName IPアドレス
     Port 50022
     User サーバーでの作業用ユーザー名
     IdentityFile ~/.ssh/conoha

接続できるか試してみましょう。

$ ssh conoha

おまけ

iptables設定

iptablesで設定してたけどfirewallでやったほうがいいっぽいので記録としておまけに置いときます.やらないでいいです.

server
$ sudo yum install iptables-services
server
$ sudo vim /etc/sysconfig/iptables


*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 50022 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p udp -m udp --dport 500 -j ACCEPT
-A INPUT -p udp -m udp --dport 4500 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT


$ sudo systemctl restart iptables.service

設定を反映し、firewalldを無効化します。

server
#iptablesの自動起動を有効
$ sudo systemctl enable iptables.service

#firewalldを止める
$ sudo systemctl stop fireawlld.service

#firewalldの自動起動を無効
$ sudo systemctl disable firewalld.service

iptablesやfirewalldの確認

server
$ systemctl status firewalld
:
Active: inactive (dead)
:


$ systemctl status iptables
:
Active: active (exited)
:


$ systemctl list-unit-files | grep -e firewalld -e iptables
firewalld.service                             disabled
iptables.service                              enabled 

おわり

できたら成功です。お疲れ様でした。
始めてでしたが、やってるうちにコマンドやネットワーク、セキュリティに関する知識が薄くながらも付いたので良かったです。
このはちゃんかわいい。

追記

[2020/07/28]
このあと、このVPSを使って実際にVPNを構築して接続したのでその備忘録を書いたので、共有しておきます。https://kakao.hateblo.jp/entry/2019/06/27/235946

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4