Подписаться на блог

Change SSH port on Ubuntu 22 using Ansible

After updating Ubuntu 22.10 SSHd uses socket-based activation.
As a result, the sshd configuration port does not affect the listening port.
To change the port, we need to perform the following tasks:

- name: Create config folder
  file:
    path: /etc/systemd/system/ssh.socket.d
    state: directory
    recurse: true

- name: Create config file
  copy:
    dest: /etc/systemd/system/ssh.socket.d/listen.conf
    content: |
      [Socket]
      ListenStream={{ custom_ssh_port }}

- name: Reload systemd manager
  systemd:
    daemon_reload: yes

- name: restart ssh
  service:
    name: "ssh"
    state: restarted

- name: Update connection port
  set_fact:
    ansible_ssh_port: "{{ custom_ssh_port }}"