To view open ports on a Linux system, you can use the ss or netstat command. The ss command is generally recommended for modern systems as it is faster and more feature-rich compared to netstat, which is considered deprecated. Here are examples of how you can use these commands:
Using ss
To list all open ports:
ss -tuln
Explanation:
-t: Display TCP ports.
-u: Display UDP ports.
-l: Display listening ports.
-n: Show numerical addresses instead of resolving host names.
Using netstat
If your system still supports netstat, or if you prefer using it:
netstat -tuln
Explanation:
User
how can create new use in linux command
Assistant
To create a new user in Linux, you generally use the useradd command with administrative privileges, often by preceding it with sudo. Here's the basic syntax:
sudo useradd [options] username
Here's a simple example to create a new user without any options (other than specifying the username):
User
how can login to new user?
Assistant
To log in as a new user on a Linux system, you have a few options depending on whether you wish to switch to the new user account from another account on the same system, or if you're logging in directly from a login prompt or remotely.
1. Switching Users in the Terminal
If you are already logged in and have access to a terminal, you can switch to another user using the su (substitute user) or sudo commands.
Using su
To switch to the new user, you can use:
User
how can delete user?
Assistant
To delete a user in Linux, you can use the userdel command. This command must be run with superuser privileges, so you will usually precede it with sudo. Here’s how you can delete a user:
sudo userdel username
Replace username with the actual username of the account you want to delete.
Deleting User Along With Home Directory
If you also want to remove the user's home directory along with the user account, you can add the -r option:
sudo userdel -r username
This command will remove the user's home directory and the user's mail spool (if it exists) in addition to the user account itself.
-t: Display TCP ports.
-u: Display UDP ports.
-l: Display listening sockets.
-n: Show numerical addresses instead of trying to determine symbolic host, port, or user names.
Installing net-tools for netstat
If you're on a newer Linux distribution and netstat isn't available, you might need to install net-tools. For Debian-based systems like Ubuntu:
sudo apt update
sudo apt install net-tools
For Red Hat-based systems like CentOS or Fedora:
sudo dnf install net-tools
Or, for older versions of Red Hat systems using yum:
sudo yum install net-tools
These commands should help you view open ports on your Linux system.
sudo useradd newusername
However, when you create a user this way, the user won't have a password by default, and you'll need to set one for the user to be able to log in. You can set a password for the new user with:
sudo passwd newusername
You will be prompted to enter and confirm the password.
Common Options
-m or --create-home: Create the user's home directory if it doesn't exist.
-s or --shell: Define the user's default login shell.
-g or --gid: Specify the initial login group for the user.
-G or --groups: List of additional groups the user should be added to.
-d or --home: Home directory of the new account.
Full Example
To create a new user with a home directory and specify the bash shell as the user's login shell, the command would be:
sudo useradd -m -s /bin/bash newusername
And then set the user's password:
sudo passwd newusername
Using adduser Command
There's also an adduser command available on many Linux distributions, which is a more interactive way to create a new user, and it essentially runs useradd with some default options plus it takes care of creating the home directory, copying initial configuration files, and setting up the user environment more thoroughly:
sudo adduser newusername
This command might ask you to set a password during the process and prompt you to enter additional information about the user. It generally provides a more user-friendly way to add new users to the system.
The availability of useradd, adduser, or both, and the specific features and options they offer, can vary between different Linux distributions, so it's a good idea to consult the man pages (man useradd and man adduser) on your system for detailed information.
su - newusername
The - argument provides an environment similar to what the user would expect had they logged in directly. You will be prompted to enter the password of the new user account.
Using sudo
If you prefer using sudo and if your account has permissions to run commands as other users, you can use:
sudo -i -u newusername
This will switch to the new user without asking for the new user's password but instead may ask for your current user's password, depending on your sudo configuration. The -i ensures a login environment similar to a normal login session.
2. Graphical Login
If you are at the graphical login screen:
Select the user account you created from the list of users, if displayed.
Enter the password for that user.
Press Enter or click the login button (the exact method may vary based on your display manager).
3. Logging in Remotely
If you need to log in to the user account over the network (e.g., via SSH), use the ssh command from another machine:
ssh newusername@hostname
Replace newusername with the username of the account you wish to login with, and hostname with the IP address or domain name of the Linux machine. You will be prompted to enter the password for newusername.
SSH needs to be installed and running on the remote machine for this to work, and the machine needs to be accessible over the network.
Note:
Before deleting a user, you might want to ensure that the user is not running any processes. You can check running processes for the user by using the ps command:
ps -u username
Deleting a user is a critical operation and can result in data loss (for example, by removing the home directory). Always make sure you have a backup of any important data before proceeding.
In some cases, especially on servers or multi-user systems, you'll want to be careful about deleting users who own critical files or processes. You might first need to reassign ownership of those files or properly shut down the user's processes.
Some Linux distributions have the deluser command, which basically functions like userdel but can be more user-friendly and may offer additional options.
Remember, once a user account is deleted, recovering the account and any associated data (if -r was used) can be difficult or impossible, so always double-check before proceeding.