zaki work log

作業ログやら生活ログやらなんやら

Ubuntu版FortiClientを使ってCLIでVPN接続

Windows版だと(特に業務で)割とよく使ってると思うFortiClient、公式でLinux版もリリースされているのでUbuntuで接続を試してみた。

www.fortinet.com

Ubuntuは22.04向けの手順だが24.04でも全く同じ手順でインストール可能。
他にはCentOS 7+ / RHEL7+ / Fedora 向けのパッケージが、2025.03時点ではFortiClient 7.4が公開されている。

インストール

公開されてるのは22.04用(で、他と違って「and newer」と書かれてない)だけど、手元の環境の24.04にも特に問題なくインストールして動作した。

FortiClient Linux Product Downloads Information

コマンドとファイルの内容が混在してるのでコピペできるように修正した手順が以下。

wget -O - https://repo.fortinet.com/repo/forticlient/7.4/ubuntu22/DEB-GPG-KEY | gpg --dearmor | sudo tee /usr/share/keyrings/repo.fortinet.com.gpg
cat <<__EOL__ | sudo tee /etc/apt/sources.list.d/repo.fortinet.com.list
deb [arch=amd64 signed-by=/usr/share/keyrings/repo.fortinet.com.gpg] https://repo.fortinet.com/repo/forticlient/7.4/ubuntu22/ stable non-free
__EOL__
sudo apt-get update
sudo apt-get install -y forticlient

インストールするとサービスとして動作する。

ubuntu@ip-10-1-1-221:~$ cat /etc/systemd/system/multi-user.target.wants/forticlient.service 
[Unit]
Description=Forticlient Scheduler
Requires=dbus.service
Wants=dbus.service
After=dbus.service

[Service]
Type=simple
ExecStart=/opt/forticlient/fctsched
User=root
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5
StartLimitInterval=300
StartLimitBurst=30
KillMode=mixed

[Install]
WantedBy=multi-user.target

VPN接続

VPN接続するには、プロファイルを作って、それを使って接続、という手順になっており、基本的にWindows版のFortiClientと同様。
設定は非rootユーザーで実行(rootで実行するとエラーになる)し、保存先は(だいぶ探してみた結果)SQLiteのDBファイルになっている。

プロファイルの作成

プロファイル「sample-conf」を作成する。

ubuntu@ip-10-1-1-221:~$ forticlient vpn edit sample-conf
=====================
Create new VPN profile: sample-conf
=====================
Type (1.SSL VPN / 2.IPsec VPN) [default=1]: 1
Remote Gateway: <接続先アドレス>
  Port [default=443]: <接続先ポート>
Authentication (1.prompt / 2.save / 3.disable) [default=1]: 2
  Username: <接続ユーザー名>
Certificate Type (1.local (pkcs12) / 2.smartcard (pkcs11) / 3.disable) [current=disable]: 
Unable to use system's key store: The name org.freedesktop.secrets was not provided by any .service files.
DONE.

確認

ubuntu@ip-10-1-1-221:~$ forticlient vpn list
VPNs:
  Personal VPNs:
    sample-conf

ubuntu@ip-10-1-1-221:~$ forticlient vpn view sample-conf
VPN: sample-conf
  Remote Gateway: <入力した接続先アドレス>:<入力した接続先ポート>
  Client Certificate: None
  Authentication:
    Username: <入力したユーザー名>
  Single Sign On (SSO) for VPN Tunnel: Disabled
  Auto Connect: Disabled
  Always Up: Disabled
  Save Password: Disabled

接続

ubuntu@ip-10-1-1-221:~$ forticlient vpn connect sample-conf
Connect VPN: sample-conf
Unable to use system's key store: The name org.freedesktop.secrets was not provided by any .service files.
Password: <接続パスワードを入力する>
Status: Connecting...
=====================
Confirmation Required
---------------------
You are connecting to an untrusted server, which could put your confidential information at risk. Would you like to connect to this server?

Hostname: <接続先>

Reason: X509 verify certificate failed

Certificate:
        Subject name: ...
        Fingerprint (SHA1): ...

=====================
Confirm (y/n) [default=n]:Confirm (y/n) [default=n]:y
Status: Connected
DONE.
ubuntu@ip-10-1-1-221:~$ 

これで接続状態になる。

接続状態の確認

ubuntu@ip-10-1-1-221:~$ forticlient vpn status
Status: Connected
  VPN name: sample-conf
  Username: <接続ユーザー名>
  IP: <接続先>
  Sent bytes: 2914
  Recv bytes: 8562
  Duration: 00:01:52
ubuntu@ip-10-1-1-221:~$ 

切断

ubuntu@ip-10-1-1-221:~$ forticlient vpn disconnect sample-conf
Status: Disconnected
DONE.
ubuntu@ip-10-1-1-221:~$ 
ubuntu@ip-10-1-1-221:~$ forticlient vpn status
Status: Not Running
ubuntu@ip-10-1-1-221:~$ 

Appendix

ドキュメント

docs.fortinet.com

設定ファイルの場所

いろいろ探した結果、おそらく/var/lib/forticlient/config.dbが設定ファイル。
(ここ以外は分からなかったので、これがマスタデータなのかコピーなのかは厳密には不明)

形式はSQLite3のファイル。

root@ip-10-1-1-221:~# file /var/lib/forticlient/config.db 
/var/lib/forticlient/config.db: SQLite 3.x database, last written using SQLite version 3043002, file counter 127, database pages 31, cookie 0x58, schema 4, UTF-8, version-valid-for 127

中を確認すると、設定した接続先情報が保存されている。

root@ip-10-1-1-221:~# sqlite3 /var/lib/forticlient/config.db 
SQLite version 3.45.1 2024-01-30 16:01:20
Enter ".help" for usage hints.
sqlite> .table
antivirus                  sslvpn_connections       
ecdata                     system                   
endpoint_control           vpn                      
fctdata                    vulnerability_scan       
forticlient_configuration  webfilter                
removable_media_access     ztna                     
sandboxing                 ztna_rules               
sqlite> select * from vpn;
options.current_connection_name||
options.autoconnect_tunnel||
options.autoconnect_only_when_offnet||
options.keep_running_max_retries||
options.allow_personal_vpns||
options.disable_connect_disconnect||
options.minimize_window_on_connect||
options.inherit_local_dns||
options.dns_service_resetting_interval||
modified_time|1741936090|
sslvpn.options.enabled||0
sslvpn.options.block_ipv6||0
sslvpn.options.warn_invalid_server_certificate||1
options.suppress_vpn_notification||
sslvpn.connections.connection|[{"keep_running":false,"name":"sample-conf","prompt_certificate":false,"prompt_username":false,"server":"<接続先アドレス>:<接続先ポート>","ui":{"save_password":false,"save_username":true,"show_alwaysup":true,"show_autoconnect":true,"show_remember_password":true},"username":"<接続ユーザー名>"}]|

rootで実行すると?

この通り基本的に操作不可

root@ip-10-1-1-221:~# forticlient vpn edit sample-conf
Please run this command under non-root user.
root@ip-10-1-1-221:~#
root@ip-10-1-1-221:~# forticlient vpn list
Please run this command under non-root user.

コメント

WindowsのFortiClientはGUIなので自動化しづらいけど、LinuxだとCLIで操作できるのでいろいろ捗りそう。
ただネックなのは設定も接続も対話式の操作になるので、ここは工夫が必要。

AnsibleのexpectとかAnsibleのexpectとかAnsibleのexpectとかを使うとかね。

zaki-hmkc.hatenablog.com