File sharing and folder sharing between two Linux based on Samba

Client to mount

sudo mount -t cifs //192.168.122.1/WinD /D -o username=username,password=password

insert image description here

complete steps

If you wish to set up and access Samba file shares from the command line, you can follow these steps:

  1. Install Samba:
    First, make sure you have installed the Samba package on your Linux host. You can install it using the package manager that suits you. On Debian based distributions, the following command can be used:

    sudo apt-get update
    sudo apt-get install samba
    

    On Red Hat based distributions, the following command can be used:

    sudo yum install samba
    
  2. Create a shared directory:
    Select a directory on a host that you want to share with other hosts. Suppose you choose to /home/user/sharedshare the directory. You can create a directory with the following command:

    mkdir /home/user/shared
    
  3. Set Samba user passwords:
    In Samba, you need to set passwords for users so they can access shares. Set the Samba user password with the following command:

    sudo smbpasswd -a username
    

    Replace "username" with the username you want to add, and follow the prompts to set a password.

  4. Configure Samba:
    Edit the configuration file of Samba and add shared configuration. On most Linux systems, the configuration files are located at /etc/samba/smb.conf. Open the file with a text editor:

    sudo nano /etc/samba/smb.conf
    

    Add the following at the end of the file:

    [shared]
    path = /home/user/shared
    read only = no
    guest ok = yes
    

    This will create a share called "shared" that maps to /home/user/sharedthe directory and allows anonymous access.

  5. Start the Samba service:
    Start the Samba service to make the configuration take effect:

    sudo systemctl start smbd
    
  6. Accessing the share:
    On another Linux host, you can access the share using the following command:

    smbclient //<IP Address of Samba Server>/shared -U username
    

    Replace <IP Address of Samba Server>with the IP address of the host machine running the Samba server, and replace "username" with the username of the Samba user you set up.

    After entering this command, you will be prompted for the password for the Samba user. After entering the password, you will enter the command line interface of the Samba share.

    In this command line interface, you can use commands similar to FTP (eg, ls, getetc. put) to browse and manipulate shared files.

This way, you can set up and access Samba file shares from the command line. Please note that this is just a simple setup example, you can perform more advanced configurations as needed, such as adding authentication, permission control, etc.

Mounting method shared directory

Yes, you can access Samba shares by mounting them. In this way, you can use the shared directory as if you were accessing the local file system. Here are the steps to access the Samba share based on the mount:

  1. Create a local directory in which to mount the Samba share:

    mkdir /mnt/shared
    
  2. Mount mountthe Samba share with the command:

    sudo mount -t cifs //<IP Address of Samba Server>/shared /mnt/shared -o username=username,password=password
    

    Replace <IP Address of Samba Server>with the IP address of the host running the Samba server, and replace "username" and "password" with the username and password of the Samba user you set up.

    After running the command, the Samba share will be mounted to /mnt/sharedthe directory.

  3. Now you can /mnt/sharedaccess files in the Samba share through the directory. For example, use lsthe command to list files in a shared directory:

    ls /mnt/shared
    

    You can read, write, and perform other operations on files in the shared directory just like you operate on the local file system.

  4. Unmount the share:
    If you want to unmount the Samba share, you can use the following command:

    sudo umount /mnt/shared
    

    This will unmount the mounted share.

Using the mount method to access the Samba share enables you to operate the shared file in the local file system conveniently, just like operating the local file. This is useful for situations where shares need to be accessed frequently.

Guess you like

Origin blog.csdn.net/a772304419/article/details/132098612