LoginSignup
0
0

More than 3 years have passed since last update.

ターミナルを使ったサーバの初期セットアップ

Last updated at Posted at 2019-04-04

目的

もういつものサーバの初期セットアップをを惰性でやってのける為にまとめました。
ユーザ作成などの簡単なサーバの初期設定と表題の通りターミナルを使ったsshの設定方法について記載しています。

Mac側でssh用の鍵を生成しパーミッション設定

Macでターミナルを開いて公開鍵認証に使用する鍵の生成を行います。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa): 
Created directory '/Users/username/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/username/.ssh/id_rsa.
Your public key has been saved in /Users/username/.ssh/id_rsa.pub.
The key fingerprint is:

--- 略 ---

$ ls .ssh
id_rsa      id_rsa.pub
$ chmod 600 ~/.ssh/id_rsa.pub

サーバにログイン

$ ssh root@111.222.333.444
root@111.222.333.444's password: 

SAKURA Internet [Virtual Private Server SERVICE]

日本語設定

# localectl set-locale LANG=ja_JP.utf8

作業用ユーザに追加とパスワード設定、wheelグループへの追加

# useradd username
# passwd username
Changing password for user username.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
# usermod -G wheel username

一般ユーザへの切り替え、「.ssh」ディレクトリの作成

# su username
$ cd
$ mkdir .ssh
$ chmod 700 .ssh

Macから公開鍵を転送

$ scp ~/.ssh/id_rsa.pub username@111.222.333.444:~/.ssh/authorized_keys

--- 略 ---

username@111.222.333.444's password: 
id_rsa.pub                               100%  412    16.0KB/s   00:00

サーバ側でauthorized_keysのパーミッション設定

# chmod 600 .ssh/authorized_keys

sshd_configの書き換え

$ sudo vi /etc/ssh/sshd_config
①
#PermitRootLogin yes         
↓
PermitRootLogin no

②
#PasswordAuthentication yes
↓
PasswordAuthentication no 

③
#Port 22
↓
Port 3333 ※well-known ports以外で任意の番号

sshdの再起動

$ systemctl restart sshd
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: username
Password: 
==== AUTHENTICATION COMPLETE ===

Firewalldの設定とリロード

$ sudo cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/
$ sudo vi /etc/firewalld/services/ssh.xml

<?xml version="1.0" encoding="utf-8"?><service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/> ←ここのportを先ほど指定した任意の番号に変更する
</service>
$ sudo firewall-cmd --reload
success

yum -y update

何はともあれ yum update です。

$ sudo yum -y update

サーバを再起動する

ここまで設定が済んだらlocaleで行なった日本語の設定を反映の為、再起動を行います。

下記のコマンドを実行して下さい。

$ sudo systemctl reboot

Tips

SELinuxの設定

何かと邪魔になる為、結局SELinuxは無効にする事が多いですが、いざ有効にした時に困らないように設定について記しておきます。

SELinuxはLinux実装された強固なセキュリティ機構です。
さくらのVPS 標準OS CentOS7 の場合、最初はSELinuxが無効になっています。
SELinuxを有効のまま運用したい方は以下を照してください。

$ sudo vi /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disable ← ここを enforcing に変更する
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

※ ちなみに enforcing だと何かと不便な場合は permissive にすると、アクセス制限は行わず警告を出力する状態になります。

SELinux管理コマンドのインストール

$ sudo yum -y install policycoreutils-python

--- 略 ---

Complete!

SElinuxへssh接続ポートの追加設定

管理コマンドのインストール後に、変更したssh接続用ポートをSELinuxに反映させます。

$ sudo semanage port -a -t ssh_port_t -p tcp 3333   
$ sudo semanage port -l | grep ssh
ssh_port_t                     tcp      3333, 22

SELinuxの設定時の注意事項

SElinuxへssh接続ポートの追加設定にあたりSElinuxが無効になっていると以下のようなエラーになります。
さくらのVPSでCentOS7を使用する場合、デフォルトではSELinuxが無効になっているようなので有効に設定してあげる必要があります。

# semanage port -a -t ssh_port_t -p tcp 3333
SELinux:  Could not downgrade policy file /etc/selinux/targeted/policy/policy.30, searching for an older version.
SELinux:  Could not open policy file <= /etc/selinux/targeted/policy/policy.30:  No such file or directory
/sbin/load_policy:  Can't load policy:  No such file or directory
libsemanage.semanage_reload_policy: load_policy returned error code 2. (No such file or directory).
SELinux:  Could not downgrade policy file /etc/selinux/targeted/policy/policy.30, searching for an older version.
SELinux:  Could not open policy file <= /etc/selinux/targeted/policy/policy.30:  No such file or directory
/sbin/load_policy:  Can't load policy:  No such file or directory
libsemanage.semanage_reload_policy: load_policy returned error code 2. (No such file or directory).
OSError: No such file or directory

詳細はRedHat社の公式ドキュメント参照

SELinuxのステータス取得

SElinux のステータスをチェックするには、getenforce または sestatus コマンドを使います。
ステータスの意味はSElinuxのステータス変更を参照して下さい。

# getenforce
Permissive

# sestatus
SELinux status:                 enabled
        <省略>

SElinuxのステータス変更

有効化

# setenforce 1
一時的なステータス変更コマンド 変更後のステータス ステータスの意味
setenforce 0 permissive 一時的に無効(ポリシーに違反するもののログが残る※) アクセス制限はされないが警告が出る
setenforce 1 enforcing アクセス制限がかかる

※監査ログは /var/log/audit/audit.log に記録されるが ausearch -m avc で確認する方がわかりやすいです。詳細な確認方法はコチラを参照

無効化

 # vi /etc/selinux/config
SELINUX=enforcing

その他のsshクライアントの公開鍵認証の設定

windowsを使う事も多々あるので記載しておきます。
使用するsshクライアントによって設定が異なります。
以下を参照して鍵の生成からおこなってください

・TeraTermの場合

さくらのサポート情報 -SSH接続の設定変更方法-

コチラの[鍵認証の設定]のSTEP1からSTEP2までを実施して下さい。
※STEP3以降はCentOS7では使えないコマンドが含まれています。

・RLoginの場合

「よく分かる公開鍵認証」~初心者でもよくわかる!VPSによるWebサーバー運用講座(2)

コチラの"Windowsの場合の公開鍵認証のやり方"を参照して下さい。

0
0
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
0
0