File sharing between Linux (black screen) and Windows

Setting up file sharing was relatively easy in the Linux desktop environment, but this time we will be setting up file sharing in a CUI environment. The main purpose is to exchange files with Windows.
Command line operation may not be intuitive for some users because it does not involve the use of a mouse. Especially if you are used to a graphical user interface (GUI), the text-based operation of the command line can seem a bit challenging. However, the command line is a very powerful and flexible tool that, once mastered, allows you to perform many tasks efficiently. It may take some time to get used to it, but many resources and tutorials are available online to help you learn.


The most common and easiest way to share files between Windows and Linux is to use “Samba”. Samba is recommended for the following reasons

  1. Compatibility: Samba is highly compatible with Windows because it implements the Windows sharing protocol SMB/CIFS. This allows Windows users to access files on a Linux server without installing any special software.
  2. Ease of Configuration: Samba is relatively easy to configure and can be easily set up on many Linux distributions; GUI tools are also available, which are more intuitive than editing configuration files directly.
  3. Flexibility: Samba is highly flexible in terms of security due to the ability to fine-tune permissions and access control lists (ACLs). Different users and groups can also be assigned different access rights.
  4. Extensive Support: Because Samba is so widely used, there are many guides and tutorials available online. This makes it relatively easy for even novice users to set up.

However, Samba is not the only way to share files in Linux. There are several other methods. Some of the main methods are listed below.

  1. Samba: Most commonly used for file sharing on Windows networks; suitable for sharing files between Linux and Windows.
  2. NFS (Network File System): Used primarily to share files between UNIX-like systems. It is relatively easy to set up and provides fast data transfer.
  3. SSHFS (SSH Filesystem): file sharing using the SSH protocol. It is highly secure and suitable for sharing files over the Internet.
  4. rsync: A tool specialized for file synchronization. Files can be transferred over a network and only differences can be synchronized.
  5. FTP/SFTP: The classic way to share files using file transfer protocols; SFTP is based on SSH for enhanced security.
  6. WebDAV (Web-based Distributed Authoring and Versioning): This method shares files using the HTTP protocol. It is suitable for access via the Internet and allows remote file management.

When configuring Samba in the CUI (command line interface) environment of Ubuntu 22.04 LTS (or similar versions), you will primarily need to use command line tools and edit the configuration files. Here are the basic steps

  1. Install Samba: Since Samba is not installed by default on Ubuntu, it must first be installed. This can be done with the following command

    sudo apt update
    sudo apt install samba
  2. Samba configuration: Samba configuration is done by editing the file /etc/samba/smb.conf Before editing this file, it is recommended to backup the existing configuration.

    sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup

    Then edit the file using a text editor (e.g. nano or vim ).

    sudo nano /etc/samba/smb.conf
  3. Configure shared directories: Add the settings for the directories you want to share to the end of the smb.conf file. For example, to share a specific directory, write the following

    [share name]
    path = /path/to/shared/folder
    read only = no
    browsable = yes

    Where “share name” is the name you want to access from Windows, and “path” is the path to the directory you want to share.
  4. Samba User Setup: In order to share files with Samba, a Samba user must be created. First, create a regular user on your Linux system, then set a password for Samba.

    sudo adduser [username]
    sudo smbpasswd -a [username]
  5. Restart S amba: Restart Samba for the configuration to take effect.

    sudo systemctl restart smbd

If you follow these steps, you can configure Samba in the Ubuntu CUI environment and share files with Windows. However, for security reasons, be careful about access permissions for the directories and files to be shared. Firewalls and routers must also be properly configured.

Samba user configuration can also be done with the following command

sudo pdbedit -a username

What is the difference between sudo pdbedit -a username and sudo smbpasswd -a username?

sudo pdbedit -a username

  • Function: pdbedit is a user management tool for Samba.-Use the -a option to add a new Samba user. This command will create a new entry in the user’s Samba password database (usually stored in a backend such as smbpasswd file, tdbsam, or ldapsam ).
  • Details: pdbedit can be used to manage detailed information about a user (user ID, group ID, home directory, login shell, etc.).

sudo smbpasswd -a username

  • Function: The smbpasswd command is a tool for setting passwords for Samba users. The -a option adds a new user to the Samba password database. This command is primarily used to set or change a user’s password.
  • Details: The smbpasswd command is a basic tool for managing Samba user passwords, including adding, changing, disabling, and deleting passwords.

Main differences

  • User information management:
    • pdbedit is more versatile, as it can manage detailed user information.
    • smbpasswd is mainly dedicated to password management.
  • Scope of Usage:
    • pdbedit is used when detailed user management and configuration is required.
    • smbpasswd is used to simply add users or change passwords.

Which should you use?

  • If you only need to add basic Samba users or set passwords, smbpasswd is easier and more suitable.
  • If you need to manage detailed user information, you may want to use pdbedit.

About shared directories

It is important to set appropriate file ownership and access rights for /path/to/shared/folder using chown and chmod. These settings are necessary to ensure security and proper access control for directories and files shared via Samba. Here are some basic guidelines

  1. Ownership and group settings(chown):
    • Use the chown command to set the owner of a shared directory to a Samba user (or appropriate user).
    • Example: sudo chown [user name]:[group name] /path/to/shared/folder
  2. Set access permissions(chmod):
    • Use the chmod command to set access privileges on a shared directory.
    • If the shared directory is read-only, set appropriate access privileges (e.g., chmod 755 /path/to/shared/folder ).
    • If the directory must be read-write, you may set more lax access privileges (e.g., chmod 777 /path/to/shared/folder ). However, this is usually not recommended, as it increases security risks.
  3. Security and Access Control:.
    • Proper use of chmod and chown can protect directories from unauthorized access and control access so that only authenticated users can access them.
    • To ensure security, it is important to follow the “principle of least privilege,” setting only the minimum necessary permissions.
  4. smb.conf settings:.
    • The smb.conf file contains settings for Samba service behavior and sharing, which are separate from file system-level access privileges.
    • The Samba configuration defines access privileges and sharing behavior over the network, but the actual file system access privileges depend on the Linux file system.

These settings allow for secure file sharing through Samba. Always keep security in mind and grant only the minimum necessary access privileges.

We will show the actual installation procedure later, but once samba was installed, the service was up and running. Many Linux distributions seem to start the service automatically. This automatic startup is due to the fact that Samba is enabled by default during installation. To check if the service is running after installation, run the following command

sudo systemctl status smbd

Actual work done

sudo apt install samba
sudo mkdir share
sudo vi /etc/samba/smb.conf 
Add the following

[share]
  comment = Samba on Ubuntu
  path = /home/mamu/share
  read only = no
  browsable = yes
sudo systemctl restart smbd

If you are using Samba and an Ubuntu system with UFW (Uncomplicated Firewall) enabled, you will need to open certain ports.

  • TCP 139: NetBIOS session service
  • TCP 445: SMB protocol
  • UDP 137: NetBIOS Name Service
  • UDP 138: NetBIOS datagram service

To open these ports on the UFW firewall, use the following command

sudo ufw allow 139/tcp
sudo ufw allow 445/tcp
sudo ufw allow 137/udp
sudo ufw allow 138/udp

The ports required by Samba depend on the version of Samba you are using and your configuration, but in general the following ports are required

  • TCP 445: This is the main port for the SMB protocol and is the primary port used by the latest versions of Samba.
  • TCP 139: This is the port for NetBIOS session services. It may be needed for older clients or when using certain configurations.

UDP 137 (NetBIOS Name Service) and UDP 138 (NetBIOS Datagram Service) are needed for older network environments and certain NetBIOS-dependent features, but when using Samba in a modern network, it is often sufficient to open only TCP 445.

The most important port is TCP 445. Consider opening other ports depending on your own network requirements and whether you need support for older devices.

The actual command to open the ports

sudo ufw allow 445/tcp

Access the ubuntu shared directory by writing the following to “Run” on a Windows PC.

\\192.168.0.140

When “Enter network resource information” is displayed, enter the user information configured in samba. I checked the “Record credentials” checkbox and pressed the OK button.

Go to “Control Panel” -> “User Accounts” -> “Credentials Manager” and check it, it should be added automatically.

The following is a reminder.

When accessing a Samba share on Windows, you do not necessarily need to create a Samba user with the same name as your Windows user name. However, if your user name is different, you will need to enter your Samba credentials when accessing the network drive; if your Windows user name and your Samba user name are the same, the authentication process will be smoother and more convenient.

The following methods can be used to simplify the process of accessing Samba shares in Windows.

  1. Use the Windows Credentials Manager:.
    • Windows has a feature called “Credentials Manager” where you can save your user name and password to avoid having to enter your login information each time.
    • Go to “Control Panel” -> “User Accounts” -> “Credentials Manager” and add your Samba server credentials under Windows Credentials.
  2. Map it as a network drive:.
    • In Windows, you can map a Samba share as a network drive to eliminate the need to re-enter your credentials each time you connect.
    • Open “This Computer”, select “Map to Network Drive”, assign a drive letter and enter the path to the shared folder. Select the Use credentials when connecting option and enter your user name and password.
  3. Use the same user name and password:.
    • By setting the Windows user name and password to be the same as the user name and password on the Samba server, authentication will be performed automatically.

Using these methods will reduce the need to enter login information each time you access a Samba share from Windows.

Checking it out in practice

You have successfully accessed the ubuntu shared directory.

You may be able to connect to Samba without creating a special Samba-only user. This is the case in the following situations

  1. Anonymous or guest access is allowed: If anonymous (guest) access is allowed in the Samba configuration, you can access the shared folder without specifying a user name or password.
  2. Using an existing Linux user account: Samba may be configured to use an existing Linux user account for access control. In this case, you can access the Samba share with your existing Linux user name and password.

Create a folder on a Windows computer. However, an error occurs: set write permissions on the ubuntu share directory.

sudo chmod 777 share

Check if the Ubuntu PC is visible on the Windows network. However, it does not show up; I can access it when I type the IP address directly, but I need to set it up so that it can be listed on the network. The following Github page solves the problem.

https://github.com/christgau/wsdd

wsdd implements the Web Service Discovery host daemon, a program that allows Samba hosts (e.g., local NAS devices) to be discovered by the Windows Web Service Discovery client. This is useful as a way to make hosts running Samba visible again in the Windows “Network (Network Neighborhood)” view, since NetBIOS discovery is no longer supported in Windows.

Specific instructions on how to install and use wsdd are detailed in the wsdd repository on GitHub. The repository provides installation instructions for various Linux distributions and FreeBSD, and requires Python 3.7 or later. wsdd (Web Services Dynamic Discovery) requires certain ports to be open wsdd (Web Services Dynamic Discovery). These typically include the following ports

  • UDP 3702: This is the port used by the WS-Discovery protocol.
  • TCP 5357: This is used for HTTP service publishing via WSDAPI.

The repository contains detailed information related to the use of wsdd, including usage and technical instructions, known issues, security precautions, etc. For Ubuntu 22.04 LTS, wsdd is included in the official Universe package repository, so adding the repository is No repository addition is required. It can be installed directly using the following command

sudo apt install wsdd

On Ubuntu 24.04, the following command is required to install wsdd, otherwise errors will occur during service checks after installation.

sudo apt install wsdd2

When I checked the status, it was running.

sudo systemctl status wsdd
Or
sudo systemctl status wsdd2

wsdd.service – Web Services Dynamic Discovery host daemon
Loaded: loaded (/lib/systemd/system/wsdd.service; enabled; vendor preset: >
Active: active (running) since Sun 2023-11-19 19:04:10 JST; 48s ago
Docs: man:wsdd(8)
Main PID: 6018 (python3)
Tasks: 1 (limit: 9321)
Memory: 16.1M
CPU: 210ms
CGroup: /system.slice/wsdd.service
mq6018 python3 /usr/sbin/wsdd –shortlog –chroot=/run/wsdd

Nov 19 19:04:10 g570 systemd [1] : Started Web Services Dynamic Discovery host d>
Nov 19 19:04:10 g570 wsdd [6018]: WARNING: no interface given, using all interf>

I installed wsdd2.

mamu@dynabook:~$ sudo systemctl status wsdd2
wsdd2.service – WSD/LLMNR Discovery/Name Service Daemon
Loaded: loaded (/usr/lib/systemd/system/wsdd2.service; enabled; preset: en>
Active: active (running) since Tue 2024-05-21 13:07:04 JST; 12s ago
Main PID: 3424 (wsdd2)
Tasks: 1 (limit: 4478)
Memory: 392.0K (peak: 3.7M)
CPU: 335ms
CGroup: /system.slice/wsdd2.service
mq3424 /usr/sbin/wsdd2

May 21 13:07:04 dynabook systemd [1] : Started wsdd2.service – WSD/LLMNR Discover>
May 21 13:07:04 dynabook wsdd2. [3424] : cannot read additional dns hostn wsdd2.service – WSD/LLMNR Discover> : cannot read additional dns hostnames from>
May 21 13:07:04 dynabook wsdd2 [3424] : cannot read netbios aliases dynabook wsdd2 [3424] : cannot read netbios aliases from testparm
May 21 13:07:05 dynabook wsdd2 [3424]. : starting.

You also need to allow traffic on certain ports.

sudo ufw allow 3702/udp
sudo ufw allow 5357/tcp

The Ubuntu PC appears on the Windows PC.

But now I have a problem connecting: I can connect by IP address, but not by computer name.

So I opened the following ports in addition to 445/tcp to Ubuntu

sudo ufw allow 139/tcp
sudo ufw allow 137/udp
sudo ufw allow 138/udp

Adding these ports solved the previous problem. I am sure there are other ways to do this.

To access a Windows shared folder from the Ubuntu CUI (command line interface), use the Samba client tool to access the Windows shared folder from the Ubuntu CUI (command line interface). Follow the steps below to set it up.

  1. Install the necessary packages: If the Samba Client Tool is not already installed, it can be installed with the following command

    sudo apt update
    sudo apt install cifs-utils
  2. Mounting a shared folder: To access a Windows shared folder, first create a mount point (directory to access)

    sudo mkdir /mnt/windows_share

    Next, mount the Windows shared folder with the following command

    sudo mount -t cifs -o username=your_username,password=your_password //WindowsPC_IP_Address/share_name /mnt/windows_share

    Here, your_username and your_password are the credentials used to access the Windows share. Please replace WindowsPC_IP_Address and share_name with the appropriate values for your setup.

This will allow you to access the Windows shared folder from Ubuntu to read and write files. For security reasons, you can also use a credential file instead of entering the password directly on the command line. account (email address and password), you can still connect to a Samba share using those credentials. However, depending on the share settings and access privileges, it may be smoother to connect using a specific local account. Actual accessibility depends on the share’s settings and network security policies.

  1. Access mounted shared folders: Mounted shared folders reside at the specified mount point ( /mnt/windows_share in this example). You can navigate to this directory from a file browser or from the command line to view and edit the contents of the shared folder. From the command line, you can access it with the following command

    cd /mnt/windows_share
    ls

    This will display a list of files and directories in the shared folder.
  2. File operations: Through this mount point, you can perform file operations using the usual Linux commands ( cp, mv, rm, etc.).

If the Windows IP address changes, the mount settings on Ubuntu will also need to be updated. Specifically, the following steps are required

  1. Dismount existing mounts: First, dismount the current mount. To do this, use the umount command.

    sudo umount /mnt/windows_share
  2. Remount with a new IP address: Next, remount the shared folder using the new IP address.

    sudo mount -t cifs -o username=your_username,password=your_password //New_WindowsPC_IP_Address/share_name /mnt/windows_share

    Here, your_username and your_password are the credentials used to access the Windows share. Please replace New_WindowsPC_IP_Address and share_name with the appropriate values for your setup.

If IP addresses change frequently, it may be useful to consider assigning a static IP to the Windows machine or using a DNS name to mount it. Using a static IP or DNS name eliminates the need to change the mount settings each time the IP address changes.

If the Windows IP address changes, it may still be resolved by computer name. If name resolution (through DNS or NetBIOS) is working properly on your network, you can access the share using the computer name. In this case, use the computer name instead of the IP address in the mount command.

sudo mount -t cifs -o username=your_username,password=your_password //Computer_Name/share_name /mnt/windows_share

Here, your_username and your_password are the credentials used to access the Windows share. Please replace Computer_Name and share_name with the appropriate values for your setup.

However, this depends on your network configuration and environment. DNS and NetBIOS name resolution must be properly configured in the network.

What to do when the following message appears

umount: /mnt/windows_share: target is busy.

It means that the mount point /mnt/windows_share is currently in use and cannot be released. To resolve this, try the following steps.

The easiest way is to dismount in a directory other than the mount point /mnt/windows_share. If this does not solve the problem, there is the next step.

You can use the lsof command to see which process is using the mount point. the lsof command provides information about the process associated with the file being opened.

First, check to see if the lsof command is installed, and if not, install it.

sudo apt update
sudo apt install lsof

Then, check the process using the mount point with the following command

sudo lsof /mnt/windows_share

This will give you a list of processes using the /mnt/windows_share directory. After safely terminating those processes, try the umount command again.

Please share if you like it!
TOC