LoginSignup
23
30

More than 5 years have passed since last update.

ConoHa VPSでUbuntuの初期設定(作業ユーザ追加、ssh設定)メモ

Last updated at Posted at 2016-06-21

背景

  • ローカルマシンが近々切り替わるのと、外出が多くなることから、VPS上に環境が欲しくなった
  • さくらVPSを考えていたが、最近はConoHaが比較対象になってるようなので試しに使ってみることにした
  • dockerがUbuntu推しらしいので、Ubuntuを利用することにした

概要

  • 作業ユーザを作って、rootログインを禁止するところまでの手順

手順メモ

ConoHaでサーバを追加

下記内容を選択してサーバを追加。事前にSSH Keyは登録しておく。

  1. プラン:VPS / 東京 / 1GB
  2. イメージ:OS / Ubuntu / 14.04(64bit)
  3. オプション:自動バックアップ:無効 / SSD 50GB / SSH, Web / SSH, Web / 登録済みのキー

作業ユーザ設定

  • sshでログイン

    IPアドレスはConoHaの管理画面で「サーバー」>ネームタグをクリック>「ネットワーク情報」で確認できる。
    以下、Windowsマシンのgit-bashから作業を実施。

$ ssh root@xxx.xxx.xxx.xxx
The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xxx.xxx.xxx.xxx' (ECDSA) to the list of known hosts.
Enter passphrase for key '/c/Users/user/.ssh/id_rsa':
Welcome to Ubuntu 14.04.4 LTS (GNU/Linux 3.16.0-60-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Tue Jun 21 12:35:22 JST 2016

  System load: 0.0               Memory usage: 5%   Processes:       65
  Usage of /:  3.2% of 48.11GB   Swap usage:   0%   Users logged in: 0

  Graph this data and manage this system at:
    https://landscape.canonical.com/

root@xxx-xxx-xxx-xxx:~#
  • ユーザ追加
# adduser segu
Adding user `segu' ...
Adding new group `segu' (1000) ...
Adding new user `segu' (1000) with group `segu' ...
Creating home directory `/home/segu' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for segu
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]
#
  • ユーザをsudoグループに追加
# gpasswd -a segu sudo
Adding user segu to group sudo
  • 作業ユーザの公開鍵を設置

    一旦サーバからログアウトして、scpで公開鍵を転送。

# exit
logout
Connection to xxx.xxx.xxx.xxx closed.

$ scp /path/to/ssh/id_rsa.pub root@xxx.xxx.xxx.xxx:~/
Enter passphrase for key '/c/Users/user/.ssh/id_rsa':
id_rsa.pub                                                                                                                    100%  750     0.7KB/s   00:00
  • ログインして公開鍵を設置
$ ssh root@xxx.xxx.xxx.xxx
  :
 (略)
  :
# mkdir ~segu/.ssh
# mv id_rsa.pub ~segu/.ssh/authorized_keys
# chown -R segu: ~segu/.ssh
# chmod 700 ~segu/.ssh
# chmod 600 ~segu/.ssh/authorized_keys
# exit
  • 作業ユーザでのログインを確認
$ ssh -i /path/to/ssh/id_rsa segu@xxx.xxx.xxx.xxx
  :
 (略)
  :
$

ssh設定(rootログイン禁止)

  • sshd_configファイルを編集する
$ sudo vi /etc/ssh/sshd_config
  • rootログインを禁止する(それ以外の基本的な設定はされた状態になっている)
sshd_config
PermitRootLogin yes
                 ↓ 変更
PermitRootLogin no
  • sshサービスを再起動する

    下記のようにsudo実行時にメッセージが表示されるがあとで対応する。

$ sudo service ssh restart
sudo: unable to resolve host xxx-xxx-xxx-xxx
ssh stop/waiting
ssh start/running, process 2454
  • rootでログインできないことを確認する
$ ssh root@xxx.xxx.xxx.xxx
Enter passphrase for key '/c/Users/user/.ssh/id_rsa':
Permission denied (publickey).

なお、サーバーを追加するときの設定で接続許可ポートをSSHとWebに限定しているので、それ以外のポートへの接続はUbuntuに到達する以前に弾かれる模様。
そのため、ssh のポート番号を変更したい場合は、接続許可ポートを「全て許可」にした上で、ポートの設定は自分で別途行う必要がある。

アップデート

アップデートを先に行ってもいいと思う。個人的には先にRootログインを禁止したかったので、この順番。

$ sudo apt-get upgrade
sudo: unable to resolve host xxx-xxx-xxx-xxx
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

$ sudo apt-get update
sudo: unable to resolve host xxx-xxx-xxx-xxx
Ign http://jp.archive.ubuntu.com trusty InRelease
  :
 (略)
  :
Fetched 5,520 kB in 7s (747 kB/s)
Reading package lists... Done

sudo時のメッセージ対策

ホスト名設定してからの方がいい。

$ sudo sh -c 'echo 127.0.1.1 $(hostname) >> /etc/hosts'

あとがき

  • sudoの設定の仕方がCentOSとは違っていた
  • sshd_configの設定があらかたされている状態なのがよい
  • こういうのはchef等で用意しておくとよいのだろうけど、よく分からない

参考

23
30
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
23
30