Closing an open ssh port in Linux with one line [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I often ssh tunnel into Rstudio on a server I have set up. I'm trying to devise a single command that I can use to close the ssh port. I know that I can find the PID for localhost:1234 with:
sudo lsof -i :1234
And I also know that I can kill the process with:
sudo kill $(sudo lsof -t -i:1234)
The issue is that if I have Chrome open to run Rstudio server, the 2nd command will kill the open Chrome browswer as well. Is there a way to modify the 2nd command so that I close the open ssh port, but not the Chrome browser? There are two PID numbers, so I could theoretically grep for 'ssh' but I'm not sure how.
EDIT FOR CLARITY:
For example, I get the following output from the first command. I want to modify the 2nd command so that I can kill PID 15834, but not 30117. Apologies, I hope that makes more sense.

try this
sudo kill $(sudo lsof -t -i:1234 -c ssh)
-c => selects the listing of files for processes executing the command that begins with the characters of c.

Just firewall the port:
sudo iptables -I INPUT -p tcp --dport 1234 -j DROP

Related

Auto accept "Configuring iptables-persistent" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
i'm created bash script where i need to configure network, but in one of stage he ask me to save new rule, how to prevent it and set "Yes" automatically.
sudo iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 1443
sudo apt-get -y install iptables-persistent
sudo service netfilter-persistent save
message where he ask to save
For non-terminal programs
Actually, you can always automatize input of eny word with several methods:
yes word | command
(in this word will be entered as an input of command)
or using expect, if you need more sophisticated dialog.
For terminal programs (your case)
For interactive sessions you can try it this way:
start it in a tmux session
send "Enter" using tmux send-keys
That is exactly what you want.
tmux send-keys Enter
Everything combined:
sudo tmux -L dialog-session new-session -d service netfilter-persistent save
sudo tmux -L dialog-session send-keys Enter
(actually you don't need sudo here, but I use sudo because of your sudo)

scp to another linux system via ssh where not using port 22 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I'm trying to copy a folder from one system to another using scp (secure copy) however I cannot work out how to specify the port.
I understand that to scp, I can...
scp /file/location/folderName user#192.***.*.***:/destination/location/
however I have changed the destination system's SSH port so I get get this error...
ssh: connect to host ..*.*** port 22: Connection refused lost
connection
Is there some sort of -p command I can use to specify port number?
I also tried adding the port like this...
scp /file/location/folderName user#192.***.*.***:>>portNumHere<</destination/location/
Updated with solution...
I was struggling not only with using the wrong -P but also where to place. I know understand and this works for me...
scp -r -P >>portNumHere<< /file/location/folderName user#192.***.*.***:/destination/location/
Use a capital -P port. The lower case -p option conflicts with the -p "preserve" option from cp.
Per the man page:
scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user#]host1:]file1 ... [[user#]host2:]file2
...
-P port
Specifies the port to connect to on the remote host. Note that
this option is written with a capital ā€˜Pā€™, because -p is
already reserved for preserving the times and modes of the
file.

How to set up an SSH Server on OS X [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm planning on making a Bash script that sets up an SSH server. The script is only meant to work on a computer running OS X. With the research I have conducted it seems like you have to use the GUI to enable SSH. Is their a way to enable SSH through Terminal and then create a script that does so?
You can enable it from the command line (or a shell script) with:
sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
You might also want to regulate access to the ssh service with the com.apple.access_ssh group:
sudo dseditgroup -o edit -a usernametoallow -t user com.apple.access_ssh
sudo dseditgroup -o edit -a otherusernametoallow -t user com.apple.access_ssh
sudo dseditgroup -o edit -a groupnametoallow -t group com.apple.access_ssh
...after which only usernametoallow, otherusernametoallow, and members of groupnametoallow will be able to ssh into the Mac.

Telnet File Transfer between two linux machines [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I want to send a file from one Linux machine with IP suppose "192.168.2.25" to other Linux machine that's a server "192.168.2.110"
how can i do that by using Telnet command??
A simple option is to use netcat (nc). This is particularly useful on stripped down Linux systems where services like ssh and ftp are turned off.
On destination machine run the following command: nc -l -p 1234 > out.file
On source machine run the following command: nc -w 3 <dest-ip-adr> 1234 < out.file
For more details look, for example, here.
There are also netcat implementations for Windows, e.g. ncat.
While it may not be possible with only telnet, it is possible with telnet and netcat. Some of the examples above just referenced using netcat, but there have been times when I was on an old machine that was still in production that had telnet but not netcat. In this case, you can set netcat to listen on a newer, remote machine and telnet the file to it.
On the newer remote machine:
netcat -l <PORT> > OUTPUT.FILE
On the older telnet only machine:
cat FILE | telnet REMOTE-HOST PORT
Note that this works with text files. If you have a binary file of some sort you would need to do further manipulation on both ends.
Telnet just gives you a remote terminal session. The best you could do is telnet, open a new file in an editor and copy/paste the text from the local machine.
To copy files use something like rsync, scp, rcp or ftp.

Freeing up a TCP/IP port? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 12 months ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
netstat -tulnap
shows me what ports are in use. How to free up a port in Linux?
As the others have said, you'll have to kill all processes that are listening on that port. The easiest way to do that would be to use the fuser(1) command. For example, to see all of the processes listening for HTTP requests on port 80 (run as root or use sudo):
# fuser 80/tcp
If you want to kill them, then just add the -k option.
To kill a specific port in Linux use the below command
sudo fuser -k Port_Number/tcp
replace Port_Number with your occupied port.
In terminal type :
netstat -anp|grep "port_number"
It will show the port details. Go to last column.
It will be in this format . For example :- PID/java
then execute :
kill -9 PID
For MAC:
lsof -n -i :'port-number' | grep LISTEN
Sample Response :
java 4744 (PID) test 364u IP0 asdasdasda 0t0 TCP *:port-number (LISTEN)
and then execute :
kill -9 PID
Worked on Macbook
To check all ports:
netstat -lnp
To close an open port:
fuser -k port_no/tcp
Example:
fuser -k 8080/tcp
In both cases you can use the sudo command if needed.
You can use tcpkill (part of the dsniff package) to kill the connection that's on the port you need:
sudo tcpkill -9 port PORT_NUMBER
The "netstat --programs" command will give you the process information, assuming you're the root user. Then you will have to kill the "offending" process which may well start up again just to annoy you.
Depending on what you're actually trying to achieve, solutions to that problem will vary based on the processes holding those ports. For example, you may need to disable services (assuming they're unneeded) or configure them to use a different port (if you do need them but you need that port more).
Kill the process that is listening to the port in question. I believe netstat shows you process ids.
If you really want to kill a process immediately, you send it a KILL signal instead of a TERM signal (the latter a request to stop, the first will take effect immediately without any cleanup). It is easy to do:
kill -KILL <pid>
Be aware however that depending on the program you are stopping, its state may get badly corrupted when doing so. You normally only want to send a KILL signal when normal termination does not work. I'm wondering what the underlying problem is that you try to solve and whether killing is the right solution.
I think the only way will be to stop the process which has opened the port.
sudo killall -9 "process name"
Shutting down the computer always kills the process for me.

Resources