#author("2026-06-01T15:12:51+09:00","","") #author("2026-06-02T14:29:48+09:00","","") #nofollow #norelated 総数:&counter(total); 今日:&counter(today); 昨日:&counter(yesterday); * もくじ [#mcbb9722] #contents 参考URL&br; [[How to Setup an SFTP Server on Ubuntu 22.04 using OpenSSH>https://formatswap.com/blog/linux-tutorials/how-to-setup-an-sftp-server-on-ubuntu-22-04-using-openssh/]]&br; [[How To Setup SFTP Server on Ubuntu 22.04 LTS>https://www.linuxbuzz.com/how-to-setup-sftp-server-on-ubuntu/]]&br; [[Linux サーバーに SFTP をインストールして使用する方法>https://ja.linux-console.net/?p=3369#gsc.tab=0]]&br; * IP固定化しておく [#v88c1a13] 参考url&br; [[Ubuntu 22.04 LTS ServerのIPを固定する>https://qiita.com/energy_tom1412/items/1652a4a785df4b629f53]] 例 192.168.3.9 * Install OpenSSH [#l197aa8c] ~$ sudo apt update ~$ sudo apt install openssh-server **View SSH Status [#sa73b225] verify that OpenSSH is installed on your system and actively running.&br; Use the below command and confirm that you see “active (running)” on the third line.&br; ~$ sudo systemctl status ssh ● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2023-07-19 14:36:02 JST; 40min ago Docs: man:sshd(8) man:sshd_config(5) Process: 764 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS) Main PID: 798 (sshd) Tasks: 1 (limit: 18946) Memory: 3.8M CPU: 34ms CGroup: /system.slice/ssh.service └─798 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups" 7月 19 14:36:00 OptiPlex-790 systemd[1]: Starting OpenBSD Secure Shell server... 7月 19 14:36:02 OptiPlex-790 sshd[798]: Server listening on 0.0.0.0 port 22. 7月 19 14:36:02 OptiPlex-790 sshd[798]: Server listening on :: port 22. 7月 19 14:36:02 OptiPlex-790 systemd[1]: Started OpenBSD Secure Shell server. * Create New User [#b6cc5820] create a new user for logging into the SFTP server. &br; $ sudo adduser sftpuser ユーザー `sftpuser' を追加しています... 新しいグループ `sftpuser' (1001) を追加しています... 新しいユーザー `sftpuser' (1001) をグループ `sftpuser' に追加しています... ホームディレクトリ `/home/sftpuser' を作成しています... `/etc/skel' からファイルをコピーしています... 新しい パスワード: →例)sftpuser と入力した 正しくないパスワード: このパスワードには、一部に何らかの形でユーザー名が含まれています 新しい パスワードを再入力してください: →例)sftpuser と入力した passwd: パスワードは正しく更新されました sftpuser のユーザ情報を変更中 新しい値を入力してください。標準設定値を使うならリターンを押してください フルネーム []: 部屋番号 []: 職場電話番号 []: 自宅電話番号 []: その他 []: 以上で正しいですか? [Y/n] Y * Create New Group [#n09b3f94] create a new group for our sftpuser.&br; We will configure SSH to give SFTP access to any user in this group.&br; $ sudo addgroup sftpusers グループ `sftpusers' (GID 1002) を追加しています... 完了。 * Add User to Group [#ub1d4b61] add the user to the new group. &br; Run the usermod command to add the sftpuser to the sftpusers group.&br; $ sudo usermod -a -G sftpusers sftpuser * Change User’s Home Permissions [#y76c9edf] set new permissions on the sftpuser’s home directory. &br; This will allow the SFTP server to access these files. &br; First execute the chown command followed by the chmod command. &br; The sftpuser’s home will be the folder you access when you connect to the SFTP server.&br; $ sudo chown root:root /home/sftpuser $ sudo chmod 755 /home/sftpuser **確認 [#r58dd86c] $ ls -la ..|grep -i --color -e "sftpuser" drwxr-xr-x 2 root root 4096 7月 19 15:28 sftpuser 変更前は下記の状態 drwxr-x--- 2 sftpuser sftpuser 4096 7月 19 15:28 sftpuser * sftpユーザの鍵ファイルを準備しておく [#of33d1ff] **あらかじめsftpユーザの鍵ファイルid_rsa_of_sftp_user.pubを準備しておく [#ce12cd76] **sftpユーザの鍵ファイルをセットアップ [#me9fcabc] $ sudo bash # cd /home/sftpuser/ # mkdir .ssh # chown root:root .ssh # chown sftpuser:sftpuser .ssh # chmod 700 .ssh # cd .ssh # cat id_rsa_of_sftp_user.pub >> authorized_keys # chown root:root authorized_keys # chown sftpuser:sftpuser authorized_keys # chmod 600 authorized_keys **アップロード先用のディレクトリを設けておく [#he4c0e81] # mkdir /home/sftpuser/uploads # chmod 755 /home/sftpuser/uploads # chown sftpuser:sftpuser /home/sftpuser/uploads **確認 [#y5cc60c6] # ls -la /home/ | grep -i --color -e "sftpuser" drwxr-xr-x 15 root root 4096 7月 21 14:55 sftpuser # ls -la /home/sftpuser/ | grep -i --color -e "ssh" drwx------ 2 root root 4096 7月 21 14:56 .ssh drwx------ 2 sftpuser sftpuser 4096 7月 21 14:56 .ssh # ls -la /home/sftpuser/.ssh/ 合計 12 drwx------ 2 root root 4096 7月 21 14:56 . drwx------ 2 sftpuser sftpuser 4096 7月 21 14:56 . drwxr-xr-x 15 root root 4096 7月 21 14:55 .. -rw------- 1 root root 410 7月 21 14:56 authorized_keys -rw------- 1 sftpuser sftpuser 410 7月 21 14:56 authorized_keys * Edit the SSH Config File [#oadb980b] edit the sshd_config file and edit a few lines. &br; Open the configuration file using the gvim text editor as shown below.&br; $ sudo gvim -f /etc/ssh/sshd_config ** comment out Subsystem directive [#d2a671bf] original line 115 Subsystem sftp /usr/lib/openssh/sftp-server modified #Subsystem sftp /usr/lib/openssh/sftp-server ** PasswordAuthentication yes のコメントをはずす [#x075ad8b] original line 58 #PasswordAuthentication yes modified PasswordAuthentication yes ** Add New Lines at line 116 [#t9132eef] Subsystem sftp internal-sftp Match Group sftpusers ChrootDirectory /home/%u Match User sftpuser ChrootDirectory /home/sftpuser PasswordAuthentication no X11Forwarding no AllowTCPForwarding no ForceCommand internal-sftp ***まとめ [#nc81fd97] $ diff -uprN /etc/ssh/sshd_config_original /etc/ssh/sshd_config --- /etc/ssh/sshd_config_original 2022-11-23 16:38:19.000000000 +0900 +++ /etc/ssh/sshd_config 2023-07-21 17:14:57.110675372 +0900 @@ -26,6 +26,7 @@ Include /etc/ssh/sshd_config.d/*.conf # Logging #SyslogFacility AUTH #LogLevel INFO +LogLevel VERBOSE # Authentication: @@ -54,7 +55,7 @@ Include /etc/ssh/sshd_config.d/*.conf #IgnoreRhosts yes # To disable tunneled clear text passwords, change to no here! -#PasswordAuthentication yes +PasswordAuthentication yes #PermitEmptyPasswords no # Change to yes to enable challenge-response passwords (beware issues with @@ -112,7 +113,14 @@ PrintMotd no AcceptEnv LANG LC_* # override default of no subsystems -Subsystem sftp /usr/lib/openssh/sftp-server +#Subsystem sftp /usr/lib/openssh/sftp-server +Subsystem sftp internal-sftp + +Match Group sftpusers + ChrootDirectory /home/%u +Match User sftpuser + ChrootDirectory /home/sftpuser + PasswordAuthentication no + X11Forwarding no + AllowTCPForwarding no + ForceCommand internal-sftp # Example of overriding settings on a per-user basis #Match User anoncvs * Restart SSH [#peefac96] $ sudo service ssh restart * Configure the Firewall [#cdf6e9c0] configure the firewall using UFW to control access to our SFTP server. &br; Start by denying all incoming traffic, and allowing all outgoing.&br; $ sudo ufw default deny incoming $ sudo ufw default allow outgoing * Allow SSH [#n2a8e781] There are two options when allowing SSH through the firewall. &br; You can either allow any IP to access port 22 (not recommended). &br; Or you can only allow specific IP(s) through the firewall. &br; I recommend the second option as it offers higher security. &br; ** Allow SSH (All IP’s) [#x3dcb28e] If you want to allow any IP, run the following command.&br; $ sudo ufw allow ssh **Allow SSH (Specific IP’s) [#p636c4a0] If you want to only allow specific IP’s to access the server, &br; run the following command for each IP you want to have access. &br; You need to replace “IP-ADDRESS” with your own IP. &br; This is highly recommended as it offers the highest level of security.&br; $ sudo ufw allow from IP-ADDRESS to any port ssh * Enable UFW [#dfe32d36] After you have allow the IP’s (or everyone) who you want to have access, &br; you will need to enable UFW. Run the following command. $ sudo ufw enable * Check Firewall Status [#w3f411ac] To check the firewall status and verify your configuration. &br; Check it using the ufw status command. &br; If you allowed access to only specific IP’s you will see them in the “From” column.&br; $ sudo ufw status 状態: アクティブ To Action From -- ------ ---- 22/tcp ALLOW Anywhere 22/tcp (v6) ALLOW Anywhere (v6) *ubuntuのSFTPサーバ(SSH)設定で最も重要なこと [#p3ad73bb] 適切なパーミッションと所有者の設定です。SSH/SFTPは、権限が緩すぎるとセキュリティ上ログインを拒否(または警告)する仕組みになっています。&br; 以下に重要なポイントと、具体的なアクセス権限のコマンドをまとめました。&br; **重要な所有者・アクセス権のルール [#a9032c39] .sshディレクトリと鍵ファイルの権限が正しく設定されていないと、接続エラー(エラーコード: Permission denied など)の原因になります。&br; |ディレクトリ / ファイル|所有者(Owner)|グループ(Group)|アクセス権限(パーミッション)|h |ホームディレクトリ (~)|対象ユーザー|対象ユーザー|755 または 750| |.ssh フォルダ (~/.ssh)|対象ユーザー|対象ユーザー|700 (drwx------)| |公開鍵ファイル (authorized_keys)|対象ユーザー|対象ユーザー|600 (-rw-------)| |秘密鍵ファイル (id_rsa など)|対象ユーザー|対象ユーザー|600 (-rw-------)| **具体的な設定コマンド [#r107ea40] SSH/SFTPでログインする対象のユーザーでサーバにログインし、以下のコマンドを実行します。&br; ***1. .ssh ディレクトリの作成とパーミッション設定 [#x11feba7] ディレクトリが存在しない場合は作成し、厳格なアクセス権限を付与します。&br; # .sshディレクトリの作成 mkdir -p ~/.ssh # パーミッションを 700 (所有者のみ読み書き実行可能) に変更 chmod 700 ~/.ssh **2. 公開鍵(authorized_keys)の配置と権限設定 [#qa71526d] 手元のPCなどで作成した公開鍵(id_rsa.pub など)をサーバの ~/.ssh/authorized_keys として保存または追記します。&br; # 権限を 600 (所有者のみ読み取り・書き込み可能) に変更 chmod 600 ~/.ssh/authorized_keys **3. ディレクトリの制限とSSH設定(Chroot) [#b01e9f8f] 他のディレクトリへの移動を制限し、SFTP専用のルートディレクトリに閉じ込める(Chroot)場合、親ディレクトリの所有者と権限も重要になります。&br; &br; SFTP専用ディレクトリ(例: /home/sftpuser/data)を作成する場合の設定例です。&br; ***1) chroot 用ディレクトリの所有者を root にする [#oabb5884] sudo chown root:root /home/sftpuser sudo chmod 755 /home/sftpuser ※ホームディレクトリ(sftpuser)の所有者は root にする必要があります。&br; ***2) ファイルをアップロードするディレクトリを作成する [#lc5c06af] sudo mkdir /home/sftpuser/uploads sudo chown sftpuser:sftpuser /home/sftpuser/uploads sudo chmod 755 /home/sftpuser/uploads **3) /etc/ssh/sshd_config の設定 [#n1ad2fac] SFTPのアクセスを制限するために、設定ファイルの最下部などに以下の記述を追加します。&br; Match User sftpuser ChrootDirectory /home/sftpuser ForceCommand internal-sftp PasswordAuthentication no AllowTcpForwarding no X11Forwarding no &br; 設定変更後は、以下のコマンドでSSHサーバを再起動してください。&br; sudo systemctl restart ssh **その他の注意点 [#xf30b465] ***公開鍵のフォーマット: [#g6db214c] クライアントソフト(WinSCPやFileZillaなど)で接続する際、Tera Termなどで生成した拡張子が .ssh の鍵はOpenSSH形式と互換性がない場合があります。&br; OpenSSH形式の鍵(通常は id_rsa と id_rsa.pub)を使用してください。&br; ***ログの確認: [#y7eaa0bc] 接続がうまくいかない場合は、サーバ側のログ(/var/log/auth.log)を確認すると、どこで権限が弾かれているか&br; (例: Authentication refused: bad ownership or modes for directory など)の詳細が記録されています。&br;