LoginSignup
2

More than 5 years have passed since last update.

さくらのクラウド > MacのターミナルからSSH接続 macOS→CentOS

Last updated at Posted at 2017-11-26

ssh

環境

さくらのクラウド
OS:CentOS 7.4

以下を参考にさせていただきました。
【初心者向け】さくらのクラウドにターミナルからSSH接続するための手順&解説【macOS→CentOS】 - Qiita

下記はTerminalで作業する。

Server側設定

ssh login
$ ssh root@xxx.xxx.xxx.xxx

ユーザーを追加

# adduser hoge
# passwd hoge

ユーザーのフォルダのパーミション変更

# cd hoge
# chmod 700 hoge

ここで一度ログアウトする。
# exit

Mac側設定

カレントフォルダ移動&確認
$ cd
$ pwd

/Users/hoge

公開鍵認証の設定(mac側)

「.ssh」フォルダを作成
$ mkdir .ssh

カレントフォルダを移動
(/Users/hoge/.ssh フォルダに入る。)
$ cd .ssh

認証用の鍵を作成

$ ssh-keygen -t rsa

以下のように確認が表示される。
参考サイト同様にsakura_rsaで進める。

Enter file in which to save the key (/Users/username/.ssh/id_rsa): 

       ↓

Enter file in which to save the key (/Users/atsu/.ssh/id_rsa): sakura_rsa

パスワードは何も入力せずenterを2回。
+---[RSA 2048]----+から始まるキラキラしたAAが表示されたら成功。

これで「sakura_rsa」と「sakura_rsa.pub」という2つのファイルができた。

リモートコピー

rsaファイルをsakuraに送る。
:~ :転送先のディレクトリの指定を忘れがちなので注意。

$ scp sakura_rsa.pub hoge@xxx.xxx.xxx.xxx:~

パスワードを聞かれるので入力。
「100% 398 0.4KB/s 00:00 」みたいなのが表示されれば成功。

サーバー側

公開鍵認証の設定

ssh用ユーザーでsshログイン
$ ssh hoge@xxx.xxx.xxx.xxx

SCPした鍵が届いているか確認。
$ ls

sakura_rsa.pub が存在する事を確認。

ホームディレクトリ配下に「.ssh」ディレクトリを作成し鍵を保存する。
パーミッションを700に変更。

$ mkdir .ssh
$ chmod 700 .ssh

.sshディレクトリに「authorized_keys」という名前で移動する。
authorized_keysのパーミッション変更。

$ mv sakura_rsa.pub .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys

sshd_configの編集

sshd_configを編集するにあたりスーパーユーザーになる。
$ su

「sshd_config」という設定ファイルを書き換える。
# vi /etc/ssh/sshd_config

以下の項目について設定する。

ポート番号

#Port 22
↓
Port xxxx

ポート番号は1024以上、49513~65535が望ましい。今回は「49513」を使う。

rootログイン不可

#PermitRootLogin yes
↓
PermitRootLogin no

鍵認証設定。

#PubkeyAuthentication yes
↓
PubkeyAuthentication yes

CentOS7.4には以下は存在しなかったが、以下もあれば

#RSAAuthentication yes
↓
RSAAuthentication yes

パスワードによるログイン不可

PasswordAuthentication yes
↓
PasswordAuthentication no

ここまで変えたら保存する。

sshdの再起動

再起動を行う。
# systemctl restart sshd.service

状態を確認する
# systemctl status sshd.service
以下を確認。

  • Active: active (running)」
  • Server listening on :: port 49513.

ファイアウォールの設定

ファイアウォールの設定を変更。

# vi /usr/lib/firewalld/services/ssh.xml

port protocol="tcp" port="22"/ の「22」を「49513」に書き換える。

ファイアウォール再起動

# systemctl reload firewalld.service

SSH接続できるか確認

ターミナルをもうひとつ開いて

$ ssh hoge@xxx.xxx.xxx.xxx -i ~/.ssh/sakura_rsa -p 49513

[hoge@sakura ~]$ と表示されれば成功。

SSH接続用短縮コマンドの作成

上記まででSSH接続できるようになったが以下も作成すれば便利。(参考サイトのnulltypoさんに感謝!)

mac側での作業

ターミナルを新しく開き、
ホームディレクトリ(~/)配下に.sshディレクトリ以下にconfigファイルを作成する。
$ vi ~/.ssh/config

(/Users/hoge/.ssh/config)
configの中身にはいかを記述。

Host sakura
    Hostname xxx.xxx.xxx.xxx
    User hoge
    Port 49513
    IdentityFile ~/.ssh/sakura_rsa
    IdentitiesOnly yes

IdentitiesOnly:これをyesにしておくと他の設定を追加した時にエラーになりにくいとの事。

$ ssh sakura
[hoge@sakura ~]$ と表示されれば成功。

謝辞

以下を大いに参考にさせていただきました。ありがとうございました。
【初心者向け】さくらのクラウドにターミナルからSSH接続するための手順&解説【macOS→CentOS】 - Qiita

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
2